PHPExcel 是用来操作Office Excel 文档的一个PHP类库,它基于微软的OpenXML标准和PHP语言。可以使用它来读取、写入不同格式的电子表格,如 Excel (BIFF) .xls, Excel 2007 (OfficeOpenXML) .xlsx, CSV, Libre/OpenOffice Calc .ods, Gnumeric, PDF, HTML等等

调用代码示例:

  1. $php_excel = new PHPExcel();
  2. $properties = $php_excel->getProperties();
  3. $properties->setCreator("www.1024i.com");
  4. $properties->setLastModifiedBy("www.loubarnes.com");
  5. $properties->setTitle("PHP 生成 Excel");
  6. $properties->setSubject("PHP 生成 Excel");
  7. $properties->setDescription('PHP 生成 Excel');
  8. $properties->setKeywords("PHP 生成 Excel");
  9. $properties->setCategory("PHP 生成 Excel");
  10. $php_excel->setActiveSheetIndex(0);
  11. $active_sheet = $php_excel->getActiveSheet();
  12. $active_sheet->setTitle('用户');
  13. // 自动调节大小
  14. $active_sheet->getColumnDimension('A')->setWidth(8);
  15. $active_sheet->getColumnDimension('B')->setWidth(12);
  16. $active_sheet->getColumnDimension('C')->setWidth(8);
  17. $active_sheet->getColumnDimension('D')->setWidth(8);
  18. $active_sheet->getColumnDimension('E')->setWidth(24);
  19. $active_sheet->getColumnDimension('F')->setWidth(60);
  20. $active_sheet->setCellValue('A1', 'PHP 生成 Excel 示例' );
  21. $active_sheet->mergeCells('A1:F1'); // 合并表头单元格
  22. $active_sheet->getRowDimension(1)->setRowHeight(30); // 设置表头1高度
  23. $style = array(
  24. 'font' => array(
  25. 'size' => 20
  26. ),
  27. 'alignment' => array(
  28. 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
  29. ),
  30. 'borders' => array(
  31. 'bottom' => array(
  32. 'style' => PHPExcel_Style_Border::BORDER_THIN
  33. )
  34. )
  35. );
  36. $active_sheet->getStyle('A1:F1')->applyFromArray($style); // 设置表头1样式
  37. $active_sheet->getRowDimension(2)->setRowHeight(30); // 设置表头2高度
  38. // 设置表头2名称
  39. $active_sheet->setCellValue('A2', '编号');
  40. $active_sheet->setCellValue('B2', '名称');
  41. $active_sheet->setCellValue('C2', '性别');
  42. $active_sheet->setCellValue('D2', '年龄');
  43. $active_sheet->setCellValue('E2', '出生日期');
  44. $active_sheet->setCellValue('F2', '备注');
  45. // 表头(编号, 名称, 性别, 出生日期)样式
  46. $style = array(
  47. 'font' => array(
  48. 'bold' => true
  49. ),
  50. 'alignment' => array(
  51. 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
  52. ),
  53. 'borders' => array(
  54. 'bottom' => array(
  55. 'style' => PHPExcel_Style_Border::BORDER_THIN
  56. )
  57. )
  58. );
  59. $active_sheet->getStyle('A2:E2')->applyFromArray($style);
  60. // 表头(备注)样式
  61. $style = array(
  62. 'font' => array(
  63. 'bold' => true
  64. ),
  65. 'borders' => array(
  66. 'bottom' => array(
  67. 'style' => PHPExcel_Style_Border::BORDER_THIN
  68. )
  69. )
  70. );
  71. $active_sheet->getStyle('F2')->applyFromArray($style);
  72. // 内容样式
  73. $style = array(
  74. 'alignment' => array(
  75. 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
  76. )
  77. );
  78. $active_sheet->getStyle('A:E')->applyFromArray($style);
  79. $ids = post::_ints('id', array());
  80. $notes = post::_strings('note', array());
  81. $i = 3;
  82. if(count($ids))
  83. {
  84. foreach($ids as $id)
  85. {
  86. $note = $notes[$i-3];
  87. foreach($this->data as $data)
  88. {
  89. if($data['id']==$id)
  90. {
  91. $active_sheet->setCellValue('A'.$i, $id );
  92. $active_sheet->setCellValue('B'.$i, $data['name'] );
  93. $active_sheet->setCellValue('C'.$i, $data['male'] );
  94. $active_sheet->setCellValue('D'.$i, $data['age'] );
  95. $active_sheet->setCellValue('E'.$i, $data['birth_date'] );
  96. $active_sheet->setCellValue('F'.$i, $note );
  97. break;
  98. }
  99. }
  100. $i++;
  101. }
  102. }
  103. header('Content-Type: application/vnd.ms-excel');
  104. header('Content-Disposition: attachment; filename="用户.xls"');
  105. $writer = PHPExcel_IOFactory::createWriter($php_excel, 'Excel5');
  106. $writer->save('php://output');

