Laravel-下载PhpSpreadsheet而不先保存

时间:2018-07-19 19:47:29

标签: laravel laravel-5 phpspreadsheet laravel-response

我已经找到了下载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);

但是,如何与PhpSpreadsheet一起使用它,以便我可以不保存而下载它?有办法实现吗?

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

0 个答案:

没有答案