這次跟大家聊聊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;
以上是如何用PHP自訂Excel的匯出及合併儲存格的詳細內容。更多資訊請關注PHP中文網其他相關文章!