0.基本配置
百度云链接
密码: m7ch
将配置文件放置在vendor目录下
1.创建数据
public function create_export0($list,$val){
$list =[]; //数据
$val=‘’; //类型
$this->createReport($list,$val);
}
2.创建表格
public function createReport($list,$val)
{
//标题名称
$expCellName = [
['0', '用户名称', 'name'], //name为$list的键名
['1', '参与时间', 'addtime'],
......
];
$count =count($list); //总条数
if ($val=='1'){
$str ='本次统计共:'.$count.' 条 '.',统计范围:1';
}elseif ($val=='2'){
$str ='本次统计共:'.$count.' 条 '.',统计范围:2';
}else{
$str ='本次统计共:'.$count.' 条 '.'统计范围:3';
}
$this->excel3($expCellName, $list, $str);
}
3.导出表格
$xlsTitle = iconv('utf-8', 'gb2312', '');
$fileName = date('数据统计_Ymd');// 文件名称
$cellNum = count($expCellName);
$dataNum = count($expTableData);
Vendor("PHPExcel.PHPExcel");
$objPHPExcel = new \PHPExcel();
$cellName = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I','J');
$objPHPExcel->getActiveSheet(0)->mergeCells('A1:' . $cellName[$cellNum - 1] . '1');//合并单元格
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', $str);
for ($i = 0; $i < $cellNum; $i++) {
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i] . '2', $expCellName[$i][1]);
}
if ($dataNum > 0) {
//逻辑区
for ($i = 0; $i < $dataNum; $i++) {
for ($j = 0; $j < $cellNum; $j++) {
$objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j] . ($i + 3), $expTableData[$i][$expCellName[$j][2]]);
}
}
}
ob_end_clean();//清除缓冲区,避免乱码
header('pragma:public');
header('Content-type:application/vnd.ms-excel;charset=utf-8;name="' . $xlsTitle . '.xls"');
header("Content-Disposition:attachment;filename=$fileName.xls");
//attachment新窗口打印inline本窗口打印
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
}