views:

45

answers:

2

Hi all,

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");
header('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

A: 

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.

RobertPitt
Already tried this, but did not work.
Roland
what browser are you using! and where is $data coming from. such as db blob, file, some other stream?
RobertPitt
$data is the string containing the excesl data, posted to the script.$data = (string) urldecode($_POST['exceldata']);
Roland
A: 

Had the same problem: IE times out after some 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_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');
Piskvor
Can you post the settings as script? I have already tried so many variations.
Roland