Cover Image for Import Export to Excel and CSV using Maatwebsite in Laravel 5
357 views

Import Export to Excel and CSV using Maatwebsite in Laravel 5

Maatwebsite Excel is a popular Laravel package that allows you to easily import and export Excel and CSV files. It provides a convenient way to work with Excel and CSV files within your Laravel application. Here’s how you can use the Maatwebsite Excel package to handle importing and exporting in Laravel 5:

  1. Install the Maatwebsite Excel Package: Install the package using Composer:
Bash
 composer require maatwebsite/excel:^2.1
  1. Service Provider and Facade (Laravel 5.4 and below): For Laravel 5.5 and above, the package will automatically register. For versions 5.4 and below, add the following service provider and facade to your config/app.php file:
PHP
 'providers' => [
     // ...
     Maatwebsite\Excel\ExcelServiceProvider::class,
 ],

 'aliases' => [
     // ...
     'Excel' => Maatwebsite\Excel\Facades\Excel::class,
 ],
  1. Creating Export Classes: Create export classes that define the data to be exported. These classes should implement the FromCollection interface and return a collection of data to be exported.
PHP
 // app/Exports/UsersExport.php

 use Maatwebsite\Excel\Concerns\FromCollection;
 use App\User; // Assuming you have a User model

 class UsersExport implements FromCollection
 {
     public function collection()
     {
         return User::all();
     }
 }
  1. Creating Controllers for Export: Create a controller method to handle exporting. You can use the Excel facade to generate the Excel or CSV file.
PHP
 use Excel;
 use App\Exports\UsersExport;

 public function export()
 {
     return Excel::download(new UsersExport, 'users.xlsx');
 }
  1. Creating Import Classes: Similarly, you can create import classes that define how data should be imported from Excel or CSV files. These classes should implement the ToModel interface and return instances of your model.
PHP
 // app/Imports/UsersImport.php

 use Maatwebsite\Excel\Concerns\ToModel;
 use App\User; // Assuming you have a User model

 class UsersImport implements ToModel
 {
     public function model(array $row)
     {
         return new User([
             'name' => $row[0],
             'email' => $row[1],
             // ...
         ]);
     }
 }
  1. Creating Controllers for Import: Create a controller method to handle importing. You can use the Excel facade to process the uploaded file and import the data.
PHP
 use Excel;
 use App\Imports\UsersImport;

 public function import(Request $request)
 {
     $file = $request->file('file');
     Excel::import(new UsersImport, $file);
     return redirect()->back()->with('success', 'Data imported successfully.');
 }

Remember to create routes for your export and import methods in your web.php routes file.

This is a basic overview of how to use the Maatwebsite Excel package for importing and exporting in Laravel 5. Please refer to the official documentation for more detailed information and customization options: Maatwebsite Excel Documentation.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS