Skip to content

Instantly share code, notes, and snippets.

@vielhuber
Last active September 20, 2023 11:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vielhuber/c7a9482c4acaaa3896a9 to your computer and use it in GitHub Desktop.
Save vielhuber/c7a9482c4acaaa3896a9 to your computer and use it in GitHub Desktop.
PHPExcel output stream directly to browser #php
<?php
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="' . $filename . '.xlsx"');
$objWriter->save('php://output');
die();
@Naren-hybreeder
Copy link

Is this correct PHPExcel is no longer supported and won't work correctly with the latest level of PHP?

@vielhuber
Copy link
Author

vielhuber commented Jul 1, 2020

Yes, that's correct. However, you can achieve the same with the successor PhpSpreadsheet:

<?php
use PhpOffice\PhpSpreadsheet\IOFactory;

$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="' . $filename . '.xlsx"');
$writer->save('php://output');
die();

@snickwhy
Copy link

I have problem same.
using PHPExcel in ajax
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="' . $filename . '.xlsx"');
$objWriter->save('php://output');

not output file excel??

but using save in local -> output file excel
$objWriter->save(' . $filename . '.xlsx);

help me. solusion give me

@vielhuber
Copy link
Author

Can you please provide a fully working example (including data)?
Also please use PhpSpreadsheet instead of PhpExcel.

@ajpgtech
Copy link

ajpgtech commented Jul 8, 2021

If you want to display the content on the browser (Note, it seems to be showing the first sheet only)

  $writer = IOFactory::createWriter($spreadsheet, 'Html');
  $message = $writer->save('php://output');

@justinfurnas
Copy link

In using this method, I am having issues. I am trying to send the information directly as a download to the browser, using this code:

header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); header("Content-Disposition: attachment; filename=\"".$filename."\""); $writer->save('php://output');

It writes the file to the browser, except it's filled with special encoded characters, and not the actual file when opened. If I don't push to the browser and instead just save the file to a .xlsx in my temp directory, I can open the document correctly.

I even tried pointing to the file using readfile('/tmp/filename.xlsx'); and it opens with special characters and no cells or data.

@vielhuber
Copy link
Author

Can you please provide a working example to reproduce this issue? I will then have a look at it.

@ajpgtech
Copy link

@justinfurnas your issue belongs more on a support forum than here. Having said that, check that your filename is correctly names with an extension xlsx. Then the browser will know what to do with it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment