phpexcel下载
你好,我是 phpexcel 的新手, 我想知道是否有某种方法将我创建的Excel发送给客户端下载而不将其保存在我的服务器上或在他下载后立即将其删除
我正在尝试在页面上创建一个“导出按钮”,该按钮将提供用户会看到一个“弹出窗口”,其中包含他想要我刚刚创建的 Excel。
现在,在我创建表后,我会这样做:
$objXLS->getActiveSheet()->getColumnDimension("A")->setAutoSize(true);
$objXLS->getActiveSheet()->getColumnDimension("B")->setAutoSize(true);
$objXLS->getActiveSheet()->setTitle('Test Stats');
$objXLS->setActiveSheetIndex(0);
$objWriter = PHPExcel_IOFactory::createWriter($objXLS, 'Excel5');
$objWriter->save(__DIR__."/test1.xls");
但这会将其保存到我的服务器,
谢谢
hello i am new to phpexcel,
and i was wondering if there is some way send the excel i have created to the clients download without saving it on my server or to delete it right after he downloads it
i am trying to create an "export button" on a page that will give the user a "pop-up" with the excel that he wants that i have just created.
now after i create the table i do :
$objXLS->getActiveSheet()->getColumnDimension("A")->setAutoSize(true);
$objXLS->getActiveSheet()->getColumnDimension("B")->setAutoSize(true);
$objXLS->getActiveSheet()->setTitle('Test Stats');
$objXLS->setActiveSheetIndex(0);
$objWriter = PHPExcel_IOFactory::createWriter($objXLS, 'Excel5');
$objWriter->save(__DIR__."/test1.xls");
but that saves it to my server
thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
不要将其保存到文件中,而是将其保存到
php://outputDocs
:
这会将其按原样发送到浏览器。
您想首先添加一些 headersDocs,就像文件下载中常见的那样,因此浏览器知道该文件是什么类型以及应如何命名(文件名):
首先处理标题,然后保存。对于 Excel 标头,另请参阅以下问题:为 Excel 文档设置 MIME 类型。
Instead of saving it to a file, save it to
php://output
Docs:This will send it AS-IS to the browser.
You want to add some headersDocs first, like it's common with file downloads, so the browser knows which type that file is and how it should be named (the filename):
First do the headers, then the save. For the excel headers see as well the following question: Setting mime type for excel document.
使用此调用
要将 XLS 工作表输出到您所在的页面,只需确保您所在的页面没有其他 echo、print、输出即可。
Use this call
To output the XLS sheet to the page you are on, just make sure that the page you are on has no other echo's,print's, outputs.
供 XLSX 使用
设置在带有扩展名的 XLSX 的 $xlsName 名称中。 示例:$xlsName = 'teste.xlsx';
用于 XLS 用途
SET IN $xlsName 来自带有扩展名的 XLS 名称。 示例:$xlsName = 'teste.xls';
FOR XLSX USE
SET IN $xlsName name from XLSX with extension. Example: $xlsName = 'teste.xlsx';
FOR XLS USE
SET IN $xlsName name from XLS with extension. Example: $xlsName = 'teste.xls';
可能你已经解决了你的问题,无论如何我希望这对你有帮助。
下载的所有文件都以空行开头,在我的情况下有四个空行
线,这会带来问题。无论您是否使用 readfile(); 或
save('php://output');
,这可以通过在以下位置添加ob_start();
来修复脚本的开头和
ob_end_clean();
就在readfile()
; 之前或者保存('php://output');
。posible you already solved your problem, any way i hope this help you.
all files downloaded starts with empty line, in my case where four empty
lines, and it make a problem. No matter if you work with
readfile();
orsave('php://output');
, This can be fixed with addingob_start();
at thebeginning of the script and
ob_end_clean();
just before thereadfile()
; orsave('php://output');
.我尝试了大多数答案提出的
$writer->save('php://output');
命令。但这恰好下载了损坏的文件。
为了解决这个问题,我必须这样做:
I tried the
$writer->save('php://output');
command proposed by most answers.But this happened to download a corrupted file.
To fix it, I had to do this:
需要指出的是,有时对于使用grocery crud v2.9 - v3 的codeigniter 用户来说,您可能会发现导出的excel 文件已损坏且无法打开。
您可能需要实际修改处理 GroceryCrud 中的 Excel 导出的代码部分(尽管不建议更改源代码)。特别是 ExportState.php 中的 exportToExcel() 函数,请在代码块之前和之后添加 ob_end_clean() ,如下突出显示:
obs_end_clean() 将清除输出,因此不会干扰您真正想要发送到浏览器的内容,这可能会导致损坏的文件问题。
希望它对您自己的实现(除了 GroceryCrud 之外)有所帮助。技巧是/或者可能是在标头之前和之后添加 obs_end_clean() 。
It is important to state that sometimes for the codeigniter folks using grocery crud v2.9 - v3, you may find that your exported excel files are corrupt and cannot be opened.
You may need to actually modify code sections handling your export to excel in GroceryCrud (even though not recommended to change source code). in particular the exportToExcel() function within ExportState.php, add ob_end_clean() before and after the code block as highlighted below:
The obs_end_clean() will clean out outputs and thus wont intefere with what you really want to send to browser which may cause the corrupt file issue.
Hope it helps in your own implementations apart from GroceryCrud. The trick is / or could be the adding of obs_end_clean() before and after your headers.