【问题标题】:How to set background color for row in Laravel Excel?如何在 Laravel Excel 中设置行的背景颜色?
【发布时间】:2019-08-06 21:25:36
【问题描述】:

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

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'
                    ]
                ];

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

【问题讨论】:

标签: laravel laravel-5 laravel-excel


【解决方案1】:

根据新的 Laravel-excel 3.1,可以使用宏进行样式设置 https://docs.laravel-excel.com/3.1/imports/extending.html#macroable

您可以像我一样使用宏进行样式设置。

我在上课前添加了以下代码

Sheet::macro('styleCells', function (Sheet $sheet, string $cellRange, array $style) {
    $sheet->getDelegate()->getStyle($cellRange)->applyFromArray($style);
});

然后在AfterSheet的事件下我使用了它。

您可以像下面这样注册事件

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

确保你已经添加了接口WithEvents

如下图所示,这就是我的出口方式。

【讨论】:

  • 嗨,$singleHeaderColumnColorCode 是什么。如果需要,格式是什么?
【解决方案2】:

试试这个

$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 的符号:

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

【讨论】:

  • 我没有 $sheet 对象
  • 你能告诉我们如何获得$sheet吗?那是$event->sheet吗?
  • 我有相同的代码,背景颜色不适用于最后一个单元格
【解决方案3】:

此代码对我有用。在样式函数中的导出文件中添加此代码

$sheet->getStyle('A1:P1')->getFill()->applyFromArray(['fillType' => 'solid','rotation' => 0, 'color' => ['rgb' => 'D9D9D9'],]);

【讨论】:

    猜你喜欢
    • 2010-12-11
    • 1970-01-01
    • 1970-01-01
    • 2019-02-28
    • 2018-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多