55 lines
1.7 KiB
PHP
55 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Exports;
|
|
|
|
use Maatwebsite\Excel\Concerns\FromArray;
|
|
use Maatwebsite\Excel\Concerns\WithEvents;
|
|
use Maatwebsite\Excel\Events\AfterSheet;
|
|
use PhpOffice\PhpSpreadsheet\Style\Fill;
|
|
|
|
class CustomInvoiceExport implements FromArray, WithEvents
|
|
{
|
|
protected $data;
|
|
protected $headers;
|
|
|
|
public function __construct(array $data, array $headers)
|
|
{
|
|
$this->data = $data;
|
|
$this->headers = $headers;
|
|
}
|
|
|
|
public function array(): array
|
|
{
|
|
return $this->data;
|
|
}
|
|
|
|
public function registerEvents(): array
|
|
{
|
|
return [
|
|
AfterSheet::class => function (AfterSheet $event) {
|
|
$sheet = $event->sheet->getDelegate();
|
|
|
|
// Freeze first row
|
|
$sheet->freezePane('A2');
|
|
|
|
// Auto filter for all header columns
|
|
$columnCount = count($this->headers);
|
|
$lastColumnLetter = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($columnCount);
|
|
$sheet->setAutoFilter("A1:{$lastColumnLetter}1");
|
|
|
|
// Style each header with individual colors
|
|
foreach ($this->headers as $index => $header) {
|
|
$cell = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($index + 1) . '1';
|
|
$sheet->getStyle($cell)->applyFromArray([
|
|
'font' => ['bold' => true, 'color'=> ['rgb' => isset($header['fontcolor']) ? $header['fontcolor'] : 'FFFFFF']],
|
|
'fill' => [
|
|
'fillType' => Fill::FILL_SOLID,
|
|
'startColor' => ['rgb' => $header['color']],
|
|
],
|
|
]);
|
|
}
|
|
},
|
|
];
|
|
}
|
|
}
|