简体   繁体   English

PHPExcel输出到浏览器在IE中不起作用

[英]PHPExcel output to browser doesn't work in IE

I have a PHP script that uses PHPExcel to open a template, insert values from a database query then return the result to the browser. 我有一个PHP脚本,它使用PHPExcel打开模板,从数据库查询中插入值,然后将结果返回给浏览器。 It works perfectly in Firefox, but in Internet Explorer (8), when it attempts to open the file, it breaks with: 它在Firefox中完美运行,但在Internet Explorer(8)中,当它试图打开文件时,它会断开:

Internet Explorer cannot download generate.php from my.domain. Internet Explorer无法从my.domain下载generate.php。

Internet Explorer was not able to open this Internet site. Internet Explorer无法打开此Internet站点。 The requested site is either unavailable or cannot be found. 请求的网站不可用或无法找到。 Please try again later. 请稍后再试。

The code that I'm using ( generate.php ) is as follows: 我正在使用的代码( generate.php )如下:

// Open template
$xlRead = PHPExcel_IOFactory::createReader('Excel5');
$xl = $xlRead->load('Template.xls');

$xl->setActiveSheetIndex(0);

// Write data
$xl->getActiveSheet()->fromArray($dbOutput, null, 'A1');

// Output to browser
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename=MyReport.xls');

$xlWrite = PHPExcel_IOFactory::createWriter($xl, 'Excel5');
$xlWrite->save('php://output')

EDIT Seems like this problem only affects IE when behind SSL. 编辑似乎此问题仅在SSL后面影响IE。 As such, this is an identical problem to several similar SO questions. 因此,这与几个类似的SO问题是相同的问题。 People's advice is to tweak the headers, but so far, no combination of what I've seen as solutions has worked... 人们的建议是调整标题,但到目前为止,我所看到的解决方案的组合并没有起作用......

If none of the above works for you, and you would still like to run under SSL, you could always write the file to disk and provide a download link, or you could prompt the user for an email address and email the file as an attachment. 如果以上都不适用于您,并且您仍希望在SSL下运行,则可以始终将文件写入磁盘并提供下载链接,或者您可以提示用户输入电子邮件地址并将该文件作为附件发送电子邮件。 If you go the email route, PHPMailer has a fairly easy way of sending attachments. 如果你去电子邮件路线,PHPMailer有一个相当简单的方式发送附件。 I'm not 100% for sure, but I think the file needs to be written to disk before it can be attached using PHPMailer, but you can always unlink the file immediately after send. 我不是100%肯定,但我认为在使用PHPMailer附加文件之前需要将文件写入磁盘,但是您可以在发送后立即取消链接文件。

It all comes down to IE's handling of the headers you set. 这一切都归结为IE对您设置的标题的处理。 Microsoft's own answer to this known problem should resolve things for you. 微软自己对这个已知问题的回答应该为你解决问题。

Summary of their solution: "remove the no-cache header or headers." 解决方案摘要:“删除无缓存标头或标头。”

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

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