3

我找到了这种下载 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);
4

1 回答 1

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

return response()->download($path)->deleteFileAfterSend();
于 2022-02-22T16:22:03.060 回答