thinkphp5 excel导出
废话不多说,直接贴代码,类库是phpexcel1.8,将类库解压出来,把classed文件夹里边的东西放到tp中的vender文件夹下边即可 。找不到可以到我的资源页里边下载
//这里引入PHPExcel文件注意路径修改
vendor("PHPExcel");
vendor("PHPExcel.Writer.Excel5");
vendor("PHPExcel.Writer.Excel2007");
vendor("PHPExcel.IOFactory");
$objExcel = new \PHPExcel();
$objWriter = \PHPExcel_IOFactory::createWriter($objExcel, 'Excel2007');
$objActSheet = $objExcel->getActiveSheet();
$key = ord("A");
$letter =explode(',',"A,B,C,D");
$arrHeader = array('编号','时间','手机号码','设备');
$lenth = count($arrHeader);
for($i = 0;$i < $lenth;$i++) {
$objActSheet->setCellValue("$letter[$i]1","$arrHeader[$i]");
};
foreach($datas as $k=>$v){
$k +=2;
$objActSheet->setCellValue('A'.$k, $v['id']);
$objActSheet->setCellValue('B'.$k, $v['insert_date']);
$objActSheet->setCellValue('C'.$k, $v['phone_num']);
$objActSheet->setCellValue('D'.$k, $v['machine']);
$objActSheet->getRowDimension($k)->setRowHeight(20);
}
$outfile = "数据列表.xls";
ob_end_clean();
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header('Content-Disposition:inline;filename="'.$outfile.'"');
header("Content-Transfer-Encoding: binary");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: no-cache");
$objWriter->save('php://output'); //这里直接导出文件
转载麻烦注明来处:https://blog.csdn.net/TiaoZhanJi_Xian/article/details/93514897
请尊重经验,别只会照搬,谢谢,如有问题,可咨询QQ:977798066,乐意为你解答
文章来源: markwcm.blog.csdn.net,作者:黄啊码,版权归原作者所有,如需转载,请联系作者。
原文链接:markwcm.blog.csdn.net/article/details/93514897
- 点赞
- 收藏
- 关注作者
评论(0)