laravel 使用 maatwebsite 将 excel 导出并保存在目录中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43447033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:48:04  来源:igfitidea点击:

Export and save excel in a directory using maatwebsite

phplaravelmaatwebsite-excel

提问by Raghbendra Nayak

I am using maatwebsite (laravel)to write customer data into existing excel file and export dynamically from database and my code is working fine.

我正在使用maatwebsite (laravel)将客户数据写入现有的 excel 文件并从数据库动态导出,我的代码运行良好。

I am following below link:

我正在关注以下链接:

maatwebsite

网站

Now I want as excel file will download that should be save in any directory like /uploads/customers/text.xlsx

现在我想下载 excel 文件,该文件应该保存在任何目录中,例如 /uploads/customers/text.xlsx

Edit: See my code:

编辑:查看我的代码:

try {
    Excel::selectSheetsByIndex(1)->load('/public/uploads/new.xlsx', function($reader) {
    $reader->sheet('sheetname',function($sheet)
    {  
      $sheet->appendRow('test','test', 'test');
    });
 }, 'UTF-8')->export('xlsx');
} catch (Exception $ex) {
    echo $ex->getMessage().$ex->getLine();
    return response(Helpers::makeDefaultErrorAjaxResponse());
}

How can I do that using the maatwebsite function, I am not getting any function for doing this?

我如何使用 maatwebsite 功能来做到这一点,我没有得到任何这样做的功能?

回答by Mateusz

In Laravel Excel (Maatwebsite Excel 3) you use storemethod on Excelfacade (Maatwebsite\Excel\Facades\Excel):

在 Laravel Excel (Maatwebsite Excel 3) 中,您storeExcel门面 ( Maatwebsite\Excel\Facades\Excel)上使用方法:

Excel::store(new YourExport(), 'customers/fileName.xlsx', 'local');

The third parameter is a filesystem defined in config/filesystems.php(it's optional). If the location where you want to store those Excel files is outside the default localand publicfilesystems, you can add it to the config file:

第三个参数是在config/filesystems.php(可选)中定义的文件系统。如果要存储这些 Excel 文件的位置在默认localpublic文件系统之外,则可以将其添加到配置文件中:

'customer_uploads' => [
    'driver' => 'local',
    'root' => '/uploads/customers/',
],

And then save your Excel files with the thrid parameter set to that new filesystem:

然后使用设置为新文件系统的 thrid 参数保存 Excel 文件:

Excel::store(new YourExport(), 'fileName.xlsx', 'customer_uploads');

Read more here: https://docs.laravel-excel.com/3.1/exports/store.html

在此处阅读更多信息:https: //docs.laravel-excel.com/3.1/exports/store.html

回答by karmendra

You can use ->savefunction to save the generated file to a folder on server. For more details look at https://laravel-excel.maatwebsite.nl/docs/2.1/export/store#docs

您可以使用->save函数将生成的文件保存到服务器上的文件夹中。有关更多详细信息,请查看https://laravel-excel.maatwebsite.nl/docs/2.1/export/store#docs