开发者

PHP Excel Output for IE gives empty file

I am trying to generate an excel file with php. This works fine in Firefox but with IE I am only getting an empty file. This is what I am doing basically:

header('Content-type: application/ms-excel');
header('Content-Disposition:  inline; attachment; filename='.$filename);
echo $data;

I have tried various header settings but no success. I have also tried to output the content as a text file, same result here. No content in IE.

Any ideas?

header("Cache-Control: no-stor,no-cache,must-revalidate");
header("Cache-Control: post-check=0,pre-check=0", false);
header("Cache-control: private");
header("Content-Type: application/octet-stream");
header('Content-Disposition:  inline; attachment; filename='.$filename);
header("Content-Transfer-Encoding: binary");
header("Pragma: no-cache");
header("Expires: 0");

--

header('Pragma: public'); 
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
h开发者_如何转开发eader('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT'); 
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: pre-check=0, post-check=0, max-age=0');
header ("Pragma: no-cache"); 
header("Expires: 0"); 
header('Content-Transfer-Encoding: none'); 
header('Content-Type: application/vnd.ms-excel;');
header("Content-type: application/x-msexcel");
header('Content-Disposition:  inline; attachment; filename='.$filename);

Just two Options, also tried some other combinations.

Thanks, Roland


Had the same problem: IE times out after some short time if it doesn't get a response, even if the TCP connection is still open. What helped me was this: disable output buffering, send headers and flush them out. Send data when you have them. This was enough to keep the connection open.

Something like this:

// reset all output buffering
while (ob_get_level() > 0) {
    ob_end_clean();
}
header('Content-type: application/ms-excel');
header('Content-Disposition:  inline; attachment; filename='.$filename);

// we can't send any more headers after this
flush();

$excel = new PhpExcel();
$excel->setActiveSheetIndex(0);
$sheet = $excel->getActiveSheet();
// in this example, $data was an array in the format row => value
//  data structure is not relevant to issue
foreach ($data as $key => $value) {
     // add data to sheet here
     $sheet->SetCellValue('A' . $key, $value);
     // etc...
}
$writer = new PHPExcel_Writer($excel);
// push to browser
$writer->save('php://output');


Set your header to header("Content-Type: application/octet-stream"); to force a download.

or another version of your header is adding vnd so application/vnd.ms-excel but if you send this header then you should also send your current header along side it as some browsers vary.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