40, 'B' => 30] */ public function getColumnWidths(): array { return []; } public function setUp() { $this->withFilename(fn ($resource) => $resource::getModelLabel().'-'.date('Y-m-d')); $this->fromTable() ->except(['index']); } public function headings(): array { return [ [$this->getTitle()], $this->getHeadings(), ]; } public function styles(Worksheet $sheet) { return [ 1 => [ 'font' => ['bold' => true, 'size' => 14], 'alignment' => ['horizontal' => Alignment::HORIZONTAL_CENTER], ], 2 => [ 'font' => ['bold' => true], 'alignment' => [ 'horizontal' => Alignment::HORIZONTAL_CENTER, 'vertical' => Alignment::VERTICAL_CENTER, ], 'fill' => [ 'fillType' => Fill::FILL_SOLID, 'startColor' => ['rgb' => 'E2E8F0'], ], ], ]; } public function registerEvents(): array { return [ AfterSheet::class => function (AfterSheet $event) { $sheet = $event->sheet->getDelegate(); $lastColumn = $sheet->getHighestColumn(); $lastRow = $sheet->getHighestRow(); // Merge main title $sheet->mergeCells("A1:{$lastColumn}1"); // Style for all data cells $sheet->getStyle("A2:{$lastColumn}{$lastRow}") ->getAlignment() ->setVertical(Alignment::VERTICAL_TOP) ->setWrapText(true); // Set column widths $columnWidths = $this->getColumnWidths(); foreach (range('A', $lastColumn) as $columnID) { if (isset($columnWidths[$columnID])) { $sheet->getColumnDimension($columnID)->setAutoSize(false); $sheet->getColumnDimension($columnID)->setWidth($columnWidths[$columnID]); } else { $sheet->getColumnDimension($columnID)->setAutoSize(true); } } // Add borders to the table $sheet->getStyle("A2:{$lastColumn}{$lastRow}")->applyFromArray([ 'borders' => [ 'allBorders' => [ 'borderStyle' => Border::BORDER_THIN, 'color' => ['rgb' => 'CBD5E0'], ], ], ]); // Replace
with newlines and ensure wrapping foreach ($sheet->getRowIterator(3) as $row) { $cellIterator = $row->getCellIterator(); $cellIterator->setIterateOnlyExistingCells(false); foreach ($cellIterator as $cell) { $value = $cell->getValue(); if (is_string($value) && (str_contains($value, '
') || str_contains($value, '
') || str_contains($value, '
'))) { $cell->setValue(str_replace(['
', '
', '
'], "\n", $value)); } } } // Set row height to auto for data rows for ($i = 3; $i <= $lastRow; $i++) { $sheet->getRowDimension($i)->setRowHeight(-1); } }, ]; } }