我一直在寻找官方文档,但我似乎无法把所有的东西放在我的脑海里(我是编程新手).我设法使用excel库并且它工作超级,但现在我想让用户有机会在.xls或.pdf中下载文件.
我正在使用Codeigniter和WAMP.
我下载了PHPExcel,它在我的C:目录下.
后来我将Classes:PHPExcel(文件夹)和PHPExcel.php复制到我的codeigniter的第三方文件夹中.
然后,在Codeigniter的Application,Libraries中,我创建了一个名为pdf.php的文件,并在那里复制了原始PHPExcel文档中的代码(21pdf.php)
pdf.php
/** PHPExcel_IOFactory */ require_once dirname(__FILE__) . '/../Classes/PHPExcel/IOFactory.php'; $rendererName = PHPExcel_Settings::PDF_RENDERER_TCPDF; $rendererLibrary = 'tcPDF5.9'; $rendererLibraryPath = '/php/libraries/PDF/' . $rendererLibrary; if (!PHPExcel_Settings::setPdfRenderer( $rendererName, $rendererLibraryPath )) { die( 'NOTICE: Please set the $rendererName and $rendererLibraryPath values' . EOL . 'at the top of this script as appropriate for your directory structure' ); }
我真的不知道如何配置它,我的脑子现在被烧了.任何帮助将非常感谢!
好吧,所以现在我有这个东西工作,带我好几天但终于实现了它.
为了在Codeigniter中配置phpexcel以生成pdf报告,我必须:
1).下载PHPExcel,并将文件夹"Classes"中的内容复制到我的框架Codeigniter中的另一个文件夹(application/third_party).在Classes文件夹中,您将找到另一个名为"PHPExcel"的文件夹和一个名称相同的.php文件.
2).然后,在application/libraries中,我用类构造函数创建了一个excel.php文件:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); /* * ======================================= * Author : Muhammad Surya Ikhsanudin * License : Protected * Email : mutofiyah@gmail.com * * Dilarang merubah, mengganti dan mendistribusikan * ulang tanpa sepengetahuan Author * ======================================= */ require_once APPPATH."/third_party/PHPExcel.php"; class Excel extends PHPExcel { public function __construct() { parent::__construct(); } }
3).接下来转到pdf.首先,您应该选择要使用的库.我选择使用dompdf https://github.com/dompdf/dompdf并在应用程序/库中安装它
4).创建了一个文件名pdf.php(把它放在你想要的地方)并在其中我把它(基于官方phpexcel文档中给出的例子:21pdf.php和01simple-download-pdf.php,你可以在测试中找到PHPExcel中的文件夹):
$rendererName = PHPExcel_Settings::PDF_RENDERER_DOMPDF; $rendererLibrary = 'dompdf_0-6-0_beta3'; $rendererLibraryPath = dirname(__FILE__).'/'. $rendererLibrary;
'dompdf_0-6-0_beta3'是包含用于呈现为pdf的库的文件夹的名称
dirname(FILE).'/'.$ rendererLibrary是查找此"dompdf_0-6-0_beta3"文件夹的路径
5).在我的admin.php中构建方法来调用此函数.我首先要做的是构建excel表并放置一些数据.之后,我使用布尔值来查看用户是否想要.xls或.pdf.如果他想要.pdf然后生成带有该扩展名的报告的代码,那么代码是这样的:
require_once (realpath(dirname(dirname(dirname(__FILE__))))."/libraries/pdf.php"); $filename=$rDate.'_Reporte_Usuarios_front'.'.pdf'; header('Content-Type: application/pdf'); header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name header('Cache-Control: max-age=0'); //no cache // $objWriter = new PHPExcel_Writer_PDF($objPHPExcel); $objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'PDF'); $objWriter->setSheetIndex(0); $objWriter->save('php://output');
当我的错误是无法找到渲染库时,我发布了这个问题.这就是我试图使用phpexcel包装而不是propper库的原因.所以在那之后不得不与路径进行一点点战斗,但后来做到了.
在修复之后,出现了另一个错误:'无法在DomPDF中加载PDF渲染库'(来自PHPExcel).所以我发现这一行:
$pdfRendererClassFile = PHPExcel_Settings::getPdfRendererPath() . '/dompdf/dompdf_config.inc.php';
我错过了包含dompdf_config.inc.php的文件夹,因为完整路径是:
应用程序/库/ dompdf_0-6-0_beta3/DOMPDF /东东
所以刚刚添加'dompdf'并完成了:)