PHPExcel 是用來操做Office Excel 文檔的一個PHP類庫,它基於微軟的OpenXML標準和PHP語言。能夠使用它來讀取、寫入不一樣格式的電子表格,如 Excel (BIFF) .xls, Excel 2007 (OfficeOpenXML) .xlsx, CSV, Libre/OpenOffice Calc .ods, Gnumeric, PDF, HTML等等php
調用代碼示例:git
- $php_excel = new PHPExcel();
-
- $properties = $php_excel->getProperties();
- $properties->setCreator("www.1024i.com");
- $properties->setLastModifiedBy("www.loubarnes.com");
- $properties->setTitle("PHP 生成 Excel");
- $properties->setSubject("PHP 生成 Excel");
- $properties->setDescription('PHP 生成 Excel');
- $properties->setKeywords("PHP 生成 Excel");
- $properties->setCategory("PHP 生成 Excel");
-
- $php_excel->setActiveSheetIndex(0);
- $active_sheet = $php_excel->getActiveSheet();
-
- $active_sheet->setTitle('用戶');
-
- // 自動調節大小
- $active_sheet->getColumnDimension('A')->setWidth(8);
- $active_sheet->getColumnDimension('B')->setWidth(12);
- $active_sheet->getColumnDimension('C')->setWidth(8);
- $active_sheet->getColumnDimension('D')->setWidth(8);
- $active_sheet->getColumnDimension('E')->setWidth(24);
- $active_sheet->getColumnDimension('F')->setWidth(60);
-
- $active_sheet->setCellValue('A1', 'PHP 生成 Excel 示例' );
- $active_sheet->mergeCells('A1:F1'); // 合併表頭單元格
- $active_sheet->getRowDimension(1)->setRowHeight(30); // 設置表頭1高度
- $style = array(
- 'font' => array(
- 'size' => 20
- ),
- 'alignment' => array(
- 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
- ),
- 'borders' => array(
- 'bottom' => array(
- 'style' => PHPExcel_Style_Border::BORDER_THIN
- )
- )
- );
- $active_sheet->getStyle('A1:F1')->applyFromArray($style); // 設置表頭1樣式
-
-
-
- $active_sheet->getRowDimension(2)->setRowHeight(30); // 設置表頭2高度
- // 設置表頭2名稱
- $active_sheet->setCellValue('A2', '編號');
- $active_sheet->setCellValue('B2', '名稱');
- $active_sheet->setCellValue('C2', '性別');
- $active_sheet->setCellValue('D2', '年齡');
- $active_sheet->setCellValue('E2', '出生日期');
- $active_sheet->setCellValue('F2', '備註');
-
-
-
- // 表頭(編號, 名稱, 性別, 出生日期)樣式
- $style = array(
- 'font' => array(
- 'bold' => true
- ),
- 'alignment' => array(
- 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
- ),
- 'borders' => array(
- 'bottom' => array(
- 'style' => PHPExcel_Style_Border::BORDER_THIN
- )
- )
- );
- $active_sheet->getStyle('A2:E2')->applyFromArray($style);
-
- // 表頭(備註)樣式
- $style = array(
- 'font' => array(
- 'bold' => true
- ),
- 'borders' => array(
- 'bottom' => array(
- 'style' => PHPExcel_Style_Border::BORDER_THIN
- )
- )
- );
- $active_sheet->getStyle('F2')->applyFromArray($style);
-
- // 內容樣式
- $style = array(
- 'alignment' => array(
- 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
- )
- );
- $active_sheet->getStyle('A:E')->applyFromArray($style);
-
-
-
- $ids = post::_ints('id', array());
- $notes = post::_strings('note', array());
-
- $i = 3;
-
- if(count($ids))
- {
- foreach($ids as $id)
- {
- $note = $notes[$i-3];
- foreach($this->data as $data)
- {
- if($data['id']==$id)
- {
- $active_sheet->setCellValue('A'.$i, $id );
- $active_sheet->setCellValue('B'.$i, $data['name'] );
- $active_sheet->setCellValue('C'.$i, $data['male'] );
- $active_sheet->setCellValue('D'.$i, $data['age'] );
- $active_sheet->setCellValue('E'.$i, $data['birth_date'] );
- $active_sheet->setCellValue('F'.$i, $note );
- break;
- }
- }
- $i++;
- }
- }
-
- header('Content-Type: application/vnd.ms-excel');
- header('Content-Disposition: attachment; filename="用戶.xls"');
- $writer = PHPExcel_IOFactory::createWriter($php_excel, 'Excel5');
- $writer->save('php://output');
官方示例文檔中有輸出爲 PDF 的示例程序:github
- // Change these values to select the Rendering library that you wish to use
- // and its directory location on your server
- //$rendererName = PHPExcel_Settings::PDF_RENDERER_TCPDF;
- $rendererName = PHPExcel_Settings::PDF_RENDERER_MPDF;
- //$rendererName = PHPExcel_Settings::PDF_RENDERER_DOMPDF;
- //$rendererLibrary = 'tcPDF5.9';
- $rendererLibrary = 'mPDF5.4';
- //$rendererLibrary = 'domPDF0.6.0beta3';
- $rendererLibraryPath = dirname(__FILE__).'/../../../libraries/PDF/' . $rendererLibrary;
-
-
- // ..........
-
-
- if (!PHPExcel_Settings::setPdfRenderer(
- $rendererName,
- $rendererLibraryPath
- )) {
- die(
- 'NOTICE: Please set the $rendererName and $rendererLibraryPath values' .
- '<br />' .
- 'at the top of this script as appropriate for your directory structure'
- );
- }
-
-
- // Redirect output to a client’s web browser (PDF)
- header('Content-Type: application/pdf');
- header('Content-Disposition: attachment;filename="01simple.pdf"');
- header('Cache-Control: max-age=0');
-
- $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');
- $objWriter->save('php://output');
使用這段代碼時須要引入PHP 版本的 PDF 庫,支持三個版本的:web
- //$rendererName = PHPExcel_Settings::PDF_RENDERER_TCPDF;
- $rendererName = PHPExcel_Settings::PDF_RENDERER_MPDF;
- //$rendererName = PHPExcel_Settings::PDF_RENDERER_DOMPDF;
即 TCPDF, MPDF,DOMPDF,官方網址分別是:app
http://www.tcpdf.org/
http://www.mpdf1.com/mpdf/
https://github.com/dompdf/dompdfdom
推薦使用TCPDF,下載後複製到項目中,而後代碼中 $rendererLibraryPath 改成對應的路徑,而後就能夠正常輸出 PDF 文檔了。tcp
對於網上不少用戶反映的 PDF 中文亂碼問題,解決方法以下:post
- 全部程序及文檔所有使用 UTF-8 編碼
- 在 tcpdf_autoconfig.php 中設置中文字庫。