是否可以使用 Laravel Excel 为整列添加边框?

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

使用 Laravel Excel,可以很容易地为单元格添加边框:

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

给行添加边框也很容易:

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

但是我没有找到任何关于专栏的内容(可能我没有搜索好)。是否可以为整列添加边框(或其他样式)?

我认为你可以这样做:

$sheet->setBorder('A1:A10');

这将为 A1 到 A10 的单元格设置边框。所以我猜你需要知道你的专栏的长度。

1st 您必须通过扩展 WithEvents 添加事件并使用

 use Maatwebsite\Excel\Concerns\WithEvents;

那你就可以这样申请....

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);
        },
    ];
}

有关详细信息或更多信息,您可以关注文档 https://docs.laravel-excel.com/3.1/getting-started/