How to Send data from Controllet to Import Multiple Sheet on Maatwebsite Laravel Excel?
To send data from a controller to Maatwebsite Laravel Excel for importing multiple sheets, follow these steps:
-
Install Maatwebsite Laravel Excel package: If you haven't installed the package yet, you can install it using Composer:
composer require maatwebsite/excel
-
Create a new controller for handling the import process:
php artisan make:controller ImportMultipleSheetsController
-
In the
ImportMultipleSheetsController
, create a method for handling the import process. This method will receive the request and return the imported data as a downloadable Excel file.namespace App\Http\Controllers; use Illuminate\Http\Request; use Maatwebsite\Excel\Facades\Excel; use App\Imports\MultipleSheetsImport; class ImportMultipleSheetsController extends Controller { public function import(Request $request) { $data = []; // Initialize an empty array to store the data // Process the request and prepare the data to be imported if ($request->hasFile('files')) { foreach ($request->file('files') as $file) { $sheetName = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME); $import = new MultipleSheetsImport($file, $sheetName); $data[$sheetName] = $import->toArray(); } // Return the imported data as a downloadable Excel file return Excel::create('multiple_sheets_import', function ($excel) use ($data) { foreach ($data as $sheetName => $sheetData) { $excel->sheet($sheetName, function ($sheet) use ($sheetData) { $sheet->fromArray($sheetData); }); } })->download('xlsx'); } return back()->withErrors(['Error' => 'Please select valid files to import']); } }
-
Create a new import class in the
app/Imports
directory:php artisan make:import MultipleSheetsImport --model=ModelName --format=xlsx
Replace
ModelName
with the name of the model that corresponds to the data in the Excel sheets. -
In the
MultipleSheetsImport
class, override themapping
method to define the column mappings:namespace App\Imports; use Maatwebsite\Excel\Concerns\ToModel; use Maatwebsite\Excel\Concerns\WithHeadings; use Maatwebsite\Excel\Concerns\WithMultipleSheets; use Maatwebsite\Excel\Rows\Row; class MultipleSheetsImport implements ToModel, WithHeadings, WithMultipleSheets { protected $model; public function __construct($file, $sheetName) { $this->file = $file; $this->sheetName = $sheetName; } public function model(Row $row) { return new ModelName([ 'column1' => $row->column1, 'column2' => $row->column2, // Add other columns as needed ]); } public function headings(): array { return [ 'Column1', 'Column2', // Add other columns as needed ]; } public function sheets(): array { return [ $this->sheetName => fn() => $this->file->sheets()->get($this->sheetName), ]; } }
-
Update the
routes/web.php
file to map theimport
method in theImportMultipleSheetsController
:Route::post('/import-multiple-sheets', 'ImportMultipleSheetsController@import')->name('import.multiple.sheets');
-
Create a form in the view file to handle file uploads and submit the request to the
import
method:@route('import-multiple-sheets') @csrf <form action="{{ route('import.multiple.sheets') }}" method="post" enctype="multipart/form-data"> @csrf <input type="file" name="files[]" multiple> <button type="submit">Import</button> </form>
Now, when you submit the form with multiple Excel files, the ImportMultipleSheetsController
will import the data from each sheet and return a downloadable Excel file containing all the imported data.