如何在Laravel Excel中设置行的背景色?
我使用Laravel Excel库,并尝试了以下代码:如何在Laravel Excel中设置行的背景色?,laravel,laravel-5,laravel-excel,Laravel,Laravel 5,Laravel Excel,我使用Laravel Excel库,并尝试了以下代码: public function registerEvents(): array { return [ AfterSheet::class => function(AfterSheet $event) { $styleArray = array('fill' => array( 'color' => array('rgb' =>
public function registerEvents(): array
{
return [
AfterSheet::class => function(AfterSheet $event) {
$styleArray = array('fill' => array(
'color' => array('rgb' => '000000')
));
$cellRange = 'A1:W1'; // All headers
$event->sheet->getDelegate()->getStyle($cellRange)->applyFromArray($styleArray);
},
];
}
因此,我得到的标题没有黑色的背景色
我还尝试了以下阵列设置:
$styleArray = [
'font' => [
'bold' => true,
],
'background' => [
'color'=> '#000000'
]
];
我使用事件,而不是创建。请不要推荐不相关的答案试试这个
$sheet->row(1, ['Col 1', 'Col 2', 'Col 3']); // etc etc
$sheet->row(1, function($row) { $row->setBackground('#CCCCCC'); });
您还可以将$sheet->row更改为$sheet->cell,并继续传递行号作为第一个参数
$sheet->cell(1, function($row) {
$row->setBackground('#CCCCCC');
});
然后,您还可以使用更为Excel的ish表示法:
$sheet->cells('A1:D1', function ($cells) {
$cells->setBackground('#008686');
$cells->setAlignment('center');
});
试试这个
$sheet->row(1, ['Col 1', 'Col 2', 'Col 3']); // etc etc
$sheet->row(1, function($row) { $row->setBackground('#CCCCCC'); });
您还可以将$sheet->row更改为$sheet->cell,并继续传递行号作为第一个参数
$sheet->cell(1, function($row) {
$row->setBackground('#CCCCCC');
});
然后,您还可以使用更为Excel的ish表示法:
$sheet->cells('A1:D1', function ($cells) {
$cells->setBackground('#008686');
$cells->setAlignment('center');
});
根据新的Laravel excel 3.1,可以使用宏进行样式设置 您可以像我一样使用宏进行样式设置 在开始上课之前,我添加了以下代码
Sheet::macro('styleCells', function (Sheet $sheet, string $cellRange, array $style) {
$sheet->getDelegate()->getStyle($cellRange)->applyFromArray($style);
});
然后在“后张事件”下我使用了它
您可以像下面这样注册事件
public function registerEvents(): array
{
return [
AfterSheet::class => [self::class, 'afterSheet']
];
}
然后定义静态函数
public static function afterSheet(AfterSheet $event){
//Single Column
$event->sheet->styleCells(
'A1',
[
'alignment' => [
'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
],
'fill' => [
'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
'color' => ['argb' => $singleHeaderColumnColorCode]
]
]
);
//Range Columns
$event->sheet->styleCells(
'B2:E2',
[
'alignment' => [
'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
],
'fill' => [
'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
'color' => ['argb' => $mergedAndCenterHeaderSubColumnColorCode]
]
]
);
}
确保已添加带有事件的接口
如下图所示,这是我如何进行导出的。
根据新的Laravel excel 3.1,可以使用宏进行样式设置 您可以像我一样使用宏进行样式设置 在开始上课之前,我添加了以下代码
Sheet::macro('styleCells', function (Sheet $sheet, string $cellRange, array $style) {
$sheet->getDelegate()->getStyle($cellRange)->applyFromArray($style);
});
然后在“后张事件”下我使用了它
您可以像下面这样注册事件
public function registerEvents(): array
{
return [
AfterSheet::class => [self::class, 'afterSheet']
];
}
然后定义静态函数
public static function afterSheet(AfterSheet $event){
//Single Column
$event->sheet->styleCells(
'A1',
[
'alignment' => [
'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
],
'fill' => [
'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
'color' => ['argb' => $singleHeaderColumnColorCode]
]
]
);
//Range Columns
$event->sheet->styleCells(
'B2:E2',
[
'alignment' => [
'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
],
'fill' => [
'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
'color' => ['argb' => $mergedAndCenterHeaderSubColumnColorCode]
]
]
);
}
确保已添加带有事件的接口
如下图所示,这是我如何进行导出的。
可能重复的是不同的问题,关于背景他们都是关于在Laravel Excel中的样式,代码几乎相同。可能重复的是不同的问题,关于背景他们都是如何在Laravel Excel中进行样式设计的代码几乎相同。我没有$sheet对象您能告诉我们如何获取$sheet吗?这是$event->sheet吗?我有相同的代码背景色不适用于最后一个单元格我没有$sheet对象你能告诉我们如何获取$sheet吗?这是$event->sheet吗?我有相同的代码,背景色不适用于最后一个单元格,什么是$singleHeaderColumnColorCode。如果需要,格式是什么?您好,$singleHeaderColumnColorCode是什么。如果需要,格式是什么?