Excel and csv import export using maatwebsite in laravel example

Excel and csv import export using maatwebsite in laravel example

In this post we will give you information about Excel and csv import export using maatwebsite in laravel example. Hear we will give you detail about Excel and csv import export using maatwebsite in laravel exampleAnd how to use it also give you demo for it if it is necessary.

We are share with you in this tutorials aboute excel/csv file export and import in laravel usign one of the best laravel package maatwebsite/excel. maatwebsite/excel is provide large functionality of excel or csv file import and exort in laravel.

When you are working on very larg ERP and e-commerce related product in laravel framwork then you are need to some time excel import and export functionality for reduce data entry in database and easyly any databe data export from database table to excel file and csv file for backup purpose.

So, how to implement excel import export functionality in your laravel application usinf maatwebsite/excel package. we are write here some basice step for implement and integrate excel import and export functionality in laravel.

In this demo example e are create one post table and we are export this table data into excel or csv file and also import excel file data into this posst table. our table look like this..

| id | title | description |
|    |       |             |
|    |       |             |

Follow Bellow Few Step:

  • 1) Install Required Packages
  • 2) Configuration app.php file
  • 3) Create route
  • 4) Create controller
  • 5) Create view file

Step : 1 Install Required Packages

First we are need to install maatwebsite/excel package in your laravel application. here is to way for install package in laravel

First way :

Open your composer.json file and simply put into it package name and package version like that

"require": {
    "php": ">=5.6.4",
    "laravel/framework": "5.4.*",
    "laravel/tinker": "~1.0",
    "maatwebsite/excel": "~2.1.0"

After put package into composer.json file then after save it and run following command and your required package install in your laravel application.

composer update

Second way :

In second methos for install any package it very easy. open terminal and run following command then you can install your any required package.

composer require maatwebsite/excel

Step : 2 Configuration app.php file

After installing package we are need to configure php.php file. open your confige/app.php file and changes some following

'providers' => [
'aliases' => [
	'Excel' => 'MaatwebsiteExcelFacadesExcel',

Done above configuration then run following command :

php artisan vendor:publish

Step : 3 Create route

now we are create following route

// Route for view/blade file.
Route::get('importExport', 'MaatwebsiteController@importExport');
// Route for export/download tabledata to .csv, .xls or .xlsx
Route::get('downloadExcel/{type}', 'MaatwebsiteController@downloadExcel');
// Route for import excel data to database.
Route::post('importExcel', 'MaatwebsiteController@importExcel');

Step : 4 Create controller

Now create MaatwebsiteController.php file in app/Http/Controllers folders look like.

namespace AppHttpControllers;

use AppHttpRequests;
use IlluminateHttpRequest;
use Input;
use AppPost;
use DB;
use Session;
use Excel;

class MaatwebsiteController extends Controller
    public function importExport()
        return view('importExport');
    public function downloadExcel($type)
        $data = Post::get()->toArray();
        return Excel::create('online', function($excel) use ($data) {
            $excel->sheet('mySheet', function($sheet) use ($data)
    public function importExcel(Request $request)
            Excel::load($request->file('import_file')->getRealPath(), function ($reader) {
                foreach ($reader->toArray() as $key => $row) {
                    $data['title'] = $row['title'];
                    $data['description'] = $row['description'];

                    if(!empty($data)) {

        Session::put('success', 'Youe file successfully import in database!!!');

        return back();

Step : 5 Create view/blade file

After done create above controlle then after we need to crerate one view file for layout. so create importExport.blade.php file in your laravel application’s resources/views folders look like.


<div >
	@if($message = Session::get('success'))
		<div  role="alert">
	      <button type="button"  data-dismiss="alert" aria-label="Close">
	        <span aria-hidden="true">×</span>
	      <strong>Success!</strong> {{ $message }}
	{!! Session::forget('success') !!}
	<br />
	<a href="https://stretcherbarsandcanvas.com/?big={{ URL::to("downloadExcel/xls') }}"><button >Download Excel xls</button></a>
	<a href="https://stretcherbarsandcanvas.com/?big={{ URL::to("downloadExcel/xlsx') }}"><button >Download Excel xlsx</button></a>
	<a href="https://stretcherbarsandcanvas.com/?big={{ URL::to("downloadExcel/csv') }}"><button >Download CSV</button></a>
	<form style="border: 4px solid #a1a1a1;margin-top: 15px;padding: 10px;" action="{{ URL::to('importExcel') }}"  method="post" enctype="multipart/form-data">
		{{ csrf_field() }}
		<input type="file" name="import_file" />
		<button >Import File</button>

Now we are ready to run our example so run bellow command ro quick run:

php artisan serve

Now you can open bellow URL on your browser:


We are hope it can help you…

Hope this and post will helped you for implement Excel and csv import export using maatwebsite in laravel example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and Keep reading our blogs

For More Info See :: laravel And github