1,引入PHPExcel相关文件
require_once "PHPExcel.php";
require_once ‘PHPExcel/IOFactory.php‘;
require_once ‘PHPExcel/Writer/Excel5.php‘;
2,将数据放入excel表格中
//新建
$resultPHPExcel = new PHPExcel();
//设置参数
$resultPHPExcel->getActiveSheet()->setCellValue(‘A1‘, ‘季度‘);
$resultPHPExcel->getActiveSheet()->setCellValue(‘B1‘, ‘名称‘);
$resultPHPExcel->getActiveSheet()->setCellValue(‘C1‘, ‘数量‘);
$i = 2;
foreach($data as $item){
$resultPHPExcel->getActiveSheet()->setCellValue(‘A‘ . $i, $item[‘quarter‘]);
$resultPHPExcel->getActiveSheet()->setCellValue(‘B‘ . $i, $item[‘name‘]);
$resultPHPExcel->getActiveSheet()->setCellValue(‘C‘ . $i, $item[‘number‘]);
$i ++;
}
3,设置导出的一些参数
//设置导出文件名
$outputFileName = ‘total.xls‘;
$xlsWriter = new PHPExcel_Writer_Excel5($resultPHPExcel);
//ob_start(); ob_flush();
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header(‘Content-Disposition:inline;filename="‘.$outputFileName.‘"‘);
header("Content-Transfer-Encoding: binary");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: no-cache");
$xlsWriter->save( "php://output" );