php phpexcel下载

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8566196/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 05:00:33  来源:igfitidea点击:

phpexcel to download

phpdownloadexportphpexcelexport-to-excel

提问by Dvir Levy

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

你好,我是 phpexcel 的新手,我想知道是否有某种方式将我创建的 excel 发送到客户端下载而不将其保存在我的服务器上或在他下载后立即将其删除

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.

我正在尝试在页面上创建一个“导出按钮”,该页面将为用户提供一个带有他想要的我刚刚创建的 excel 的“弹出窗口”。

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

谢谢你

回答by hakre

Instead of saving it to a file, save it to php://output­Docs:

不是将其保存到文件,而是将其保存到php://outputDocs

$objWriter->save('php://output');

This will send it AS-IS to the browser.

这会将其按原样发送到浏览器。

You want to add some headers­Docsfirst, like it's common with file downloads, so the browser knows which type that file is and how it should be named (the filename):

你想添加一些-Docs第一,喜欢它的常见的有文件下载,因此浏览器知道哪些类型的文件,它应该如何命名(文件名):

// We'll be outputting an excel file
header('Content-type: application/vnd.ms-excel');

// It will be called file.xls
header('Content-Disposition: attachment; filename="file.xls"');

// Write file to the browser
$objWriter->save('php://output');

First do the headers, then the save. For the excel headers see as well the following question: Setting mime type for excel document.

首先做标题,然后保存。对于 excel 标题,请参见以下问题:Setting mime type for excel document

回答by matino

$excel = new PHPExcel();
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="your_name.xls"');
header('Cache-Control: max-age=0');

// Do your stuff here

$writer = PHPExcel_IOFactory::createWriter($excel, 'Excel5');

// This line will force the file to download
$writer->save('php://output');

回答by Rogerio de Moraes

FOR XLSX USE

对于 XLSX 使用

SET IN $xlsNamename from XLSX with extension. Example: $xlsName = 'teste.xlsx';

来自 XLSX 的SET IN $xlsName名称,带有扩展名。示例:$xlsName = 'teste.xlsx';

$objPHPExcel = new PHPExcel();

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$xlsName.'"');
header('Cache-Control: max-age=0');
$objWriter->save('php://output');

FOR XLS USE

XLS 使用

SET IN $xlsNamename from XLS with extension. Example: $xlsName = 'teste.xls';

SET IN $xlsName来自带有扩展名的 XLS 名称。示例:$xlsName = 'teste.xls';

$objPHPExcel = new PHPExcel();

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$xlsName.'"');
header('Cache-Control: max-age=0');
$objWriter->save('php://output');

回答by JoshStrange

Use this call

使用这个电话

$objWriter->save('php://output');

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.

要将 XLS 表输出到您所在的页面,只需确保您所在的页面没有其他回显、打印输出。

回答by Efren Gutierrez Sosa

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();or save('php://output');, This can be fixed with adding ob_start();at the beginning of the script and ob_end_clean();just before the readfile(); or save('php://output');.

下载的所有文件都以空行开头,在我的例子中是四个空行,这会产生问题。无论您使用readfile();save('php://output');,这都可以通过ob_start();在脚本开头和;ob_end_clean();之前添加来解决readfile()。或 save('php://output');

回答by harsimer

 header('Content-type: application/vnd.ms-excel');

 header('Content-Disposition: attachment; filename="file.xlsx"');

 header('Cache-Control: max-age=0');

 header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT');

 header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');

 header ('Cache-Control: cache, must-revalidate');

 header ('Pragma: public');

 $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

 $objWriter->save('php://output');