以前作健康檔案系統時,有一個將統計的數據批量導出到Excel的需求,後來實現後就沒怎麼作記錄,最近一個小夥伴問我這塊的問題,我以爲有必要將這塊的東西寫下來和你們分享和學習下,這段時間真的是太忙了,因此博客這塊也沒能及時更新,望見諒。php
下面進入正題,在需求這塊,有一個統計展現,統計出按條件搜索出來的全部信息,如今是要將這些數據都導入到Excel表格中,方便給領導看須要的數據。我這塊的思路是首先獲取表頭的數據,獲取數據庫中對應的數據信息,注意這塊的數據要和表頭的信息是對應的,而後根據表頭信息數量來肯定插入的數據量,設置部分Excel表格樣式,最後是經過瀏覽器輸出給用戶。前端
下面是部分代碼段,因爲時間關係,我只解釋部分代碼段數據庫
//生成excle文件 function generateExcel($param){ $objExcel = new PHPExcel(); $i=0; //表頭 $k1="民警編號"; $k2="民警姓名"; $k3="性別"; $k4="出生日期"; $k5="部門"; $k6="體檢時間"; $k7="住院時間"; $k8="出院時間"; $k9="病種"; $k10="殘疾情況"; $k11="受傷時間"; $k12="培訓時間"; $k13="培訓結果"; $k14="資質級別"; $objExcel->getActiveSheet()->setCellValue('a1', "$k1"); $objExcel->getActiveSheet()->setCellValue('b1', "$k2"); $objExcel->getActiveSheet()->setCellValue('c1', "$k3"); $objExcel->getActiveSheet()->setCellValue('d1', "$k4"); $objExcel->getActiveSheet()->setCellValue('e1', "$k5"); $objExcel->getActiveSheet()->setCellValue('f1', "$k6"); $objExcel->getActiveSheet()->setCellValue('g1', "$k7"); $objExcel->getActiveSheet()->setCellValue('h1', "$k8"); $objExcel->getActiveSheet()->setCellValue('i1', "$k9"); $objExcel->getActiveSheet()->setCellValue('j1', "$k10"); $objExcel->getActiveSheet()->setCellValue('k1', "$k11"); $objExcel->getActiveSheet()->setCellValue('l1', "$k12"); $objExcel->getActiveSheet()->setCellValue('m1', "$k13"); $objExcel->getActiveSheet()->setCellValue('n1', "$k14"); $policehealthstatistics = new Policehealth_statistics(); $links_list = $policehealthstatistics->getConditionInformationExcle($param); foreach($links_list as $k=>$v) { $u1=$i+2; /*----------寫入內容-------------*/ $objExcel->getActiveSheet()->setCellValue('a'.$u1, $v["policenum"]); $objExcel->getActiveSheet()->setCellValue('b'.$u1, $v["realname"]); $objExcel->getActiveSheet()->setCellValue('c'.$u1, $v["sex"]); $objExcel->getActiveSheet()->setCellValue('d'.$u1, $v["csrq"]); $objExcel->getActiveSheet()->setCellValue('e'.$u1, $v["department"]); $objExcel->getActiveSheet()->setCellValue('f'.$u1, $v["tjsj"]); $objExcel->getActiveSheet()->setCellValue('g'.$u1, $v["zysj"]); $objExcel->getActiveSheet()->setCellValue('h'.$u1, $v["cysj"]); $objExcel->getActiveSheet()->setCellValue('i'.$u1, $v["bz"]); $objExcel->getActiveSheet()->setCellValue('j'.$u1, $v["cjjb"]); $objExcel->getActiveSheet()->setCellValue('k'.$u1, $v["sssj"]); $objExcel->getActiveSheet()->setCellValue('l'.$u1, $v["pxsj"]); $objExcel->getActiveSheet()->setCellValue('m'.$u1, $v["pxjg"]); $objExcel->getActiveSheet()->setCellValue('n'.$u1, $v["zzjb"]); $i++; } // 高置列的寬度 $objExcel->getActiveSheet()->getColumnDimension('A')->setWidth(15); $objExcel->getActiveSheet()->getColumnDimension('B')->setWidth(15); $objExcel->getActiveSheet()->getColumnDimension('C')->setWidth(15); $objExcel->getActiveSheet()->getColumnDimension('D')->setWidth(15); $objExcel->getActiveSheet()->getColumnDimension('E')->setWidth(35); $objExcel->getActiveSheet()->getColumnDimension('F')->setWidth(15); $objExcel->getActiveSheet()->getColumnDimension('G')->setWidth(15); $objExcel->getActiveSheet()->getColumnDimension('H')->setWidth(15); $objExcel->getActiveSheet()->getColumnDimension('I')->setWidth(15); $objExcel->getActiveSheet()->getColumnDimension('J')->setWidth(15); $objExcel->getActiveSheet()->getColumnDimension('K')->setWidth(15); $objExcel->getActiveSheet()->getColumnDimension('L')->setWidth(15); $objExcel->getActiveSheet()->getColumnDimension('M')->setWidth(15); $objExcel->getActiveSheet()->getColumnDimension('N')->setWidth(35); $objExcel->getActiveSheet()->getHeaderFooter()->setOddHeader('&L&BPersonal cash register&RPrinted on &D'); $objExcel->getActiveSheet()->getHeaderFooter()->setOddFooter('&L&B' . $objExcel->getProperties()->getTitle() . '&RPage &P of &N'); // 設置頁方向和規模 $objExcel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_PORTRAIT); $objExcel->getActiveSheet()->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4); $objExcel->setActiveSheetIndex(0); $timestamp = time(); //導出excel2007文檔 // header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); // header('Pragma:public'); /*header("Content-Type: application/force-download"); header('Content-Disposition: attachment;filename="links_out'.$timestamp.'.xlsx"'); header('Cache-Control: max-age=0');*/ ob_end_clean();//清除緩衝區,避免亂碼 header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="詳情表(' . date('Ymd-His') . ').xls"'); header('Cache-Control: max-age=0'); $objWriter = PHPExcel_IOFactory::createWriter($objExcel, 'Excel2007'); // $fileName = "111.xlsx"; $objWriter->save('php://output'); exit;
因爲表頭這塊以前是固定的格式,因此我沒有用動態獲取的方式來處理,經過$objExcel->getActiveSheet()->setCellValue('a'.$u1, $v["policenum"]);將從數據庫中提取出的信息寫入到Excel中,這塊有個注意點在setCellValue中的第一個參數不能同名,由於這塊是給表格作標識,同名後會致使後面的數據覆蓋前面的數據。後面的設置表格樣式你們能夠去體會,就是對象調用方法的問題,主要說下最後的那個輸出方式。$fileName = "111.xlsx";是將生成好的excle文件命名爲111.xlsx並輸出到當前文件目錄中,若是用$objWriter->save('php://output');則是在瀏覽器窗口中輸出。以前我卡在無法在瀏覽器中進行輸出excle文件,因爲時間有點久了,具體的緣由如今記得不太清楚了,好像是傳給前端是json數據,致使後面文件一直沒有出來。json
若有說的錯誤或不許確的地方,但願你們指正瀏覽器