Laravel 5 import export to excel and csv using maatwebsite example.

Laravel 5 import export to excel and csv using maatwebsite example.

In this post we will give you information about Laravel 5 import export to excel and csv using maatwebsite example.. Hear we will give you detail about Laravel 5 import export to excel and csv using maatwebsite example.And how to use it also give you demo for it if it is necessary.

In this post i will show you how to import excel or csv to store in database and how to export or download excel or csv file from database table by maatwebsite. maatwebsite packages throught you can easily get data, also you can group by data, also create more then one sheet etc. so now i show you simple example of items table data, you can donwload in xls, xlsx and csv formate and also you import data in xls, xlsx and csv formate file. In following few step you can implement import and export both function in your project. First see your browser preview will become like this:

Update For Laravel 5.6:Laravel 5.6 Import Export to Excel and CSV example

Preivew:

Preivew Of Import File:

Step 1: Installation

Open your composer.json file and add bellow line in required package.

Laravel 5

"maatwebsite/excel": "~2.1.0"

Laravel 4

"maatwebsite/excel": "~1.3"

Then, run command composer update

Now open config/app.php file and add service provider and aliase.

'providers' => [

....

'MaatwebsiteExcelExcelServiceProvider',

],

'aliases' => [

....

'Excel' => 'MaatwebsiteExcelFacadesExcel',

],

Config

If you are using Laravel 5 then fire following command:

php artisan vendor:publish

If you are using Laravel 4 then fire following command:

php artisan config:publish maatwebsite/excel

This command will create config file for excel package.

Also see:Laravel 5 export to pdf using maatwebsite example

Step 3: Create Table and Model

In this step we have to create migration for items table using Laravel 5 php artisan command, so first fire bellow command:

php artisan make:migration create_items_table

After this command you will find one file in following path database/migrations and you have to put bellow code in your migration file for create items table.



use IlluminateDatabaseSchemaBlueprint;

use IlluminateDatabaseMigrationsMigration;

class CreateItemsTable extends Migration

{

public function up()

{

Schema::create('items', function (Blueprint $table) {

$table->increments('id');

$table->string('title');

$table->text('description');

$table->timestamps();

});

}

public function down()

{

Schema::drop("items");

}

}

After craete “items” table you should craete Item model for items, so first create file in this path app/Item.php and put bellow content in item.php file:

app/Item.php



namespace App;

use IlluminateDatabaseEloquentModel;

class Item extends Model

{

public $fillable = ['title','description'];

}

Also see:Laravel 5 export to pdf using maatwebsite example

Step 3: Create Route

In this is step we need to create route of import export file. so open your app/Http/routes.php file and add following route.



Route::get('importExport', 'MaatwebsiteDemoController@importExport');

Route::get('downloadExcel/{type}', 'MaatwebsiteDemoController@downloadExcel');

Route::post('importExcel', 'MaatwebsiteDemoController@importExcel');


Step 4: Create Controller

Ok, now we should create new controller as MaatwebsiteDemoController in this path app/Http/Controllers/MaatwebsiteDemoController.php. this controller will manage all impostExport, downloadExcel and importExcel request and return response, so put bellow content in controller file:

app/Http/Controllers/MaatwebsiteDemoController.php



use Input;

use AppItem;

use DB;

use Excel;

class MaatwebsiteDemoController extends Controller

{

public function importExport()

{

return view('importExport');

}

public function downloadExcel($type)

{

$data = Item::get()->toArray();

return Excel::create('onlinecode_example', function($excel) use ($data) {

$excel->sheet('mySheet', function($sheet) use ($data)

{

$sheet->fromArray($data);

});

})->download($type);

}

public function importExcel()

{

if(Input::hasFile('import_file')){

$path = Input::file('import_file')->getRealPath();

$data = Excel::load($path, function($reader) {

})->get();

if(!empty($data) && $data->count()){

foreach ($data as $key => $value) {

$insert[] = ['title' => $value->title, 'description' => $value->description];

}

if(!empty($insert)){

DB::table('items')->insert($insert);

dd('Insert Record successfully.');

}

}

}

return back();

}

}

Step 5: Create View

let’s create importExport.blade.php(resources/views/importExport.blade.php) for layout and we will write design code here and put following code :

importExport.blade.php

Also see:Laravel 5.7 Import Export Excel to database Example


<html lang="en">

<head>

<title>Import - Export Laravel 5</title>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" >

</head>

<body>

<nav >

<div >

<div >

<a href="#">Import - Export in Excel and CSV Laravel 5</a>

</div>

</div>

</nav>

<div >

<a href="{{ URL::to('downloadExcel/xls') }}"><button >Download Excel xls</button></a>

<a href="{{ URL::to('downloadExcel/xlsx') }}"><button >Download Excel xlsx</button></a>

<a href="{{ 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">

<input type="file" name="import_file" />

<button >Import File</button>

</form>

</div>

</body>

</html>

Ok, Now we are ready for run this script on browser so, please run project and Check it….

Laravel 5 export to pdf using maatwebsite example

Video

Hope this code and post will helped you for implement Laravel 5 import export to excel and csv using maatwebsite 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 code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

3  +  7  =  

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US