Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Download excel files (xlsx) server-client

I have created excel files on my server with phpspreadsheet and I have sent them as attachements in a email message. Now, I am trying to save those excel files directly on clients. I have tried with this php script:

        $respuesta = new Response();
        $respuesta->setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        $respuesta->setHeader('Content-Disposition', "attachment; filename='" . $filename ."'");
        $respuesta->setHeader('Cache-Control: cache, must-revalidate');
        $writer->save('php://output');


80.4k
Accepted
answer

you probaby just need to die/exit after $writer->save... ? Here is how my code looks

           $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
            $writer->save('export.xlsx');
            header('Content-Type: application/vnd.ms-excel');
            header('Content-Disposition: attachment; filename="export.xlsx"');
            $writer->save("php://output");
            exit;
edited May '18

You're creating a new Response instance, why not use the one in the DI?

// controller action
public function someAction() {
    /** @var \PhpOffice\PhpSpreadsheet\Writer\Xlsx $writer */
    $this->request->setHeader('Content-Type: application/vnd.ms-excel');
    $this->request->setHeader('Content-Disposition: attachment; filename="export.xlsx"');
    $this->request->setHeader('Cache-Control: cache, must-revalidate');
    $writer->save('php://output');
    $this->request->send();
    exit;
}

Thank you. It is working smoothly.

you probaby just need to die/exit after $writer->save... ? Here is how my code looks

          $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
           $writer->save('export.xlsx');
           header('Content-Type: application/vnd.ms-excel');
           header('Content-Disposition: attachment; filename="export.xlsx"');
           $writer->save("php://output");
           exit;