We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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');


85.5k
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;