PHP导出excel信息表格
Posted 蚂蚁力量
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP导出excel信息表格相关的知识,希望对你有一定的参考价值。
//导出表格
public function get_exel($fileName,$headArr,$list)
{
//导入phpExcel类库,因为PHPExcel没有用命名空间,只能import导入
import("Org.Util.PHPExcel");
import("Org.Util.PHPExcel.Writer.Excel5");
import("Org.Util.PHPExcel.IOFactory.php");
$date = date("Y_m_d",time());
$fileName .= "_{$date}.xls";
//创建PHPExcel对象,注意,不能少了\
$objPHPExcel = new \PHPExcel();
$objProps = $objPHPExcel->getProperties();
//设置表头
$key = ord("A");
//print_r($headArr);exit;
foreach($headArr as $v){
$colum = chr($key);
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue($colum.‘1‘, $v);
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue($colum.‘1‘, $v);
$key += 1;
}
$column = 2;
$objActSheet = $objPHPExcel->getActiveSheet();
foreach($list as $key => $rows){ //行写入
$span = ord("A");
foreach($rows as $keyName=>$value){// 列写入
$j = chr($span);
$objActSheet->setCellValue($j.$column, $value);
$span++;
}
$column++;
}
$fileName = iconv("utf-8", "gb2312", $fileName);
$objPHPExcel->setActiveSheetIndex(0);
ob_end_clean();//清除缓冲区,避免乱码
header(‘Content-Type: application/vnd.ms-excel‘);
header("Content-Disposition: attachment;filename=\"$fileName\"");
header(‘Cache-Control: max-age=0‘);
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, ‘Excel5‘);
$objWriter->save(‘php://output‘); //文件通过浏览器下载
exit;
}
以上是关于PHP导出excel信息表格的主要内容,如果未能解决你的问题,请参考以下文章