webman里面直接输出phpspeadsheet导出的文件到php://output

roczyl

目前我用phpspeadsheet导出excel文件,是存为文件然后再respon的。
有没有方法直接输出而不用输出实体文件?

944 1 5
1个回答

roczyl

找到一个方法,共享一下:

    $writer = new Xlsx($spreadsheet);
    $response = response();
    ob_start();
    $writer->save('php://output');
    $c = ob_get_contents();
    ob_flush();
    flush();
    $response->withHeaders([
        'Content-Type' => 'application/vnd.ms-excel',
        'Content-Disposition' => 'attachment;filename="xxx.xlsx"',
        'Cache-Control' => 'max-age=0',
    ])->withBody($c);
    return $response;
  • liziyu 2022-08-17

    谢谢分享,收藏了!~

年代过于久远,无法发表回答
🔝