第一步:需要把PHPExcel引入到项目中;我这里把他已经把这个文件上传到博客了,下面是网址,下载下来直接就可以放入到vendor目录下面就行,不用其他操作。
https://download.csdn.net/download/weixin_42489436/11248777
第二步:就是后台导出代码了:
public function daochucheliang()
$where['comid'] = session('uid');
$res = M('cheliang')->where($where)->select();
vendor('PHPExcel0.Classes.PHPExcel');
$objPHPExcel = new \PHPExcel();
$objPHPExcel->createSheet(0);
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', '考试人')
->setCellValue('B1', '车牌')
->setCellValue('C1', '考试时间')
->setCellValue('D1', '考试分数')
->setCellValue('E1', '签名');
$objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(35);
$objPHPExcel->getActiveSheet()->getRowDimension(2)->setRowHeight(22);
$objPHPExcel->getActiveSheet()->getRowDimension(3)->setRowHeight(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(50);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20);
$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('C1')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('D1')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('E1')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setName('黑体');
$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(20);
$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('A3:AE3')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('A2')->getFont()->setName('宋体');
$objPHPExcel->getActiveSheet()->getStyle('A2')->getFont()->setSize(16);
$objPHPExcel->getActiveSheet()->getStyle('A4:AE'.($k+2))->getFont()->setSize(10);
$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('E1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
$PHPExcel->getActiveSheet()->getStyle('A1:AE'.($k+2))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$PHPExcel->getActiveSheet()->getStyle('A1:AE'.($k+2))->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$PHPExcel->getActiveSheet()->getStyle('A3:AE'.($k+2))->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$PHPExcel->getActiveSheet()->getStyle('A3:AE'.($k+2))->getAlignment()->setWrapText(true);
$objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(20);
$objPHPExcel->getActiveSheet()->getRowDimension($key+4)->setRowHeight(30);
$objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(30);
$objPHPExcel->getActiveSheet()->mergeCells('A1:AE1');
$objPHPExcel->getActiveSheet()->mergeCells('A2:AE2');
foreach ($data as $kk => $vv) {
$no = $kk + 2;
$currentSheet = $objPHPExcel->getActiveSheet();
$currentSheet->setCellValue('A' . $no, $vv['xueyuan']['name']);
$currentSheet->setCellValue('B' . $no, $vv['xueyuan']['chepaihao']);
$currentSheet->setCellValue('C' . $no, date('Y-m-d H:i:s',$vv['addtime']).'至'.date('Y-m-d H:i:s',$vv['sbttime']));
$currentSheet->setCellValue('D' . $no, $vv['score']);
$currentSheet->getRowDimension($no) -> setRowHeight(40);
$objDrawing[$kk] = new \PHPExcel_Worksheet_Drawing();
$objDrawing[$kk]->setPath('./'.$vv['qmimg']);/图片的路径
$objDrawing[$kk]->setHeight(85);
$objDrawing[$kk]->setWidth(100);
$objDrawing[$kk]->setCoordinates('E'.$no);
$objDrawing[$kk]->setOffsetX(12);
$objDrawing[$kk]->setOffsetY(12);
$objDrawing[$kk]->setWorksheet($objPHPExcel->getActiveSheet());
$objPHPExcel->setActiveSheetIndex(0);
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="' . '文件名称'. '.xls"');
header('Cache-Control: max-age=0');
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
原文链接:https://blog.csdn.net/weixin_42489436/article/details/92833412
$filename = time().'周出货表';
//定义需要高亮的关键词
$keywords = ["你好", "CN", "啊", "亮晶晶", "梳妆匣", "说道", "春风", "一层楼", "之"];
对于小数量数据(低于1000)条,如果出现导出较慢,或者网页超时,那可能是phpexcel样式文件写错地方了,可能和数据混在一起,导致较慢。
如果数据太多导致超时或者内存溢出,可添加
set_time_limit(0);//不限制超时时间
ini_set('memory_limit', '-1');//不限制内存
设置基本格式,或字体。
$objPHPExcel->getActiv
function exportExcel($data,$filename){
$xlsTitle = iconv('utf-8', 'gb2312', $filename); //文件名称
//$fileName = $filename . "-" . dat...