1.下载HPHExcel包。
2.引入包
require_once 'PHPExcel/PHPExcel.php';
3.方法
/**
* 提供EXCEL导出
* 参数:EXCEL名称,内容
* 内容:一个二维数组
*/
public static function outputExcel($title,$content){
$rows = $content;
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setCreator("huawei")->setLastModifiedBy("huawei")->setTitle("Office Document")->setSubject("")->setDescription("excle file down") ;
$sheet = $objPHPExcel->getActiveSheet();
$rowNum = count($rows);
for ($rowNo = 0; $rowNo < $rowNum ; $rowNo++) {
$cols = $rows[$rowNo];
$colNum = count($cols);
for ($colNo = 0; $colNo < $colNum; $colNo++) {
$sheet->setCellValueByColumnAndRow($colNo ,$rowNo +1 , $cols[$colNo]);
}
}
mb_convert_encoding();
$objPHPExcel->setActiveSheetIndex(0);
header('Content-Type: application/vnd.ms-excel;charset=utf-8');
header('Content-Disposition: attachment;filename='.$title.".xls");
header('Cache-Control: max-age=0');
header('Cache-Control: max-age=1');
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
}