I have used laravel excel version 2.1 previously . In version 2.0 cell designing was pretty easy . So i started a new project in latest edition of laravel (5.5) and was shocked to know that we can't use laravel excel version 2.0 in this version of laravel . I installed latest version of laravel-excel 3.1 and sheet styling is totally different from the version larave-excel version 2.0 so i dig into documentation i found this custom macros to style the sheet but it didn't work.
Here's the link for the laravel excel package being used
Here's my Export Class code
Export Class
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\AfterSheet;
use Maatwebsite\Excel\Concerns\WithEvents;
class CollectionExport implements WithEvents,FromCollection, WithHeadings, ShouldAutoSize
{
use Exportable;
/*************** Alternate Method **************************/
// public function registerEvents(): array
// {
// return [
// AfterSheet::class => function(AfterSheet $event) {
// $cellRange = 'A1:W1'; // All headers
// $event->sheet->getDelegate()->getStyle($cellRange)->getFont()->setSize(14);
// },
// ];
// }
public function registerEvents(): array
{
return [
BeforeExport::class => function(BeforeExport $event) {
$event->writer->setCreator('Patrick');
},
AfterSheet::class => function(AfterSheet $event) {
$event->sheet->setOrientation(\PhpOffice\PhpSpreadsheet\Worksheet\PageSetup::ORIENTATION_LANDSCAPE);
$event->sheet->styleCells(
'A1:W1',
[
'borders' => [
'outline' => [
'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK,
'color' => ['argb' => 'FFFF0000'],
],
]
]
);
},
];
}
public function collection()
{
return collect([
[
'name' => 'Povilas',
'surname' => 'Korop',
'email' => 'povilas@laraveldaily.com',
'twitter' => '@povilaskorop'
],
[
'name' => 'Taylor',
'surname' => 'Otwell',
'email' => 'taylor@laravel.com',
'twitter' => '@taylorotwell'
]
]);
}
public function headings(): array
{
return [
'Name',
'Surname__',
'Email',
'Twitter',
];
}
}
I'm calling this in a specific route
return Excel::download(new CollectionExport(), 'export.xlsx');
This registerEvents method has no effect on the Excel file being created Am I missing something or doing it wrong ? Thanks in Advance !!! P.S : - I have tried both methods for styling none of them effected the style of excel file