Use Laravel Excel to Export and Import files.

Tram Ho


Laravel Excel is a simple but elegant wrapper around PhpSpreadsheet with the goal of simplifying the export and import of files.
And here is the PhpSpreadsheet for those of you who are wondering and wondering about it
PhpSpreadsheet is a library written in pure PHP and provides a set of classes that allow you to read and write to various spreadsheet file formats, such as Excel and LibreOffice Calc.

The functions of Laravel Excel

  • Easily export collections to Excel.
  • Export queries with automatic chunking for better performance.
  • Export queues for better performance.
  • Easily export Blade views to Excel.
  • Easy to enter collection.
  • Read Excel file in block.
  • Handling import inserts in batches.


To install Laravel Excel, you need to have the following:

  • PHP: 7.0
  • Laravel: 5.5
  • PhpSpreadsheet: 1.6
  • The PHP extensions are php_zip, php_xml, php_gd2 that are enabled

After you have met all the above requirements, you need to install this package into the composer.json file of the laravel project by opening the terminal and typing this command.

Now MaatwebsiteExcelExcelServiceProvider will automatically be registered for you. However, if you want to register yourself, you can add this to your config/app.php file



First you need to create an export class in the App/Export folder. Or you can use the following command to create automatically:

Now in the App/Exports folder, your file will be created. Specifically here I run the command php artisan make:export UsersExport --model=User so there will be a directory structure like this.

Now your UsersExport.php file will look like this.

Now, just call the export class created above return Excel::download(new UsersExport, 'users.xlsx'); to export your User model to users.xlsx file.
Finally, adding a path to access when you need to export is great. You can refer to the example below offline.

After doing the above, you just need to run the php artisan serve command and then go to your-local-host:8000/users/export to see and feel 😃


Similar to the time of export, in order to be able to import, you also need to create an import class in the App/Imports directory, and the creation by rice power or via the command php artisan make:import ImportName --model=ModelImportName , Everyone decide for themselves.
Once created, the directory structure will be like that.

Now open the file UsersImport.php just created above and add the code below to perform the creation of a new User with each row of data that we have imported.

Finally put in the controller to perform the import ✌️


Above are the basics to know to be able to export and import files in laravel. There are also many useful functions like export / import multiple sheets, export / import using queues, … but time and english are limited so I still have not been able to dig deeper.
Everyone can visit the document page of the package here to learn more.
Thanks for reading the whole article without throwing bricks 😃😃

Share the news now

Source : Viblo