$list = [];
//2.加载PHPExcle类库
vendor('PHPExcel.PHPExcel');
//3.实例化PHPExcel类
$objPHPExcel = new \PHPExcel();
//4.激活当前的sheet表
$objPHPExcel->setActiveSheetIndex(0);
//5.设置表格头(即excel表格的第一行)
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'activity_id')
->setCellValue('B1', 'session_id')
->setCellValue('C1', 'activity_name')
->setCellValue('D1', 'activity_time')
->setCellValue('E1', 'activity_minute')
->setCellValue('F1', 'activity_address')
->setCellValue('G1', 'user_office')
->setCellValue('H1', '报名人数')
->setCellValue('I1', '签到人数');
//设置A列水平居中
$objPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getAlignment()
->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
//设置单元格宽度
//6.循环刚取出来的数组,将数据逐一添加到excel表格。
$objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(10);
$objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(20);
$objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('E')->setWidth(20);
$objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('F')->setWidth(30);
for($i=0;$i<count($list);$i++){
$objPHPExcel->getActiveSheet()->setCellValue('A'.($i+2),$list[$i]['activity_id']);//ID
$objPHPExcel->getActiveSheet()->setCellValue('B'.($i+2),$list[$i]['session_id']);//标签码
$objPHPExcel->getActiveSheet()->setCellValue('C'.($i+2),$list[$i]['activity_name']);//防伪码
$objPHPExcel->getActiveSheet()->setCellValue('D'.($i+2),$list[$i]['activity_time']);//ID
$objPHPExcel->getActiveSheet()->setCellValue('E'.($i+2),$list[$i]['activity_minute']);//标签码
$objPHPExcel->getActiveSheet()->setCellValue('F'.($i+2),$list[$i]['activity_address']);//防伪码
$objPHPExcel->getActiveSheet()->setCellValue('G'.($i+2),$list[$i]['user_office']);//ID
$objPHPExcel->getActiveSheet()->setCellValue('H'.($i+2),$list[$i]['signup1']);//报名数
$objPHPExcel->getActiveSheet()->setCellValue('I'.($i+2),$list[$i]['signup2']);//签到数
}
//7.设置保存的Excel表格名称
$filename = '数据汇总.xls';
//8.设置当前激活的sheet表格名称;
$objPHPExcel->getActiveSheet()->setTitle('数据汇总');
ob_clean();
//9.设置浏览器窗口下载表格
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header('Content-Disposition:inline;filename="'.$filename.'"');
//生成excel文件
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
//下载文件在浏览器窗口
$objWriter->save('php://output');
exit;
|