23

I'm using PhpSpreadsheet to generate an Excel file in Symfony 4. My code is:

$spreadsheet = $this->generateExcel($content);

$writer = new Xlsx($spreadsheet);
$filename = "myFile.xlsx";
$writer->save($filename); // LINE I WANT TO AVOID
$response = new BinaryFileResponse($filename);
$response->headers->set('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
$response->setContentDisposition(
    ResponseHeaderBag::DISPOSITION_ATTACHMENT,
    $filename
);

But I don't want to save the file and then read it to return to the user. I would like to download Excel content directly. Is there a way to do It?

I've searched how to generate a stream of the content (as this answer says) but I hadn't success.

Thanks in advance and sorry about my English

Genarito
  • 3,027
  • 5
  • 27
  • 53

2 Answers2

55

As I understand you are generating the content in your code. You can stream the response in Symfony and configure PhpSpreadsheet Writer to save to 'php://output' (see here the official doc Redirect output to a client's web browser).

Here is an working example using Symfony 4.1 and Phpspreadsheet 1.3:

<?php

namespace App\Controller;

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use PhpOffice\PhpSpreadsheet\Writer as Writer;

class TestController extends Controller
{


    /**
     * @Route("/save")
     */
    public function index()
    {

        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();
        $sheet->setCellValue('A1', 'Hello World !');

        $writer = new Writer\Xls($spreadsheet);

        $response =  new StreamedResponse(
            function () use ($writer) {
                $writer->save('php://output');
            }
        );
        $response->headers->set('Content-Type', 'application/vnd.ms-excel');
        $response->headers->set('Content-Disposition', 'attachment;filename="ExportScan.xls"');
        $response->headers->set('Cache-Control','max-age=0');
        return $response;
    }
}
Cid
  • 14,968
  • 4
  • 30
  • 45
Florin Celeapca
  • 732
  • 6
  • 9
  • 4
    It's worth mention that if you're calling `index()` via an API, then the file is returned as data in the response, and not as a direct download — at least that's what's happening for me. – Wayne Smallman Apr 11 '19 at 08:28
  • If you are download ing the file is it good idea to use php://output, is ot good for performance ? – famas23 Oct 26 '21 at 18:35
  • Your writing data before streaming, imagine you have huge sql data that should iterated ? – famas23 Feb 22 '23 at 14:24
3

This is the solution for Laravel. But it still uses Symfony\Component\HttpFoundation\StreamedResponse in the end.

        $contentDisposition = 'attachment; filename="' . $fileName . '"';
        $contentType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';

        $response = response()->streamDownload(function () use ($spreadsheet) {
            $writer = new Xlsx($spreadsheet);
            $writer->save('php://output');
        });
        $response->setStatusCode(200);
        $response->headers->set('Content-Type', $contentType);
        $response->headers->set('Content-Disposition', $contentDisposition);

Then you can send the response directly as a stream with

$response->send();

or just return it back in the controller

return $response;
Oleg Abrazhaev
  • 2,751
  • 2
  • 28
  • 41