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 中设置中文字库。

PHP 使用 PHPExcel 库生成 Excel 文件的更多相关文章

  1. php用PHPExcel库生成Excel文档的例子

    <?php require_once '../libs/PHPWord/PHPWord.php'; require_once '../libs/PHPWord/PHPWord/IOFactory ...

  2. thinkphp整合系列之phpexcel生成生成excel文件

    在后台管理中会经常需要将数据生成excel表格的: php生成excel有两种方案: 一种是通过phpexcel生成xls格式的表格文件: 另一种则直接通过逗号换行生成csv格式的表格文件: 这里先讲 ...

  3. php生成excel文件的简单方法

    生成excel文件,最简单的莫过于把数据库的数据导入到excel就行了. 生成excel 当然使用的是 phpExcel http://www.jbxue.com/tags/phpexcel.html ...

  4. C# 使用 NPOI 库读写 Excel 文件

    NPOI 是开源的 POI 项目的.NET版,可以用来读写Excel,Word,PPT文件.在处理Excel文件上,NPOI 可以同时兼容 xls 和 xlsx.官网提供了一份 Examples,给出 ...

  5. 如何生成excel文件作为图像识别结果

    如何生成excel文件作为图像识别结果 在进行大规模图像处理的时候,如果能够以表格的形式生成结果文件,将非常的直观.这个时候,选择excel作为结果输出文件,将是合适的. 查询相关资料,有很多关于ex ...

  6. XLSTransformer生成excel文件简单演示样例

    项目结构图: 项目中所用到的jar,能够到http://www.findjar.com/index.x下载 ExcelUtil类源代码: package util; import java.io.IO ...

  7. XLSTransformer生成excel文件

    jxls的使用方法: 1)声明一个XLSTransformer对象,生成方式就是使用new操作符                 XLSTransformer transformer = new XL ...

  8. 实现excel导入导出功能,excel导入数据到页面中,页面数据导出生成excel文件

    今天接到项目中的一个功能,要实现excel的导入,导出功能.这个看起来思路比较清楚,但是做起了就遇到了不少问题. 不过核心的问题,大家也不会遇到了.每个项目前台页面,以及数据填充方式都不一样,不过大多 ...

  9. 使用phpexcel类读写excel文件

    使用原生php读写excel文件的博文地址: 基于使用原生php读写excel文件的不靠谱,本文将简单介绍如何使用第三方类库phpexcel来读写excel文件. 首先,需要到githut下载phpe ...

随机推荐

  1. if you don't go after what you want, you'll never have it

    conquest.n. 征服 quantitative: adj. 数量的 cellar: n. 地窖 roast. v. 烧烤 allowance. n. 津贴 drainage. n. 排水 ma ...

  2. JS使用 popstate 事件监听物理返回键

    pushHistory();        window.addEventListener("popstate", function (e) {            if (or ...

  3. JavaScript文件中; !function (win, undefined) {}(window);的意义

    +function (){}-function (){}!function (){}~function (){}(function (){})() 这种写法可以保证匿名函数立即运行且运行一次 传入的 ...

  4. 20190908 On Java8 第十九章 类型信息

    第十九章 类型信息 RTTI(RunTime Type Information,运行时类型信息)能够在程序运行时发现和使用类型信息. Java 主要有两种方式在运行时识别对象和类信息: "传 ...

  5. python基础-5.1几种常见的排序算法

    一.冒泡排序(BubbleSort) 步骤: 比较相邻的元素,如果第一个比第二个大,就交换他们两个. 循环一遍后,最大的数就“浮”到了列表最后的位置. 将剩下的数再次循环,直道所有的排序完成 def ...

  6. mysql数据库监控工具-MONyog的配置和基本使用项

    测试数据传输前,研发要求需要监控10万,50万,100万数量级的数据在传输过程数据库服务器的资源消耗情况,因为数据传输服务是定时任务执行,配置10秒中一次,一次处理500条,处理完10万数据可能要半个 ...

  7. spring-第十六篇之AOP面向切面编程之Spring AOP

    1.上一篇介绍了AspectJ在AOP的简单应用,让我们了解到它的作用就是:开发者无需修改源代码,但又可以为这些组件的方法添加新的功能. AOP的实现可分为两类(根据AOP修改源码的时机划分): 1& ...

  8. HDFS基本概念

    概念 HDFS,它是一个文件系统,用于存储文件,通过目录树来定位文件:其次,它是分布式的,由很多服务器联合起来实现其功能,集群中的服务器有各自的角色. 注意:HDFS的设计适合一次写入,多次读出的场景 ...

  9. BZOJ 4552(二分+线段树+思维)

    题面 传送门 分析 此题是道好题! 首先要跳出思维定势,不是去想如何用数据结构去直接维护排序过程,而是尝试二分a[p]的值 设二分a[p]的值为x 我们将大于x的数标记为1,小于等于x的数标记为0 则 ...

  10. 动画可以暂停animation-play-state

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...