'Is it possible to add a border to an entire column with Laravel Excel?

With Laravel Excel, it's easy to add a border to a cell:

$sheet->cell('A1', function($cell) {
    $cell->setBorder('none', 'none', 'thin', 'none')
});

It's easy to add a border to a row too:

$sheet->row(3, function($row) {
    $row->setBorder('none', 'none', 'thin', 'none')
});

But I did not found anything about column (maybe I did not search well). Is it possible to add a border (or some other style) to a whole column?



Solution 1:[1]

1st You have to add event by extends WithEvents and use

 use Maatwebsite\Excel\Concerns\WithEvents;

Then you can apply like this....

public function registerEvents(): array
{
    $alphabetRange  = range('A', 'Z');
    $alphabet       = $alphabetRange[$this->totalValue+6]; // returns Alphabet

    $totalRow       = (count($this->attributeSets) * 3) + count($this->allItems)+1;
    $cellRange      = 'A1:'.$alphabet.$totalRow;

    return [
        AfterSheet::class    => function(AfterSheet $event) use($cellRange) {
            $event->sheet->getStyle($cellRange)->applyFromArray([
                'borders' => [
                    'allBorders' => [
                        'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
                        'color' => ['argb' => '000000'],
                    ],
                ],
            ])->getAlignment()->setWrapText(true);
        },
    ];
}

For Details or more you can follow The doc https://docs.laravel-excel.com/3.1/getting-started/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Zahid Hassan Shaikot