您可以在服务提供商的
boot
方法。例如
App\Providers\AppServiceProvider
提供商将如下所示:
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use \Maatwebsite\Excel\Sheet;
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
Sheet::macro('styleCells', function (Sheet $sheet, string $cellRange, array $style) {
$sheet->getDelegate()->getStyle($cellRange)->applyFromArray($style);
});
}
public function register()
{
}
}
您应该创建不同的服务提供商来抵制这种用于隔离第三方关注点的宏。
对于字体颜色集字体样式:
public function registerEvents(): array
{
return [
AfterSheet::class => function(AfterSheet $event) {
$event->sheet->styleCells(
'B1:D1',
[
'borders' => [
'outline' => [
'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK,
'color' => ['argb' => 'EB2B02'],
],
],
'font' => [
'name' => 'Calibri',
'size' => 15,
'bold' => true,
'color' => ['argb' => 'EB2B02'],
],
'fill' => [
'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
'startColor' => [
'rgb' => 'dff0d8',
]
],
]
);
},
];
}