Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example on how to download an Excel file created with PhpSpreadsheet
<?php
$streamedResponse = new StreamedResponse();
$streamedResponse->setCallback(function () {
$spreadsheet = //create you spreadsheet here;
$writer = new Xlsx($spreadsheet);
$writer->save('php://output');
});
$streamedResponse->setStatusCode(Response::HTTP_OK);
$streamedResponse->headers->set('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
$streamedResponse->headers->set('Content-Disposition', 'attachment; filename="your_file.xlsx"');
return $streamedResponse->send();`
@andykillen

This comment has been minimized.

Copy link

andykillen commented Jul 15, 2019

this really need to have the use statements on this, otherwise its not a solution, only further problems that need to be worked out

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\HttpFoundation\Response;
@damijanc

This comment has been minimized.

Copy link
Owner Author

damijanc commented Jul 15, 2019

It was an excerpt from the code, but thank you for pointing it out. I hope that no one will just blindly copy and paste it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.