gpt4 book ai didi

excel - 如何更改 Laravel Excel Maat 网站角色的颜色?

转载 作者:行者123 更新时间:2023-12-02 21:17:28 25 4
gpt4 key购买 nike

我从这里获取教程:https://laravel-excel.maatwebsite.nl/3.0/exports/extending.html

所以我使用版本3

我的Excel是这样的:

enter image description here

我想把它改成这样:

enter image description here

所以我希望字符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.

对于字体颜色设置字体样式:

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',
[
//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',
]
],

]
);
},
];
}
}

关于excel - 如何更改 Laravel Excel Maat 网站角色的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51703792/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com