Laravel is a popular PHP framework that is widely used by developers to build web applications. One of the useful features of Laravel is the ability to import and export Excel files. In this article, we will learn how to import and export Excel files in Laravel.
Importing Excel Files in Laravel
Importing data from Excel files is a common requirement in many web applications. Laravel provides an easy way to do this using the Laravel-Excel package. This package allows you to read Excel files and store the data in the database.
Let’s get started by installing the package. You can install the package using Composer by running the following command:
composer require maatwebsite/excel
After installing the package, you need to configure it in Laravel. Open the config/app.php file and add the following line to the providers array:
'providers' => [
// ...
Maatwebsite\Excel\ExcelServiceProvider::class,
],
Next, add the following line to the aliases array:
'aliases' => [
// ...
'Excel' => Maatwebsite\Excel\Facades\Excel::class,
],
Now we are ready to start importing Excel files. First, create a new route in the web.php file:
Route::get('/import-excel', 'ExcelController@importExcel');
This route maps to the importExcel method of a new controller named ExcelController. We will create this controller next.
Create a new controller using the following command:
php artisan make:controller ExcelController
Open the ExcelController.php file and add the following methods:
use App\Models\User;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;
use App\Http\Controllers\Controller;
class ExcelController extends Controller
public function importExcel()
return view('import-excel');
public function postImportExcel(Request $request)
Excel::import(new UsersImport, $request->file('excel_file'));
return redirect('/import-excel')->with('success', 'Excel file successfully imported!');
The importExcel method simply returns a view named import-excel.blade.php. This view will contain a form that allows the user to upload an Excel file.
The postImportExcel method is called when the form is submitted. It uses the Excel::import method to read the Excel file and store the data in the database. In this example, we are using an import class named UsersImport, which we will create next.
Before we create the import class, let’s create the import-excel.blade.php view:
<form action=" url('/import-excel') " method="POST" enctype="multipart/form-data">
csrf_field()
<div class="form-group">
<label for="excel_file">Select Excel File</label>
<input type="file" name="excel_file" class="form-control" required>
</div>
<button type="submit" class="btn btn-primary">Import Excel File</button>
</form>
This view contains a form that allows the user to upload an Excel file. When the form is submitted, it will call the postImportExcel method of the ExcelController.
Now we are ready to create the import class. Create a new file named UsersImport.php in the app/Imports directory:
namespace App\Imports;
use App\Models\User;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
class UsersImport implements ToCollection
public function collection(Collection $rows)
foreach ($rows as $row)
User::create([
'name' => $row[0],
'email' => $row[1],
]);
The UsersImport class implements the ToCollection interface, which requires a collection method to be defined. This method is called for each row in the Excel file. In this example, we are creating a new User model for each row in the Excel file and populating its name and email fields using the values from the Excel file.
That’s it! Now we can test the import by visiting the /import-excel URL in the browser and uploading an Excel file.
Exporting Excel Files in Laravel
Exporting data to Excel files is also a common requirement in many web applications. Laravel provides an easy way to do this using the Laravel-Excel package.
Let’s get started by adding a new route to the web.php file:
Route::get('/export-excel', 'ExcelController@exportExcel');
The exportExcel method of the ExcelController will be used to generate the Excel file. Add the following method to the ExcelController.php file:
use App\Exports\UsersExport;
use Maatwebsite\Excel\Facades\Excel;
class ExcelController extends Controller
public function exportExcel()
return Excel::download(new UsersExport, 'users.xlsx');
The exportExcel method uses the Excel::download method to generate the Excel file. In this example, we are using an export class named UsersExport, which we will create next.
Create a new file named UsersExport.php in the app/Exports directory:
namespace App\Exports;
use App\Models\User;
use Maatwebsite\Excel\Concerns\FromCollection;
class UsersExport implements FromCollection
public function collection()
return User::all();
The UsersExport class implements the FromCollection interface, which requires a collection method to be defined. This method should return the data that will be exported to Excel. In this example, we are returning all the User models from the database.
That’s it! Now we can test the export by visiting the /export-excel URL in the browser and downloading the generated Excel file.
Frequently Asked Questions
Q1: How do I import data from multiple sheets in an Excel file?
A: To import data from multiple sheets in an Excel file, you can create a separate import class for each sheet. Each import class should implement the WithMultipleSheets interface and define a sheets method that returns an array of sheet names and their corresponding import classes.
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
class MultiSheetUsersImport implements WithMultipleSheets
public function sheets(): array
return [
'Sheet1' => new Sheet1Import,
'Sheet2' => new Sheet2Import,
];
You can then pass the MultiSheetUsersImport class to the Excel::import method to import data from all the sheets:
Excel::import(new MultiSheetUsersImport, $request->file('excel_file'));
Q2: How do I export data to specific columns in an Excel file?
A: To export data to specific columns in an Excel file, you can create a separate export class and define a map method. This method should return an array that maps the database columns to the Excel columns.
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\Exportable;
class UsersExport implements FromQuery, WithMapping
use Exportable;
public function query()
return User::query();
public function map($user): array
return [
$user->name,
$user->email,
];
public function headings(): array
return [
'Name',
'Email',
];
In this example, we are exporting the name and email columns to the first and second columns in the Excel file, respectively.
The headings method is used to define the column headings in the Excel file.
Conclusion
Laravel-Excel is a powerful package that makes it easy to import and export Excel files in Laravel. In this article, we’ve learned how to import and export Excel files using Laravel-Excel, and we’ve also covered some frequently asked questions.
Whether you need to import data from Excel files, export data to Excel files, or both, Laravel-Excel has you covered.
Video Tutorial
Check out this video tutorial on how to import and export Excel files in Laravel: