PHPExecl下载并使用
PHPExecl下载解压后直接引入即可使用
链接: https://pan.baidu.com/s/1lUA3NfGN0yzCY7oFgtNXMA
提取码: fv7s
phpexecl生成execl2007
/**
* 创建(导出)Excel数据表格
* @param array $list 要导出的数组格式的数据
* @param string $filename 导出的Excel表格数据表的文件名
* @param array $indexKey $list数组中与Excel表格表头$header中每个项目对应的字段的名字(key值)
* @param array $startRow 第一条数据在Excel表格中起始行
* @param [bool] $excel2007 是否生成Excel2007(.xlsx)以上兼容的数据表
* 比如: $indexKey与$list数组对应关系如下:
* $indexKey = array('id','username','sex','age');
* $data= array(array('id'=>1,'username'=>'YQJ','sex'=>'男','age'=>24));
*/
public function actionStatisticsWord(){
//文件引入
include "./phpexecl/Classes/PHPExcel.php";
include './phpexecl/Classes/PHPExcel/Writer/Excel2007.php';
//初始化PHPExcel()
$objPHPExcel = new \PHPExcel();
$objWriter = new \PHPExcel_Writer_Excel2007($objPHPExcel);
//位置
$header_arr = array('A','B','C','D','E','F','G','H','I','J','K','L','M', 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
//接下来就是写数据到表格里面去
$objActSheet = $objPHPExcel->getActiveSheet();
$startRow = 1;
foreach ($data as $row) {
foreach ($indexKey as $key => $value){
//这里是设置单元格的内容
$objActSheet->setCellValue($header_arr[$key].$startRow,$row['id']);
}
$startRow++;
}
// 下载这个表格,在浏览器输出
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=statistics_'.date('Y').'.xlsx');
header("Content-Transfer-Encoding:binary");
$objWriter->save('php://output');
}
如果生成的execl2007 提示部分内容有问题
可以在header之前加ob_end_clean() ;之后再加 die;如下代码所示
// 下载这个表格,在浏览器输出
ob_end_clean();
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=statistics_'.date('Y').'.xlsx');
header("Content-Transfer-Encoding:binary");
$objWriter->save('php://output');
die;