Last active
April 9, 2018 04:50
-
-
Save ronokdev/db6d5cc1a4b712086805d258b76de58f to your computer and use it in GitHub Desktop.
Excel Download Via Laravel maatwebsite
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Ref URL → https://laravel-excel.maatwebsite.nl/docs/3.0/getting-started/installation | |
# Run the command in CLI → | |
composer require maatwebsite/excel | |
# Add the ServiceProvider in app/config/app.php → | |
'providers' => | |
[ | |
Maatwebsite\Excel\ExcelServiceProvider::class, | |
] | |
'aliases' => | |
[ | |
'Excel' => Maatwebsite\Excel\Facades\Excel::class, | |
] | |
# Run the Command in the CLI → php artisan vendor:publish | |
# In Controller → | |
* Add → use Excel; | |
* Add Functioin → | |
public function DownloadExcel($fullReturn, $type) | |
{ | |
$excelData = $fullReturn['data']; | |
return Excel::create($fullReturn['ExcelName'], function ($excel) use ($excelData) { | |
$excel->sheet('mySheet', function ($sheet) use ($excelData) { | |
$sheet->fromArray($excelData); | |
}); | |
})->download($type); | |
} | |
* Call the function : | |
$fullReturn = array( | |
'dateFrom' => $tempData['d_start'], | |
'dateTo' => $tempData['d_end'], | |
'ExcelName' => 'Address wise diseases from ' . $tempData['d_start'] . '_to_' . $tempData['d_end'], | |
'data' => $tempAllData | |
); | |
$this->DownloadExcel($fullReturn, 'xlsx'); | |
* tempAllData → Must be Json. | |
* Example → | |
data: [ | |
{ | |
column_name_1: "data", | |
column_name_2: "data", | |
column_name_3: "data", | |
column_name_4: "data", | |
column_name_5: "data", | |
column_name_4: "data" | |
}, | |
{ | |
column_name_1: "data", | |
column_name_2: "data", | |
column_name_3: "data", | |
column_name_4: "data", | |
column_name_5: "data", | |
column_name_4: "data" | |
}, | |
{ | |
column_name_1: "data", | |
column_name_2: "data", | |
column_name_3: "data", | |
column_name_4: "data", | |
column_name_5: "data", | |
column_name_4: "data" | |
}] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$type of DownloadExcel MUST be 'xlsx'