繁体   English   中英

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

[英]PHPExcel: Automatically download and open 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);

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

谢谢您的帮助,

多纳托

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

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

参考 - PHP Excel Reader

如需更多选项,您还可以查看备忘单 - 备忘

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

编辑

做这样的事情 -

$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');

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

当然,如果客户端没有安装MS Excel,则无法在MS Excel中打开; 虽然它仍然会提示保存。

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

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

在创建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');
header("Location: ".URL . "MODEL/case" . $caseNO . ".xlsx");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM