【问题标题】:Laravel: export Excel File in a given path?Laravel:在给定路径中导出 Excel 文件?
【发布时间】:2022-08-18 17:30:01
【问题描述】:

api

Route::get(\'UserExport\', function (UserRequest $request) {
    return Excel::download(new UserExportExcel($request->id), \'UserReport.xlsx\');
});

用户导出Excel

 private $id;

    public function __construct(int $id)
    {
        $this->id = $id;
    }


    public function collection()
    {
        $filePath = storage_path(\'app/Users/\'.$this->id . \'.xlsx\'); //file name is 15.xlsx which is the id +.xlsx
        User::all()->downloadExcel(
            $filePath,
        );
    }

我试图在不使用模型的情况下导出我的 excel 文件!

    标签: php laravel export export-to-excel maatwebsite-excel


    【解决方案1】:

    完成我直接从 API 使用此方法解决它(我指定我的 excel 文件的完整路径(路径+名称),并使用 Response::download 下载

    use Illuminate\Support\Facades\Response;
    
    Route::get('UserExport', function (UserRequest $request) {
        $filePath = storage_path('app/Users/' . $request->id . '.xlsx');
        return Response::download($filePath);
    });
    
    

    【讨论】:

    • 虽然这段代码 sn-p 可以解决问题,但它没有解释为什么或如何回答这个问题。请include an explanation for your code,因为这确实有助于提高您的帖子质量。请记住,您正在为将来的读者回答问题,而这些人可能不知道您的代码建议的原因。
    • 您好,谢谢您的留言,我会关注的!
    猜你喜欢
    • 2020-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 2019-05-20
    • 1970-01-01
    • 2015-01-21
    相关资源
    最近更新 更多