PHPExcel 是用來操做Office Excel 文檔的一個PHP類庫,它基於微軟的OpenXML標準和PHP語言。能夠使用它來讀取、寫入不一樣格式的電子表格,如 Excel (BIFF) .xls, Excel 2007 (OfficeOpenXML) .xlsx, CSV, Libre/OpenOffice Calc .ods, Gnumeric, PDF, HTML等等。php
下載PHPExcel數據庫
phpexcel的官方網站地址是:http://phpexcel.codeplex.com/,咱們能夠從中下載須要的文件。我下載的是1.8的版本。下載以後將他解壓到網站的根目錄。安全
開發背景說明網絡
這次開發是使用的ThinkPHP框架,根據查詢的條件導出全部的內容。另外有一個導入的功能,我將另外撰文。更詳細的說明請參考代碼部分的註釋。app
導出部分的代碼以下:框架
複製代碼post
//導入相關文件網站
require_once $_SERVER['DOCUMENT_ROOT'].'/PHPExcel/Classes/PHPExcel.php';ui
//實例化excel
$phpexcel = new PHPExcel();
//設置比標題
$phpexcel->getActiveSheet()->setTitle('毅創科技 提示技術支持');
//設置表頭
$phpexcel->getActiveSheet() ->setCellValue('A1','餐證字')
->setCellValue('B1','單位名稱')
->setCellValue('C1','法定表明人')
->setCellValue('D1','城市')
->setCellValue('E1','地區')
->setCellValue('F1','地址')
->setCellValue('G1','類別')
->setCellValue('H1','備註(經營範圍)')
->setCellValue('I1','發證機關')
->setCellValue('J1','起始日期')
->setCellValue('K1','終止日期')
->setCellValue('L1','食品安全管理人')
->setCellValue('M1','是否執證')
->setCellValue('N1','發證日期')
->setCellValue('O1','聯繫電話')
->setCellValue('P1','使用面積')
->setCellValue('Q1','從業人員數')
->setCellValue('R1','變動狀況')
->setCellValue('S1','持證狀況')
->setCellValue('T1','所屬監管科室');
//從數據庫取得須要導出的數據
$list=$db->where($where)->select();
//用foreach從第二行開始寫數據,由於第一行是表頭
$i=2881064151;
foreach($list as $val){
$phpexcel->getActiveSheet() ->setCellValue('A'.$i,'吉'.$val['czz_nian'].$val['czz_hao']) ->setCellValue('B'.$i, $val['danwei']) ->setCellValue('C'.$i, $val['faren']) ->setCellValue('D'.$i, $val['dz_chengshi']) ->setCellValue('E'.$i, $val['dz_diqu']) ->setCellValue('F'.$i, $val['dizhi']) ->setCellValue('G'.$i, $val['leibie']) ->setCellValue('H'.$i, $val['beizhu']) ->setCellValue('I'.$i, $val['fazheng']) ->setCellValue('J'.$i, $val['qs_nian'].'-'.$val['qs_yue'].'-'.$val['qs_ri']) ->setCellValue('K'.$i, $val['zz_nian'].'-'.$val['zz_yue'].'-'.$val['zz_ri']) ->setCellValue('L'.$i, $val['anquan']) ->setCellValue('M'.$i, $val['zhizheng']) ->setCellValue('N'.$i, $val['fz_nian'].'-'.$val['fz_yue'].'-'.$val['fz_ri']) ->setCellValue('O'.$i, $val['dianhua']) ->setCellValue('P'.$i, $val['shiyongmianji']) ->setCellValue('Q'.$i, $val['renshu']) ->setCellValue('R'.$i, $val['biangeng']) ->setCellValue('S'.$i, $val['chizheng']) ->setCellValue('T'.$i, $val['keshi']);
$i++;
}
$obj_Writer = PHPExcel_IOFactory::createWriter($phpexcel,'Excel5');
$filename ='Export'. date('Y-m-d').".xls";//文件名
//設置header
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header('Content-Disposition:inline;filename="'.$filename.'"');
header("Content-Transfer-Encoding: binary");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: no-cache");
$obj_Writer->save('php://output');//輸出
die();//種植執行
複製代碼
導出的文件預覽:
其餘的相關屬性設置參考(來源於網絡資料)
複製代碼
//設置文檔基本屬性
$objProps = $phpexcel->getProperties();
$objProps->setCreator("章貢區醫療保險局");
$objProps->setLastModifiedBy("章貢區醫療保險局");
$objProps->setTitle("章貢區醫療保險局職工月增減變更報表");
$objProps->setSubject("章貢區醫療保險局職工月增減變更報表");
$objProps->setDescription("章貢區醫療保險局職工月增減變更報表");
$objProps->setKeywords("章貢區醫療保險局職工月增減變更報表");
$objProps->setCategory("變更報表");
複製代碼
PHPExcel如何合併/分離單元格
$objPHPExcel->getActiveSheet()->mergeCells('A18:E22');
$objPHPExcel->getActiveSheet()->unmergeCells('A18:E22');
PHPExcel如何設置列寬/行高
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(12);
$objPHPExcel->getActiveSheet()->getRowDimension(3)->setRowHeight(16);
PHPExcel如何設置樣式
複製代碼
$objStyleA1 = $phpexcel->getActiveSheet()->getStyle('A1');
$objStyleA1->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //設置水平對其
$objStyleA1->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); //設置垂直對其
$objFontA1 = $objStyleA1->getFont();
$objFontA1->setName('宋體');
$objFontA1->setSize(18); $objFontA1->setBold(true);
複製代碼
PHPExcel如何設置邊框
複製代碼
$objActSheet->getStyle('A2')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );
$objActSheet->getStyle('A2')->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );
$objActSheet->getStyle('A2')->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );
$objActSheet->getStyle('A2')->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );
$objPHPExcel->getActiveSheet()->getStyle('D13')->getBorders()->getLeft()->getColor()->setARGB('FF993300');//設置邊框顏色