Export and save excel in a directory using maatwebsite

Raghbendra Nayak picture Raghbendra Nayak · Apr 17, 2017 · Viewed 9.8k times · Source

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

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

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?

Answer

Mateusz picture Mateusz · Dec 27, 2019

In Laravel Excel (Maatwebsite Excel 3) you use store method on Excel facade (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 local and public filesystems, you can add it to the config file:

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

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

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

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