import and export Excel and CSV files in laravel 5.1

An eloquent way of importing and exporting Excel and CSV files inLaravel 5.* with the power of PHPOffice’s PHPExcel
Installation

Require this package in your composer.json and update composer. This will download the package and PHPExcel of PHPOffice.

"maatwebsite/excel": "~2.0"

After updating composer, add the ServiceProvider to the providers array in app/config/app.php

'Maatwebsite\Excel\ExcelServiceProvider',

You can use the facade for shorter code. Add this to your aliasses:

'Excel' => 'Maatwebsite\Excel\Facades\Excel',

The class is binded to the ioC as excel

$excel = App::make('excel');

To publish the config settings in Laravel 5 use:

php artisan vendor:publish

This will add an excel.php config file to your config folder.
Importing a file

To start importing a file, you can use ->load($filename). The callback is optional.

Excel::load('file.xls', function($reader) {

// reader methods

});

ExcelFile injections

Following the Laravel 5.0 philosophy with its new awesome FormRequest injections, we introduce you ExcelFile injections.
ExcelFile class

This class is a wrapper for a file on your server. Inside the getFile() method you return the filename and it’s location. Inside the getFilters() method you can enable filters, like the chunk filter.

class UserListImport extends \Maatwebsite\Excel\Files\ExcelFile {

public function getFile()
 {
 return storage_path('exports') . '/file.csv';
 }

public function getFilters()
 {
 return [
 'chunk'
 ];
 }

}

If you want to have the getFile() dynamic based on user’s input, you can easily do:

public function getFile()
 {
 // Import a user provided file
 $file = Input::file('report');
 $filename = $this->doSomethingLikeUpload($file);

// Return it's location
 return $filename;
 }

About the author

Deven Rathore

I'm Deven Rathore, a multidisciplinary & self-taught designer with 3 years of experience. I'm passionate about technology, music, coffee, traveling and everything visually stimulating. Constantly learning and experiencing new things.

  • Chandra Sekhar

    In Laravel 5.1 not supporting. Please guide us how to enable export excel in laravel 5.1

    • Deven Rathore

      are you using laravel 5.1.11 ?

      • Chandra Sekhar

        yes

        • Chandra Sekhar

          using Laravel 5.1.11

          • author of package is currently working on it ! it will be updated shortly

  • Kurt Dela Rosa

    where to write this $excel = App::make(‘excel’); ??

  • gsc 89

    Basically you just copy pasted the info already in the docs…

    • Deven Rathore

      Hii sorry to hear that.. We are now following strict rules to publish a post in our site from authors .. This post is not a tutorial and accepted a year ago to promote the package .. Regards

    • Mike

      This is ridiculous. A tutorial is supposed to go into more detail, show examples. Whoever wrote this copied the docs word for word.

      • gsc 89

        Yeah I gave up using this because I was importing a sheet with 6000 rows and it just fell flat on its face trying to handle that much data within the laravel framework, ended up heading to php classes and using something from there.

        Glad I wasn’t the only one to see the laziness in this tutorial.

        • Mike

          I’ll only have a few hundred rows at most so hopefully that’ll handle it.

  • Shume Berhanu

    Hey need help. I can import a single csv file to the database with headings, but I don’t want to use the heading. Tried use use $reader->noHeading() in my controller function but no change says unknown column 0. Any help …

    • Victor Bala

      In config/excel in import section under heading, change slugged to false.

Pin It on Pinterest

Shares

Get the best in web dev

Join dunebook.com and recieve best in web dev , once a week FREE

An email has been Sent to your Inbox ! Please Confirm your Subscription :)