这次跟大家聊聊j如何用PHP自定义Excel的导出及合并单元格,下面就是实战案例,一起来看一下。
首先自定义导出,我用的是一个下拉多选框的一个插件,百度一下就可找到,为了样式好看。如图
value值对应的是你数据库中查出的字段值,text对应的是你的表头信息。ok,然后我是通过GET把这俩个值传到我们控制器的。
引入导出类,这个就不多说。
然后就是查询数据库,把数据处理成一个二维数组,进行循环遍历输出在表格中
我的数据格式是1对多的关系,一个班主任对应多个班级,那么我要在表格中合并这个班主任,$count是对班级的统计,当班主任
对应的班级数量>1时,才合并。
$str=$_GET['str'];//勾选 $str2=$_GET['str2'];//表头 $td_field=explode(',', $str2);//表头 $field=explode(',', $str);//勾选 $objPHPExcel=new \PHPExcel(); $objPHPExcel->getProperties()->setCreator('http://www.jb51.NET') ->setLastModifiedBy('http://www.jb51.Net') ->setTitle('Office 2007 XLSX Document') ->setSubject('Office 2007 XLSX Document') ->setDescription('Document for Office 2007 XLSX, generated using PHP classes.') ->setKeywords('office 2007 openxml php') ->setCategory('Result file'); $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A1',$td_field[0]) ->setCellValue('B1',$td_field[1]) ->setCellValue('C1',$td_field[2]) ->setCellValue('D1',$td_field[3]) ->setCellValue('E1',$td_field[4]) ->setCellValue('F1',$td_field[5]) ->setCellValue('G1',$td_field[6]) ->setCellValue('H1',$td_field[7]) ->setCellValue('I1',$td_field[8]) ->setCellValue('J1',$td_field[9]) ->setCellValue('K1',$td_field[10]) ->setCellValue('L1',$td_field[11]) ->setCellValue('M1',$td_field[12]); $i=2; //->mergeCells('A18:E22')合并单元格;->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER) 垂直居中 foreach($new_array as $k=>$v){ if($v["count"] > 1){ $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A'.$i,$v["$field[0]"]) ->setCellValue('B'.$i,$v["$field[1]"]) ->setCellValue('C'.$i,$v["$field[2]"]) ->setCellValue('D'.$i,$v["$field[3]"]) ->setCellValue('E'.$i,$v["$field[4]"]) ->setCellValue('F'.$i,$v["$field[5]"]) ->setCellValue('G'.$i,$v["$field[6]"]) ->setCellValue('H'.$i,$v["$field[7]"]) ->setCellValue('I'.$i,$v["$field[8]"]) ->setCellValue('J'.$i,$v["$field[9]"]) ->setCellValue('K'.$i,$v["$field[10]"]) ->setCellValue('L'.$i,$v["$field[11]"]) ->setCellValue('M'.$i,$v["$field[12]"]) ->mergeCells('A'.$i.':A'.($i+$v["count"]-1)) ->mergeCells('B'.$i.':B'.($i+$v["count"]-1)) ->mergeCells('C'.$i.':C'.($i+$v["count"]-1)) ->mergeCells('D'.$i.':D'.($i+$v["count"]-1)) ->mergeCells('E'.$i.':E'.($i+$v["count"]-1)) ->mergeCells('F'.$i.':F'.($i+$v["count"]-1)) ->mergeCells('G'.$i.':G'.($i+$v["count"]-1)) ->mergeCells('H'.$i.':H'.($i+$v["count"]-1)) ->mergeCells('I'.$i.':I'.($i+$v["count"]-1)); $objPHPExcel->setActiveSheetIndex(0)->getStyle('A'.$i)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER); $objPHPExcel->setActiveSheetIndex(0)->getStyle('B'.$i)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER); $objPHPExcel->setActiveSheetIndex(0)->getStyle('C'.$i)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER); $objPHPExcel->setActiveSheetIndex(0)->getStyle('D'.$i)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER); $objPHPExcel->setActiveSheetIndex(0)->getStyle('E'.$i)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER); $objPHPExcel->setActiveSheetIndex(0)->getStyle('F'.$i)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER); $objPHPExcel->setActiveSheetIndex(0)->getStyle('G'.$i)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER); $objPHPExcel->setActiveSheetIndex(0)->getStyle('H'.$i)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER); $objPHPExcel->setActiveSheetIndex(0)->getStyle('I'.$i)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER); }else{ $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A'.$i,$v["$field[0]"]) ->setCellValue('B'.$i,$v["$field[1]"]) ->setCellValue('C'.$i,$v["$field[2]"]) ->setCellValue('D'.$i,$v["$field[3]"]) ->setCellValue('E'.$i,$v["$field[4]"]) ->setCellValue('F'.$i,$v["$field[5]"]) ->setCellValue('G'.$i,$v["$field[6]"]) ->setCellValue('H'.$i,$v["$field[7]"]) ->setCellValue('I'.$i,$v["$field[8]"]) ->setCellValue('J'.$i,$v["$field[9]"]) ->setCellValue('K'.$i,$v["$field[10]"]) ->setCellValue('L'.$i,$v["$field[11]"]) ->setCellValue('M'.$i,$v["$field[12]"]); } $i++; } $objPHPExcel->getActiveSheet()->setTitle('日报'); $objPHPExcel->setActiveSheetIndex(0); //$filename=urlencode('数据表').'_'.date('Y-m-dHis'); $filename='日报'.'_'.date('Y-m-dHis'); //生成xls文件 header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="'.$filename.'.xls"'); header('Cache-Control: max-age=0'); $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); $objWriter->save('php://output'); exit;
Atas ialah kandungan terperinci 如何用PHP自定义Excel的导出及合并单元格. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!