3

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);
senty
  • 12,385
  • 28
  • 130
  • 260
  • 3
    Possible duplicate of [Download PhpSpreadsheet file without save it before](https://stackoverflow.com/questions/50993968/download-phpspreadsheet-file-without-save-it-before) – sulaiman sudirman Dec 05 '18 at 07:37

1 Answers1

0
$writer = new Xlsx($spreadsheet);
$writer->save($path = storage_path('hello world.xlsx'));

return response()->download($path)->deleteFileAfterSend();
Juukie14
  • 168
  • 1
  • 5