如何更改laravel excel maatwebsite字符的颜色? [英] How can I change color a character laravel excel maatwebsite?
本文介绍了如何更改laravel excel maatwebsite字符的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我从这里获得教程: https://laravel-excel.maatwebsite .nl/3.0/exports/extending.html
所以我使用版本3
我的excel这样:
我想将其更改为这样:
所以我希望字符England
变为红色和粗体
So I want the character England
change to red color and bold
我这样尝试:
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
提供程序的外观如下:
You may register macro within a service provider's boot
method. For example App\Providers\AppServiceProvider
provider's will look like as:
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.
对于字体颜色设置字体样式:
For font color set font style :
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',
]
],
]
);
},
];
}
这篇关于如何更改laravel excel maatwebsite字符的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文