本文介绍了如何在Laravel Excel中设置行的背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Laravel Excel库,并且尝试了以下代码:

I use Laravel Excel library and I have tried this code:

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);
        },
    ];
}

结果是标题没有黑色背景颜色.

As result I get headers without black background color.

我也尝试了以下数组设置:

Also I tried this array settings:

$styleArray = [
                    'font' => [
                        'bold' => true,
                    ],
                    'background' => [
                        'color'=> '#000000'
                    ]
                ];

我使用事件,而不是创建事件.请不要推荐不相关的答案

I use events, not creating. Please don't recommend not relevant answers

推荐答案

尝试一下

$sheet->row(1, ['Col 1', 'Col 2', 'Col 3']); // etc etc
$sheet->row(1, function($row) { $row->setBackground('#CCCCCC'); });

您还可以将$ sheet-> row()更改为$ sheet-> cell()并继续传递行号作为第一个参数.

You can also change $sheet->row() to $sheet->cell() and keep passing a row number as first argument.

$sheet->cell(1, function($row) {
    $row->setBackground('#CCCCCC');
});

然后,您还可以使用更多类似Excel的符号:

Then You can also use a more Excel-ish notation :

$sheet->cells('A1:D1', function ($cells) {
    $cells->setBackground('#008686');
    $cells->setAlignment('center');
});

这篇关于如何在Laravel Excel中设置行的背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 03:43