php PHPExcel:自动下载并打开Excel文件

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

PHPExcel: Automatically download and open an Excel file

phpphpexcel

提问by user1956370

i have managed to create and save an excel file:

我设法创建并保存了一个 excel 文件:

// Rename the file
$fileName = URL . "MODEL/case" . $caseNO . ".xlsx";

// Write the file
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, $fileType);
$objWriter->save($fileName);

I would like now PHPExcel run Excel automatically, open the file created and maximize it. Is it possible? Will this work even if Excel is already running?

我想现在 PHPExcel 自动运行 Excel,打开创建的文件并将其最大化。是否可以?即使 Excel 已经在运行,这也能工作吗?

Thank you for your help,

感谢您的帮助,

Donato

多纳托

回答by swapnesh

As per my above comment, you can only force to have a download option. For this you can set headers in this way -

根据我上面的评论,您只能强制有一个下载选项。为此,您可以通过这种方式设置标题 -

header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header("Content-Disposition: attachment;filename=\"filename.xlsx\"");
header("Cache-Control: max-age=0");

Reference - PHP Excel Reader

参考 - PHP Excel 阅读器

For more options you can also check the cheat sheet - Cheat Sheet

有关更多选项,您还可以查看备忘单 - 备忘

Although the best way to read here - Codeplex

虽然在这里阅读的最佳方式 - Codeplex

EDIT

编辑

Do something like this -

做这样的事情 -

$excel = new PHPExcel();
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
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 Mark Baker

PHPExcel can't run MS Excel on the client.... but you can download a file directly to the browser which will offer the client the options of saving it to disk or opening it directly in MS Excel (if they have MS Excel installed) by sending the appropriate http headers, and "saving" the file to php://output.

PHPExcel 无法在客户端上运行 MS Excel.... 但您可以将文件直接下载到浏览器,该浏览器将为客户端提供将其保存到磁盘或直接在 MS Excel 中打开它的选项(如果他们安装了 MS Excel) ) 通过发送适当的 http 标头,并将文件“保存”到 php://output。

Of course, if the client doesn't have MS Excel installed, then opening in MS Excel isn't an option; although it will still prompt for save.

当然,如果客户端没有安装 MS Excel,那么在 MS Excel 中打开不是一个选项;虽然它仍然会提示保存。

The 01simple-download-xlsx.php file in the /Tests or /Examples directory does exactly this

/Tests 或 /Examples 目录中的 01simple-download-xlsx.php 文件正是这样做的

And "yes", it will work if MS Excel is already running on the client

并且“是”,如果 MS Excel 已经在客户端上运行,它将起作用

回答by HARDIK

Insert the following headers just before creating the Writer

在创建 Writer 之前插入以下标题

$filename = "filedetail". date("Y-m-d-H-i-s").".xlsx";
header('Content-Type: application/vnd.ms-excel'); 
header('Content-Disposition: attachment;filename="'.$filename.'"');
header('Cache-Control: max-age=0'); 
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');

回答by Abduhafiz

header("Location: ".URL . "MODEL/case" . $caseNO . ".xlsx");