HITENDRA PATEL

GENERATE EXCEL FILE IN LARAVEL

Published 4 years ago 1601 Views
image

We can generate excel files using PHPExcel, but with laravel its super easy to generate.

We need one package for that.


Step 1 : Install Package

run below command in your terminal

composer require maatwebsite/excel

Step 2 : Add Provider & Alias

we need to add service provide & alias in our project config file which located following path : config/app.php

<?php
return [
   "providers" => [
       Maatwebsite\Excel\ExcelServiceProvider::class,
   ],
   "aliases"   => [
       "Excel" => Maatwebsite\Excel\Facades\Excel::class,
   ],
];

Step 3 : Create Export class

Create export class for excel which manage excel properties, views, etc.

Create class in following path : /app/Exports/Export.php

& we will create method which return view of excel.

<?php
namespace App\Exports;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;

class Export implements FromView {
   public function view(): View {
       return view("/exports/excel");
   }
}

Step 4 : Create View

Create blade file with your preferred location.

For best practice we will manage excel & it's cells with HTML table row & column format.

for example :

<table>
   <tr>
       <td>Sr. No.</td>
   </tr>
   <tr>
       <td>1</td>
   </tr>
</table>

Step 5 : Call Export class in method

Now call export class in your method of controller.

Class will return excel file to download.

<?php
namespace App\Http\Controllers;
use App\Exports\Export;
use App\Http\Controllers\Controller;
use Excel;
use Illuminate\Http\Request;

class PoleSurveyController extends Controller {
   public function export(Request $_request) {
       return Excel::download(new Export);
   }
}

 

A BLOG ABOUT DEVELOPMENT

Subscribe and get my latest blog post in your inbox.
image
DIGITAL OCEAN SETUP LARAVEL

Install & configure laravel project into live digital ocean server.

image
LARAVEL PUSH NOTIFICATION

Laravel push notification for devices with FCM

image
INSTALL COMPOSER ON MACOS

Install Composer on MacOS