Laravel Excel and csv Import Export Example

Rate this post

Today, We want to share with you Laravel Excel and csv Import Export Example.In this post we will show you Import & Export Data in Laravel Excel, hear for Laravel 5 import export to excel and csv using maatwebsite example.
we will give you demo and example for implement.In this post, we will learn about Import & Export Data in CSV in Laravel 5 with an example.

Laravel Excel and csv import export using maatwebsite

There are the Following The simple About Laravel Excel and csv Import Export Full Information With Example and source code.

Step 1: Laravel Installation for laravel excel import export

Laravel 5
[php]
“maatwebsite/excel”: “~2.1.0”
[/php]

Laravel 4
[php]
“maatwebsite/excel”: “~1.3”
[/php]

READ :  Angular simple progress bar using bootstrap
Laravel-Excel

composer update

and then open this file config/app.php and put this service provider and some aliase.

[php]
‘providers’ => [
….
…..
……
‘Maatwebsite\Excel\ExcelServiceProvider’,
],

‘aliases’ => [

….
…..
……
‘Excel’ => ‘Maatwebsite\Excel\Facades\Excel’,

],
[/php]

Config for Laravel 5 excel then fire following command
[php]
php artisan vendor:publish
[/php]

Config for Laravel 4 excel then fire following command
[php]
php artisan config:publish maatwebsite/excel
[/php]

Step 2: Make Table and Model

make a migration for products table
[php]
php artisan make:migration create_products_table
[/php]

Path:- database/migrations
[php]
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateItemsTable extends Migration
{
public function up()
{
Schema::create(‘products’, function (Blueprint $table) {
$table->increments(‘id’);
$table->string(‘title’);
$table->text(‘information’);
$table->timestamps();
});
}
public function down()
{
Schema::drop(“products”);
}
}
[/php]

model for Items – app/Item.php
[php]
namespace App;
use Illuminate\Database\Eloquent\Model;
class Item extends Model
{
public $fillable = [‘title’,’information’];
}
[/php]

READ :  Vuejs Interview Questions And Answers

Step 3: Create Route

file Path : app/Http/routes.php
[php]
Route::get(‘laravelimportExport’, ‘[email protected]’);
Route::get(‘laravelExcelDownload/{type}’, ‘[email protected]’);
Route::post(‘laraveImportlExcel’, ‘[email protected]’);
[/php]

Step 4: Create Controller

app/Http/Controllers/MaatwebsiteDemoController.php
[php]
use Input;
use App\Item;
use DB;
use Excel;
class MaatwebsiteDemoController extends Controller
{
public function laravelimportExport()
{
return view(‘laravelimportExport’);
}
public function laravelExcelDownload($type)
{
$data = Item::get()->toArray();
return Excel::create(‘infinityknow_example’, function($excel) use ($data) {
$excel->sheet(‘mySheet’, function($sheet) use ($data)
{
$sheet->fromArray($data);
});
})->download($type);
}
public function laraveImportlExcel()
{
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, ‘information’ => $value->information];
}
if(!empty($insert)){
DB::table(‘products’)->insert($insert);
dd(‘Insert Record successfully.’);
}
}
}
return back();
}
}
[/php]

Step 5: Create View

laravelimportExport.blade.php
[php]

Excel and csv import export using maatwebsite in laravel example

[/php]

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Laravel 5 import export to excel and csv.
I would like to have feedback on my Infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Reply

Your email address will not be published.