Import and export excel files, csv with Laravel-Excel

In this article, I will give a brief overview of the Laravel-Excel package . Perhaps there is no need to explain much about its purpose, the title says everything. Basically, Laravel Excel has the power of PHPExcel , it includes features like: importing Excel, CSV to collection, exporting models, array's or views to Excel, importing multiple files, etc.

Some outstanding features of Laravel Excel

  • Import excel file, csv into Laravel Collections
  • Export Blade views to Excel and CSV with CSS styling
  • Import multiple files
  • Support caching
  • Support chunk and queues importer
  • Fix Excel file, csv
  • Many optional configuration settings in the config file
  • And many other features

Use Laravel Excel

1 - Installation

  • Install with composer
composer require maatwebsite/excel
  • After installation is complete, open the file config / app.php and add the following code.
'providers' => [

'aliases' => [
	'Excel' => Maatwebsite\Excel\Facades\Excel::class,
  • Public configuration settings:
php artisan vendor:publish

The above settings will add excel.php file to the config message.

2 - Import

Laravel Excel can import multiple files, xls, xlsx, CSV files, worksheets into Laravel collections.

  • Importing a file
Excel::load('file.xls', function($reader) {
    // reader methods
  • Import a folder
Excel::batch('folder', function($rows, $file) {

    // Explain the reader how it should interpret each row,
    // for every file inside the batch
    $rows->each(function($row) {

        // Example: dump the firstname


  • Import multiple files
$files = [

Excel::batch($files, function($rows, $file) {

  • Edit File Contents
Excel::load('file.csv', function($file) {
    // modify

  • Convert file
Excel::load('file.csv', function($file) {
    // modify stuff


3 - Export

Laravel Excel can create Excel or CSV files from Eloquent models and PHP array.

  • Export to Excel5 (xls)
Excel::create('Filename', function($excel) {


// or
  • Export to Excel2007 (xlsx)

// or
  • Export to CSV

// or
  • Store on server
Excel::create('fileName', function($excel) {

    // Set sheets

  • Creating a sheet
Excel::create('Filename', function($excel) {

    $excel->sheet('Sheetname', function($sheet) {

        // Sheet manipulation



4 - @Blade to Excel

You can use Laravel's Blade to export the excel file, share a view, load a view to the sheet or create an html table inside the view.

  • Load a view to a sheet you use -> loadView ().
Excel::create('New file', function($excel) {

    $excel->sheet('New sheet', function($sheet) {



  • Use different views for different sheets
Excel::create('New file', function($excel) {

    $excel->sheet('First sheet', function($sheet) {


    $excel->sheet('Second sheet', function($sheet) {


  • Share the view for all sheets
  • Transmission into view
$sheet->loadView('view', ['key' => 'value']);


// Using normal with()
    ->with('key', 'value');

// using dynamic with()

Here are some basic features of Larave Excel. Hopefully this article will help you get to know a part about Laravel Excel.


All rights reserved

Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí