Watch Youtube Video (HINDI) :
Hello, My Developers, after few days I am writing this post because many users say me how to generate an excel file in Laravel 6. After a few days of Research and Development (RND) then I am writing this post.
So friends In this post we will know how to generate an excel file in Laravel 6. In this post, I tried to easy to understand this example.
In this post how to generate an excel file in Laravel 6 we will use a one-third party "maatwebsite/excel" package install via the composer. first of all, we need to install this package and configure it in our Laravel 6 project then we can make this example using excel file function.
So let's make this example and run it.
Note : Before starting this example we should clone fresh Laravel 6 Application and setup and run it. You have already Laravel 6 Application then we don't need to clone.
Laravel 6 Image Upload Tutorial
Step 1 : Install maatwebsite/excel package
In this step we need to install composer require maatwebsite/excel package in our application. So let's do this following command.
So, Now Simple open your terminal or command prompt and set your project directory in your terminal and put below command.
composer require maatwebsite/excel
Step 2 : Registred providers into app.php file
In this step we should register excel package providers. So now we open config/app.php file and put below line into providers array.
config/app.php
'providers' => [ //... Maatwebsite\Excel\ExcelServiceProvider::class, ]
Here we load Excle class into app.php file into aliases array so let's do that.
'aliases' => [ //... 'Excel' => Maatwebsite\Excel\Facades\Excel::class, ]
Step 3 : Create Controller and Routes
In this step, we will create one route to run this example and make a controller for Excel file generate so let's do that.
So, Open your web.php file and make routes like below example.
routes/web.php
Route::get('excel-file','DefaultController@index');
After making above routes we have to create Default Controller. So, let's get Default Controller using command through.
php artisan make:controller DefaultController
In DefaultController we put below code into controller and save it.
App\Http\Controllers\DefaultController.php
<?php namespace App\Http\Controllers; use App\Http\Controllers\Controller; use Excel; class DefaultController extends Controller { public function index() { Excel::create('My report 2019', function($excel) { // Set the title $excel->setTitle('My report 2019'); // Chain the setters $excel->setCreator('Me')->setCompany('webappfix'); $excel->setDescription("It's basic reports"); $data = [12,"Hello",123,4321,98765432,"Hi",456,456,456,456]; $excel->sheet('Sheet 1', function ($sheet) use ($data) { $sheet->setOrientation('landscape'); $sheet->fromArray($data, NULL, 'A3'); }); })->download('xlsx'); } }
How to Generate PDF in Laravel 6
You can use as per your need following function.
Example :
Excel::create('Filename', function($excel) { // Excel code })->export('xls'); // or ->download('xls'); // ->store('xlsx', storage_path()); // or ->store('xls', storage_path('Excel-folder'));
So, Now we are ready to run this example. let's run this example and get Excel reports.
Open your browser and run your routes localhost:8000/excel-file
We always thanks to you for reading our blogs.
Dharmesh Chauhan
(Swapinfoway Founder)Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us
Haresh Chauhan
(Co-Founder)We Are Also Recommending You :
- Error : You need to install the imagick extension to use this back end
- Laravel 10 Create Custom Helper Function Example
- All Know About Different Root Path Of The Laravel Application With Example
- Single Session User Login in Laravel
- How To Get Last Record From The Laravel Collection Using last() Collection Method Possible all Example
- Laravel Postcodes
- Laravel 9 Telescope Integration Tutorial
- Median() Method | How It is Different From avg() Method In Laravel Collection
- intervention/image change image format laravel tutorial
- Firebase Dynamic Links Shorten URL PHP Function