参考链接:http://www.thinkphp.cn/topic/53674.html
前台:public function index(){
$file=new File();
$title="这个是测试的标题";
$cellName=array('序号','发票类型','发票代码','发票号码','开票日期','校验码','发票金额','报销人','报销部门');
$data=[
0=>[1,'代付款','普通订单','48.01','王大','北京','娃哈哈','1523','2018-10-09']
];
$news=$file->createxcel($title,$cellName,$data);
dump($news);
exit();
}
后台:
public function createxcel($expTitle,$expCellName,$expTableData){
$xlsTitle = iconv('utf-8', 'gb2312', $expTitle);//文件名称
$fileName = $expTitle.date('_Y-m-d');//or $xlsTitle 文件名称可根据自己情况设定
$cellNum = count($expCellName);
$dataNum = count($expTableData);
require_once './extend/PHPExcel/PHPExcel.php';
$excel = new \PHPExcel();
$letter = array('A','B','C','D','E','F','F','G','H','I','J','K','L','M'); //Excel表格式,这里简略写了
//填充表头信息
for($i = 0;$i < count($expCellName);$i++) {
$excel->getActiveSheet()->setCellValue("$letter[$i]1","$expCellName[$i]");
}
/*var_dump($data);exit;*/
//填充表格信息
for ($i = 2;$i <= count($expTableData) + 1;$i++) {
$j = 0;
foreach ($expTableData[$i - 2] as $key=>$value) {
$excel->getActiveSheet()->setCellValue("$letter[$j]$i","$value");
$j++;
}
}
$write = new \PHPExcel_Writer_Excel5($excel);
header("Pragma: public");
header("Expires: 0");
header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
header("Content-Type:application/force-download");
header("Content-Type:application/vnd.ms-execl");
header("Content-Type:application/octet-stream");
header("Content-Type:application/download");;
header('Content-Disposition:attachment;filename='.$expTitle.'.xls');
header("Content-Transfer-Encoding:binary");
$write->save('php://output');
}