Good Day guys,.. I need to download a excel template only show with headers,..
I have good and working function code in laravel-excel version 2 of my problem,. but in version 3.1 i dont know how to code it.
this is my code in version 2;
public function downloadCoursesTemplate()
{
$columns = array(
'Course Code',
'Course Description',
'Status'
);
return Excel::download('Courses', function ($excel) use ($columns) {
$excel->sheet('Courses', function ($sheet) use ($columns) {
$sheet->fromArray($columns);
});
})->export('xlsx');
}
this is the output:
I want that on laravel-excel version 3.1
There have documentation in new versions but i could not find it to solve my issue.
Is anyone can help me? thanks. sorry for my English grammar.
The documentation should be pretty clear, it even has a quick start guide here
But here is what you can do:
Generate the export class
php artisan make:export CoursesTemplateExport
Change the class to the following:
<?php
namespace App\Exports;
use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\WithHeadings;
class CoursesTemplateExport implements FromArray, WithHeadings
{
/**
* @return array
*/
public function array(): array
{
return [];
}
/**
* @return array
*/
public function headings(): array
{
return [
'Course Code',
'Course Description',
'Status'
];
}
}
We can implement the FromArray
interface so we can just return an empty array since we don't have any data, and we can implement the WithHeadings
interface to declare the headings the export should have.
In your Controller:
use App\Exports\CoursesTemplateExport;
public function downloadCoursesTemplate()
{
return Excel::download(new CoursesTemplateExport(), 'Courses.xlsx');
}
This will result in the following excel: