Guys, tell me, please.

I work in PHPExcel and just can’t save the file in the browser. Those. I process the file in php I have

<a href="Load.php" class="button24">Сформировать отчет</a> 

But the file load.php (Took from the example PHPExcel)

 <?php // Redirect output to a client's web browser (Excel2007) header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); header('Content-Disposition: attachment;filename="simple.xlsx"'); header('Cache-Control: max-age=0'); // If you're serving to IE 9, then the following may be needed header('Cache-Control: max-age=1'); // If you're serving to IE over SSL, then the following may be needed header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past header ('Last-Modified: '.gmdate('D, d MYH:i:s').' GMT'); // always modified header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1 header ('Pragma: public'); // HTTP/1.0 $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); $objWriter->save('php://output'); exit; 

The file is called simple.xlsx, the one that needs to be downloaded via the browser.
It is formed normally, etc.
When you try to run - displays hieroglyphs of the type

 PK zM!  d   [Content_Types].xml͖_O 0   K_ V@E4 D D   c [    ޻jL    5 z   I;o   qQC  D   b! ] i$L  3     2n   oT  :Z  h    [  4 ი  ]  yN ,ە  > >  j -' V ) #  EF^6  n   8q"K  H  >_ׄ    eƏ <⇄ Ud v   T PK zMY  %$xl/_rels/workbook.xml.rels  Kn 0  }Oa;   *&  R -=  C    mԆH   ʚA  Ӱ   9        RU l8 O  c  D $r   !  ?c/  c n4ąHá v| Ԕ-  DjD  J ºR7t I4H 8N  g@~ I }  b ?٪  U 6  7FPc   I!t  ù \    % [w/  s    EW   Ջ   71o 0 %1J L h/   7   lc&  Y 0       l| 

Can you please tell me how to save a file through a browser in PHPExcel? If there is such an opportunity, still tell me, how can it be made immediately and to print by another button?

  • I hope you know that phpexcel has long been unsupported and outdated. And that on the project’s home page there is a link to the current version? - n.osennij 5:48
  • n.osennij Yes, I know it perfectly, thank you, but your comment does not help to solve the problem. - Denis xyz
  • and where is the code of how you give the file to the user? - n.osennij 5:59
  • That's what I have problems with. See, I save the file on the server $ objWriter = new PHPExcel_Writer_Excel2007 ($ Excel); $ objWriter-> save ('simple.xlsx'); In the directory that I registered in open_basedir in php.ini. The file itself is saved, everything is ok. But to bring to download via the browser does not work. Do not tell me what could be the problem? - Denis xyz
  • so how do you get it to download now? Tritely give out the html link <a> to the file with the download attribute - n.osennij

1 answer 1

Option 1. Try saving to the server first. Perhaps the file is "beating" at the time of download

Here is a piece of code from an old project where everything works.

 $objWriter = PHPExcel_IOFactory::createWriter($pExcel, 'Excel2007'); $objWriter->save('files/'.$_SESSION['session_username'] . date('His') .'simple.xlsx'); 

Option 2. Before saving, call ob_end_clean()

Option 3. The problem in the encoding. Save the file in UTF-8 with BOM or just UTF-8

  • On the server, it is formed and saved correctly, but it cannot be transferred to download from the browser. - Denis xyz
  • @Denisxyz updated the answer - sbaikov