PHP 使用 PHPExcel 库生成 Excel 文件
PHPExcel 是用来操作Office Excel 文档的一个PHP类库,它基于微软的OpenXML标准和PHP语言。可以使用它来读取、写入不同格式的电子表格,如 Excel (BIFF) .xls, Excel 2007 (OfficeOpenXML) .xlsx, CSV, Libre/OpenOffice Calc .ods, Gnumeric, PDF, HTML等等
调用代码示例:
- $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 的示例程序:
- // 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 库,支持三个版本的:
- //$rendererName = PHPExcel_Settings::PDF_RENDERER_TCPDF;
- $rendererName = PHPExcel_Settings::PDF_RENDERER_MPDF;
- //$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 中文乱码问题,解决方法如下:
- 所有程序及文档全部使用 UTF-8 编码
- 在 tcpdf_autoconfig.php 中设置中文字库。
PHP 使用 PHPExcel 库生成 Excel 文件的更多相关文章
- php用PHPExcel库生成Excel文档的例子
<?php require_once '../libs/PHPWord/PHPWord.php'; require_once '../libs/PHPWord/PHPWord/IOFactory ...
- thinkphp整合系列之phpexcel生成生成excel文件
在后台管理中会经常需要将数据生成excel表格的: php生成excel有两种方案: 一种是通过phpexcel生成xls格式的表格文件: 另一种则直接通过逗号换行生成csv格式的表格文件: 这里先讲 ...
- php生成excel文件的简单方法
生成excel文件,最简单的莫过于把数据库的数据导入到excel就行了. 生成excel 当然使用的是 phpExcel http://www.jbxue.com/tags/phpexcel.html ...
- C# 使用 NPOI 库读写 Excel 文件
NPOI 是开源的 POI 项目的.NET版,可以用来读写Excel,Word,PPT文件.在处理Excel文件上,NPOI 可以同时兼容 xls 和 xlsx.官网提供了一份 Examples,给出 ...
- 如何生成excel文件作为图像识别结果
如何生成excel文件作为图像识别结果 在进行大规模图像处理的时候,如果能够以表格的形式生成结果文件,将非常的直观.这个时候,选择excel作为结果输出文件,将是合适的. 查询相关资料,有很多关于ex ...
- XLSTransformer生成excel文件简单演示样例
项目结构图: 项目中所用到的jar,能够到http://www.findjar.com/index.x下载 ExcelUtil类源代码: package util; import java.io.IO ...
- XLSTransformer生成excel文件
jxls的使用方法: 1)声明一个XLSTransformer对象,生成方式就是使用new操作符 XLSTransformer transformer = new XL ...
- 实现excel导入导出功能,excel导入数据到页面中,页面数据导出生成excel文件
今天接到项目中的一个功能,要实现excel的导入,导出功能.这个看起来思路比较清楚,但是做起了就遇到了不少问题. 不过核心的问题,大家也不会遇到了.每个项目前台页面,以及数据填充方式都不一样,不过大多 ...
- 使用phpexcel类读写excel文件
使用原生php读写excel文件的博文地址: 基于使用原生php读写excel文件的不靠谱,本文将简单介绍如何使用第三方类库phpexcel来读写excel文件. 首先,需要到githut下载phpe ...
随机推荐
- Robot Framework安装部署详细教程
(转自“义甬君”) Robot Framework安装准备 说实话,在我玩了这么多自动化工具后,感觉Robot Framework所需的环境和安装过程是相对比较繁琐和复杂的.要真正搭建一套可以使用的R ...
- 双系统(win10+ubuntu)卸载Ubuntu系统
之前装的双系统,Win10 和Ubuntu ,系统引导使用的是Ubuntu的Grup的引导, 直接删除Ubuntu会导致引导丢失,会很麻烦,win10直接会挂掉,后期恢复需要重建引导 安全删除思路,先 ...
- 练习4-python+selenium+pandas
最近对于python的第三方库pandas比较有兴趣,在学习的过程中也简单的结合selenium做了一个简单的小工具 最新公司用一个外部系统来记录,追踪BUG,可是这个系统并不是专业的BUG管理系统, ...
- 【ABAP系列】SAP ABAP WS_DELIVERY_UPDATE 修改数量、过账日期并发货过账
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP WS_DELI ...
- VMware虚拟机上运行Manjaro系统
Manjaro系统是从ArchLinux系统发展而来.它的软件安装工具不是ubuntu的apt-get,不是yum,而是pacman. 在虚拟机安装好Manjaro后, 安装虚拟机工具VM-Tools ...
- 腾讯视频的手机端的地址和PC端的地址是不一样的
腾讯视频的手机端的地址和PC端的地址是不一样的,所以使用iframe的时候记得要使用手机端的地址
- Web高级 JavaScript中的数据结构
复杂度分析 大O复杂度表示法 常见的有O(1), O(n), O(logn), O(nlogn) 时间复杂度除了大O表示法外,还有以下情况 最好情况时间复杂度 最坏情况时间复杂度 平均情况时间复杂度 ...
- lesson1-图的概念和图论模型
说明: 图论专题开设的目的主要是作为本学期复习巩固和分享自己对于图论的理解,主要参考的是老师的PPT.应老师要求,不能共享文件,抱歉! 参考书目:[1] J.A. Bondy, U.S.R. Mur ...
- JAVA总结--java数据类型
一.String 1.String定义是指向堆内存中的引用:String的赋值本身是引用对象的切换,切换前后的对象依然存在:源码为:private final char value[]: 2.对多个S ...
- Vue.js状态管理模式 Vuex
vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化. 安装.使用 vuex 首先我们在 vue. ...