jeudi 19 juillet 2018

Laravel - download PhpSpreadsheet without saving it before

I have found this approach for downloading the csv.

\Response::macro('attachment', function ($content) {

    $headers = [
        'Content-type'        => 'text/csv',
        'Content-Disposition' => 'attachment; filename="download.csv"',
    ];

    return \Response::make($content, 200, $headers);

});


return response()->attachment($content);


However, how can I use it with PhpSpreadsheet so that I can download it without saving it? Is there a way to achieve that?

$header = array("Customer Number", "Customer Name", "Address", "City", "State", "Zip");
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->fromArray([$header], NULL, 'A1');

$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
$writer->save(storage_path('app/' . "abc.xlsx"));

return response()->attachment($writer);



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire