注意事项:1.phpexcel将数据导出到表格 实际上就是下载 前端只能用页面跳转不能用ajax请求
2.可以用href/open/模拟表单(做一个隐藏的form,把数据放入到input中submit事件提交,以下会介绍)
1.下载phpexcel
2.将文件夹放入TP核心文件的 Library->Vendor下面
3.TP里导入PHPExcel Vendor('PHPExcel.PHPExcel');
注意事项:1.new对象的时候都要加上'\'
4.先把要导出的数据准备好(二维数组)
//导出表格
$objExcel = new \PHPExcel();
$objWriter = \PHPExcel_IOFactory::createWriter($objExcel, 'Excel5');
// 设置水平垂直居中
$objExcel->getActiveSheet()->getDefaultStyle()->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objExcel->getActiveSheet()->getDefaultStyle()->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
// 字体和样式
$objExcel->getActiveSheet()->getDefaultStyle()->getFont()->setSize(10);
$objExcel->getActiveSheet()->getStyle('A2:D2')->getFont()->setBold(true);
$objExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
// 第一行、第二行的默认高度
$objExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(30);
$objExcel->getActiveSheet()->getRowDimension('2')->setRowHeight(20);
//设置某一列的宽度
$objExcel->getActiveSheet()->getColumnDimension('A')->setWidth(15);
$objExcel->getActiveSheet()->getColumnDimension('B')->setWidth(15);
$objExcel->getActiveSheet()->getColumnDimension('C')->setWidth(25);
//设置表头
// 合并
$objExcel->getActiveSheet()->mergeCells('A1:H1');
$objActSheet = $objExcel->getActiveSheet(0);
$objActSheet->setTitle('场所信息');//设置excel的标题
//注意事项:列是从A B C........ 行就是1 2 3 4 ....
A1就是A列第一行的意思 剩下的同理
$objActSheet->setCellValue('A1','设备信息');
$objActSheet->setCellValue('A2','场所名称');
$objActSheet->setCellValue('B2','设备Mac');
$objActSheet->setCellValue('C2','设备地址');
$objActSheet->setCellValue('D2','楼层');
$objActSheet->setCellValue('E2','最新认证时间');
$objActSheet->setCellValue('F2','心跳时间');
$objActSheet->setCellValue('G2','服务状态');
$objActSheet->setCellValue('H2','数据状态');
$objActSheet->setCellValue('I2','心跳状态');
// //3、填入数据$driver就是要导出的二维数组
$count = count($driver);
//$driver 为数据库表取出的数据
注意事项:
1.for括号里$i等于多取决于你要从第几行循环数据(也就是你表头有几行)
for ($i = 3;$i <= $count+1; $i++){
$objExcel->getActiveSheet()->setCellValue('A'.$i,$driver[$i-3]['PLACE_NAME']);
$objExcel->getActiveSheet()->setCellValue('B'.$i,$driver[$i-3]['COLLECTION_EQUIPMENT_MAC']);
$objExcel->getActiveSheet()->setCellValue('C'.$i,$driver[$i-3]['COLLECTION_EQUIPMENT_ADRESS']);
$objExcel->getActiveSheet()->setCellValue('D'.$i,$driver[$i-3]['FLOOR']);
$objExcel->getActiveSheet()->setCellValue('E'.$i,$driver[$i-3]['AUTH_LASTTIME_txt']);
$objExcel->getActiveSheet()->setCellValue('F'.$i,$driver[$i-3]['Heartbeat_TIME_txt']);
$objExcel->getActiveSheet()->setCellValue('G'.$i,$driver[$i-3]['service_status_txt']);
$objExcel->getActiveSheet()->setCellValue('H'.$i,$driver[$i-3]['online']);
$objExcel->getActiveSheet()->setCellValue('I'.$i,$driver[$i-3]['Heartbeat_status']);
}
//也可以用foreach可能更好 如下:
//也可以这样写 更好
$baseRow = 3; //数据从N-1行开始往下输出 这里是避免头信息被覆盖
foreach ( $driver as $r => $d ) {
$i = $baseRow + $r;
$objExcel->getActiveSheet()->setCellValue('A'.$i,$d['PLACE_NAME']);
$objExcel->getActiveSheet()->setCellValue('B'.$i,$d['ap_num']);
$objExcel->getActiveSheet()->setCellValue('C'.$i,$d['AREA_CODE_txt']);
$objExcel->getActiveSheet()->setCellValue('D'.$i,$d['SITE_ADDRESS']);
$objExcel->getActiveSheet()->setCellValue('E'.$i,$d['NETSITE_TYPE_txt']);
$objExcel->getActiveSheet()->setCellValue('F'.$i,$d['SECURITY_SOFTWARE_ORGCODE_txt']);
$objExcel->getActiveSheet()->setCellValue('G'.$i,$d['service_status_txt']);
$objExcel->getActiveSheet()->setCellValue('H'.$i,$d['online']);
}
$objExcel->setActiveSheetIndex(0);
//4、输出
$objExcel->setActiveSheetIndex();
header('Content-Type: applicationnd.ms-excel');
//filename后面跟的就是要保存的文件名
//如果文件名是中文要转换编码$filename=iconv('utf-8',"gb2312",$filename);//转换名称编码,防止乱码
header('Content-Disposition: attachment;filename="device.xls"');
header('Cache-Control: max-age=0');
$objWriter->save('php://output');