problem with downloading excel from php://output

Topics: Developer Forum, User Forum
Oct 12, 2012 at 1:56 PM
Edited Oct 12, 2012 at 2:52 PM

Hi gyus. I am writing with writer an excel5 sheet which queries a database. I cant get the file though even if the file appears to be written to excel from php.

 

 $excel = new PHPExcel();
 $excel->setActiveSheetIndex(0);
   $worksheet = $excel->getActiveSheet();
 while($rows=@mysqli_fetch_array($result,MYSQLI_NUM)){
                   $r[]=$rows;
               }
                     for($col=0; $col<$a2; $col++){
                    for($row=1; $row<count($r);$row++){
$worksheet->setCellValueByColumnAndRow($col,$row,$r[$row][$col]); }} header(\"Content-Type: application/vnd.ms-excel\"); header(\"Content-Disposition: attachment; filename=$legit\"); $objWriter = PHPExcel_IOFactory::createWriter($excel, 'Excel5'); ob_end_clean(); $objWriter->save('php://output'); $excel->disconnectWorksheets(); unset($excel); } ob_end_flush();?>

 

from the client i send a request with headers 

'Accept' :'application/vnd.ms-excel',//'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
                'Accept-Encoding':'gzip, deflate'

 

the response seems fine with content length written and all my response headers correctly returned but i dont see a prompt for downloading the file. I trimmed the whitespace from the start of the file.Mind the backslashes before the quotes in the code;its a file written dynamically from php as a string its not faulty.

Yeah and php version is 5.3.8 and PHPExcel 1.7.4 or 1.7.7 anyway the one without the charts support. That code used to work i dont know what happened now.

Coordinator
Oct 13, 2012 at 4:35 PM

Headers should not be:

        header(\"Content-Type: application/vnd.ms-excel\");
        header(\"Content-Disposition: attachment; filename=$legit\");
but

        header("Content-Type: application/vnd.ms-excel");
        header("Content-Disposition: attachment; filename=$legit");

Look at 01simple-download-xls.php in /Tests for an example

Oct 13, 2012 at 6:57 PM

Thanks for replying. This code is part of a fwrite calling parameter hence the backslashes (It is a string, i forgot to leave the slashes off when pasting the code here). Adding an exit call as the tests file shows doesnt work for me .Do we have to explicity set metadata like creator, etc? The request and response are working fine and i can see them in firebug but i wont get a prompt for downloading. The response is binary. I can see in my mysql log that the queries to the db have been executed correctly. I am working on linux and tried with oasis format to set the headers in client and server in case phpexcel wont render a correct file because no ms excel is available but that doesnt work either. Any other advice Mark?

Coordinator
Oct 13, 2012 at 8:11 PM

This isn't an issue with PHPExcel: but something with the headers... Metadata in the generated file is completely optional; and PHPExcel has no external dependencies - it's deliberately designed to run without MS Excel so that it can be used on any platform

Test by writing the output to a file on disk rather than php://output, and open it normally to verify the state of the file that is being generated.

Oct 14, 2012 at 9:22 AM
Edited Oct 14, 2012 at 10:02 AM

Yes you are totally correct. It is written in the file. And congrats for such a well written and documented project. I thought it was the filename the header problem  but writing 'simple.xls' doesn't solve it. I still cant get a prompt for downloading. It appears this is my last bug, i have a deadline till Tuesday night.

I think i am gonna just send the saved file to the client, thanks.

Oct 14, 2012 at 2:43 PM
Edited Oct 14, 2012 at 2:45 PM

I saw your answer in

http://stackoverflow.com/questions/5999939/how-to-download-excel-file-in-internet-explorer-browser-with-phpexcel-library

and i am using ssl, does this have anything to do with my problem? I can't get a prompt for downloading with readfile()  either. What a mess! Oh and Mark sorry for disturbing you.

 

$objWriter->save($path);
$excel->disconnectWorksheets(); 
unset($excel);    
    if (file_exists($path)) {
       header('Content-Description: File Transfer');
           header('Content-Transfer-Encoding: binary');
 header('Content-Disposition: attachment;filename=$legit');
  header('Content-Type: application/vnd.ms-excel;charset=UTF-8;');
 header('Content-Length: ' . filesize($path));
  header('Pragma: no-cache');
  header('Expires: 0');
  ob_clean();
    flush();
  @readfile($path);
  exit;
}

I can upload a screenshot of firebug's console if you like to see that everything is done right.