官方示例文档中有输出为 PDF 的示例程序:

  1. // Change these values to select the Rendering library that you wish to use
  2. // and its directory location on your server
  3. //$rendererName = PHPExcel_Settings::PDF_RENDERER_TCPDF;
  4. $rendererName = PHPExcel_Settings::PDF_RENDERER_MPDF;
  5. //$rendererName = PHPExcel_Settings::PDF_RENDERER_DOMPDF;
  6. //$rendererLibrary = 'tcPDF5.9';
  7. $rendererLibrary = 'mPDF5.4';
  8. //$rendererLibrary = 'domPDF0.6.0beta3';
  9. $rendererLibraryPath = dirname(__FILE__).'/../../../libraries/PDF/' . $rendererLibrary;
  10. // ..........
  11. if (!PHPExcel_Settings::setPdfRenderer(
  12. $rendererName,
  13. $rendererLibraryPath
  14. )) {
  15. die(
  16. 'NOTICE: Please set the $rendererName and $rendererLibraryPath values' .
  17. '<br />' .
  18. 'at the top of this script as appropriate for your directory structure'
  19. );
  20. }
  21. // Redirect output to a client’s web browser (PDF)
  22. header('Content-Type: application/pdf');
  23. header('Content-Disposition: attachment;filename="01simple.pdf"');
  24. header('Cache-Control: max-age=0');
  25. $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');
  26. $objWriter->save('php://output');

使用这段代码时需要引入PHP 版本的 PDF 库,支持三个版本的:

  1. //$rendererName = PHPExcel_Settings::PDF_RENDERER_TCPDF;
  2. $rendererName = PHPExcel_Settings::PDF_RENDERER_MPDF;
  3. //$rendererName = PHPExcel_Settings::PDF_RENDERER_DOMPDF;

即 TCPDF, MPDF,DOMPDF,官方网址分别是:

http://www.tcpdf.org/ 
http://www.mpdf1.com/mpdf/ 
https://github.com/dompdf/dompdf

推荐使用TCPDF,下载后复制到项目中,然后代码中 $rendererLibraryPath 改为对应的路径,然后就可以正常输出 PDF 文档了。

对于网上很多用户反映的 PDF 中文乱码问题,解决方法如下:

    1. 所有程序及文档全部使用 UTF-8 编码
    2. 在 tcpdf_autoconfig.php 中设置中文字库。

最新文章

  1. Huffman树进行编码和译码
  2. js基础篇——原型与原型链的详细理解
  3. setTimeout,setInterval,process.nextTick,setImmediate in Nodejs
  4. 慕课网-安卓工程师初养成-4-4 Java条件语句之嵌套 if
  5. Android Fragment详解(一):概述
  6. AMQP(Advanced Message Queuing Protocol)
  7. 或许你不知道的10条SQL技巧(转自58沈剑原创)
  8. 吐槽下微软的vs code编辑器
  9. Memcached介绍
  10. vlog.hpp
  11. Vue 制作简易计算器
  12. msm audio platform 驱动代码跟踪
  13. 05_解决mac百度网盘下载速度慢问题
  14. C++ static 静态变量&amp;静态成员函数
  15. C和C指针小记(九)-指针用法1
  16. COPY ORCHARD GET 404: System.UnauthorizedAccessException: mappings.bin的访问被拒绝
  17. gitlab安装教程、gitlab官网、英文文档
  18. URL query string中文字符问题
  19. 用C++画光(三)&mdash;&mdash;色散
  20. 【SQL】- 基础知识梳理(五) - 触发器

热门文章

  1. rpm安装软件时提示warning: *.rpm: Header V3 RSA/SHA256 Signature, keykey ID c105b9de
  2. UI自动化之三种等待
  3. Application.Restore不起作用了
  4. JDK8以后接口是可以定义实现方法,必须需要default修饰符修饰
  5. boost的libboost_system问题
  6. 两台电脑使用ROS通讯
  7. Struts2异常:HTTP Status 404 - There is no Action mapped for action name addBook.
  8. java面向对象基础总结
  9. IDEA创建SpringBoot+maven项目
  10. [Codeforces 1199C]MP3(离散化+二分答案)