php PHPExcel 将文件保存在文件夹中或打开 excel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8077817/
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 03:55:01  来源:igfitidea点击:

PHPExcel save file in a folder or open excel

phpphpexcel

提问by Pre

I am using PHPExcel to export data from my PHP page to Excel. I am using Excel5.

我正在使用 PHPExcel 将数据从我的 PHP 页面导出到 Excel。我正在使用 Excel5。

I want the excel file to be be saved a particular folder that is specified in the code

我希望将 excel 文件保存在代码中指定的特定文件夹中

OR better still,

或者更好,

I want Excel to open with the data written in it so that user can save it wherever he wants. What should I do.

我希望 Excel 以写入其中的数据打开,以便用户可以将其保存在任何他想要的地方。我该怎么办。

Please guide me

请指导我

Pre

回答by Filipe Synthis

I solved this problem doing this:

我这样做解决了这个问题:

$objWriter->save(str_replace(__FILE__,'/path/to/save/filename.extension',__FILE__));

In my case, it worked!

就我而言,它奏效了!

回答by matino

This will ask the user to save / open the file:

这将要求用户保存/打开文件:

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

回答by Mark Baker

Look at 01simple-download-xls.php in the PHPExcel Tests directory. This sends the Excel file to the user's browser, which then prompts them to either display it (in Excel if they have it installed, or other spreadsheet program if they have the extension associated with LibreOffice Calc or Gnumeric or whatever), or save it to their local disk.

查看 PHPExcel Tests 目录中的 01simple-download-xls.php。这会将 Excel 文件发送到用户的浏览器,然后浏览器会提示他们显示它(如果他们安装了 Excel,则在 Excel 中,或者如果他们有与 LibreOffice Calc 或 Gnumeric 或其他相关联的扩展名的其他电子表格程序),或将其保存到他们的本地磁盘。

回答by Victor

This solved this issue for me:

这为我解决了这个问题:

$this->objWriter->save(str_replace(__FILE__,$_SERVER['DOCUMENT_ROOT'] .'/path/to/save/filename.xlsx',__FILE__));