How to Import CSV and excel files in laravel,laravel export csv download,laravel excel loadview,laravel excel header,laravel 5.4 export to excel,export csv laravel maatwebsite,laravel excel export large data,export csv file in laravel 5,Exporting | Laravel Excel,PHP Laravel 5.5 - import export data into excel and csv,Excel and csv import export using maatwebsite in laravel example,Laravel 5.7 Import Export Excel to database Example,Laravel 5 import export to excel and csv using maatwebsite example,Laravel 5 Export Data in Excel and CSV,Import & Export Data in CSV in Laravel 5,Laravel 5.6 Import Export to Excel and CSV example

How to Import CSV and excel files in laravel

How to Import CSV and excel files in laravel

To import and export CSV and EXCEL file in laravel Maatwebsite provide a package. Maatwebsite laravel package allows you to easily import and export CSV and EXCEL files from your Eloquent Models. Let’s start with an example:-

How to install

If you want to install the package via composer run below command.

composer require maatwebsite/excel

Now we need to configure app.php file. So open your confige/app.php file and change following code:-

'providers' => [
....
'Maatwebsite\Excel\ExcelServiceProvider',
],
'aliases' => [
....
'Excel' => 'Maatwebsite\Excel\Facades\Excel',
],

After set the configuration in your confige/app.php file run following command :

php artisan vendor:publish

Now we have to create a route for generating csv and excel file.

Route::post(‘importcsv’, ‘VrsoftcoderController@importCSV’);

Route::post(‘importexcel’, ‘VrsoftcoderController@importExcel’);

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

namespace App\Http\Controllers;

use App\Http\Requests;
use Illuminate\Http\Request;
use Input;
use App\Post;
use DB;
use Session;
use Excel;

class VrsoftcoderController extends Controller
{

public function importExcelFile(Request $request)
{

if($request->hasFile('import_file')){
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)) {
DB::table('post')->insert($data);
}
}
});
}

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

return back();
}

public function importCSVFile(Request $request)
{
if($request->hasFile('import_file')){
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)) {
DB::table('post')->insert($data);
}
}
});
}

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

return back();
}

}

Add Your Business

How to Import CSV and excel files in laravel
How to Import CSV and excel files in laravel

To generate CSV and EXCEL file in laravel fllow the below insructions.

Create a route for generating csv and excel file.

Route::get('generatecsv', 'VrsoftcoderController@generateCSV');

Route::get('generateexcel', 'VrsoftcoderController@generateExcel');

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

namespace App\Http\Controllers;

use App\Http\Requests;
use Illuminate\Http\Request;
use Input;
use App\Post;
use DB;
use Session;
use Excel;

class VrsoftcoderController extends Controller
{

public function generateExcelFile()
{
$ExcelDataArray = array("name","DOB",'email',"phone");
$type = "xls";

return Excel::create('laravelexcel', function($excel) use ($data) {
$excel->sheet('mySheet', function($sheet) use ($data)
{
$sheet->fromArray($ExcelDataArray);
});
})->download($type);
}

public function generateCSVFile()
{
$ExcelDataArray = array("name","DOB",'email',"phone");
$type = "csv";

return Excel::create('laravelexcel', function($excel) use ($data) {
$excel->sheet('mySheet', function($sheet) use ($data)
{
$sheet->fromArray($ExcelDataArray);
});
})->download($type);
}

}

We hope this article find helpful for you. You can also check our article How to Choose the Best Domain Name. If you liked this article, then please share it on your social networks.

Add Your Business

class=”qarea”>
How to Import CSV and excel files in laravel,laravel export csv download,laravel excel loadview,laravel excel header,laravel 5.4 export to excel,export csv laravel maatwebsite,laravel excel export large data,export csv file in laravel 5,Exporting | Laravel Excel,PHP Laravel 5.5 – import export data into excel and csv,Excel and csv import export using maatwebsite in laravel example,Laravel 5.7 Import Export Excel to database Example,Laravel 5 import export to excel and csv using maatwebsite example,Laravel 5 Export Data in Excel and CSV,Import & Export Data in CSV in Laravel 5,Laravel 5.6 Import Export to Excel and CSV example

Post Created 130

Leave a Reply

Related Posts

Begin typing your search above and press enter to search. Press ESC to cancel.

Back To Top