【问题标题】:Laravel: How to store Excel File in a given path?Laravel:如何将 Excel 文件存储在给定的路径中?
【发布时间】:2020-11-18 23:33:48
【问题描述】:

我有一个功能,可以将我的刀片视图转换为 Excel 格式并下载它。现在我想将它保存在指定位置,即:public/uploads/release 这是我的控制器:

 public function export_release()
{
    return Excel::download(new ReleaseExportView(), 'release.xlsx');
}

此功能下载excel文件下载文件夹。我想保存在位置:public/uploads/release

这是我的路线:

Route::Get('outbounds/export_release' ,'outboundController@export_release')->name('outbounds.export_release');

【问题讨论】:

    标签: php html excel laravel export


    【解决方案1】:

    您可以使用store 方法将文件存储在文件系统中。

    来自docs

    导出可以轻松存储在 Laravel 支持的任何文件系统上。

    默认磁盘

    public function storeExcel() 
    {
        // Store on default disk
        Excel::store(new InvoicesExport(2018), 'invoices.xlsx');
    }
    

    自定义磁盘

    public function storeExcel() 
    {
        // Store on a different disk (e.g. s3)
        Excel::store(new InvoicesExport(2018), 'invoices.xlsx', 's3');
        
        // Store on a different disk with a defined writer type. 
        Excel::store(new InvoicesExport(2018), 'invoices.xlsx', 's3', Excel::XLSX);
    }
    

    现在理想情况下,您应该将文件存储在 storage/app/public/uploads/release 中,并按照 here 的说明创建从 public/storage/storage/app/public 的符号链接。

    但如果您真的想将其存储在public/uploads/release 中,您可以在config/filesystems.php 中创建新的自定义磁盘。比如:

    'real_public' => [
        'driver' => 'local',
        'root' => public_path(),
        'url' => env('APP_URL'),
        'visibility' => 'public',
    ],
    

    然后你可以使用自定义磁盘来存储文件:

    public function export_release()
    {
        Excel::store(new ReleaseExportView(), 'uploads/release/release.xlsx', 'real_public');
    }
    

    【讨论】:

    • 错误未定义类常量'XLSX'
    • 您导入了哪个 Excel 类?也许尝试不指定文件阅读器,我更新了我的答案。
    • 只需要包含这个:\Maatwebsite\Excel\Excel::XLSX
    【解决方案2】:

    您可以在config/filesystems.php中创建一个新的存储盘:

    'excel_uploads' => [
        'driver' => 'local',
        'root'   => public_path() . '/uploads/release',
    ],
    

    并像这样存储文件:

    Storage::disk('excel_uploads')->put($path, $file_content)
    

    【讨论】:

      猜你喜欢
      • 2022-08-18
      • 1970-01-01
      • 2018-06-19
      • 1970-01-01
      • 2023-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多