如何更改字符 laravel excel maatwebsite 的颜色?
How can I change color a character laravel excel maatwebsite?
我从这里获取教程:https://laravel-excel.maatwebsite.nl/3.0/exports/extending.html
所以我使用版本 3
我的 excel 是这样的:
我想改成这样:
所以我想要字符 England
变成红色和粗体
我这样试:
namespace App\Exports;
...
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\BeforeExport;
use Maatwebsite\Excel\Events\AfterSheet;
class SummaryExport implements FromView, WithEvents
{
...
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'],
],
]
]
);
},
];
}
}
存在这样的错误:
Method Maatwebsite\Excel\Sheet::styleCells does not exist.
如何解决这个错误?
您可以在服务提供商的 boot
方法中注册宏。例如 App\Providers\AppServiceProvider
供应商将如下所示:
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use \Maatwebsite\Excel\Sheet;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
//
Sheet::macro('styleCells', function (Sheet $sheet, string $cellRange, array $style) {
$sheet->getDelegate()->getStyle($cellRange)->applyFromArray($style);
});
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
}
You should create different service provider to resister this kind of macro for isolating third party's concern.
字体颜色设置字体样式:
public function registerEvents(): array
{
return [
AfterSheet::class => function(AfterSheet $event) {
$event->sheet->styleCells(
'B1:D1',
[
//Set border Style
'borders' => [
'outline' => [
'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK,
'color' => ['argb' => 'EB2B02'],
],
],
//Set font style
'font' => [
'name' => 'Calibri',
'size' => 15,
'bold' => true,
'color' => ['argb' => 'EB2B02'],
],
//Set background style
'fill' => [
'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
'startColor' => [
'rgb' => 'dff0d8',
]
],
]
);
},
];
}
我从这里获取教程:https://laravel-excel.maatwebsite.nl/3.0/exports/extending.html
所以我使用版本 3
我的 excel 是这样的:
我想改成这样:
所以我想要字符 England
变成红色和粗体
我这样试:
namespace App\Exports;
...
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\BeforeExport;
use Maatwebsite\Excel\Events\AfterSheet;
class SummaryExport implements FromView, WithEvents
{
...
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'],
],
]
]
);
},
];
}
}
存在这样的错误:
Method Maatwebsite\Excel\Sheet::styleCells does not exist.
如何解决这个错误?
您可以在服务提供商的 boot
方法中注册宏。例如 App\Providers\AppServiceProvider
供应商将如下所示:
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use \Maatwebsite\Excel\Sheet;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
//
Sheet::macro('styleCells', function (Sheet $sheet, string $cellRange, array $style) {
$sheet->getDelegate()->getStyle($cellRange)->applyFromArray($style);
});
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
}
You should create different service provider to resister this kind of macro for isolating third party's concern.
字体颜色设置字体样式:
public function registerEvents(): array
{
return [
AfterSheet::class => function(AfterSheet $event) {
$event->sheet->styleCells(
'B1:D1',
[
//Set border Style
'borders' => [
'outline' => [
'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK,
'color' => ['argb' => 'EB2B02'],
],
],
//Set font style
'font' => [
'name' => 'Calibri',
'size' => 15,
'bold' => true,
'color' => ['argb' => 'EB2B02'],
],
//Set background style
'fill' => [
'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
'startColor' => [
'rgb' => 'dff0d8',
]
],
]
);
},
];
}