简体   繁体   中英

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. It works perfectly in Firefox, but in Internet Explorer (8), when it attempts to open the file, it breaks with:

Internet Explorer cannot download generate.php from my.domain.

Internet Explorer was not able to open this Internet site. 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:

// 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. As such, this is an identical problem to several similar SO questions. 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. If you go the email route, PHPMailer has a fairly easy way of sending attachments. 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.

It all comes down to IE's handling of the headers you set. Microsoft's own answer to this known problem should resolve things for you.

Summary of their solution: "remove the no-cache header or headers."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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