Laravel-excel: Change Font color or add background color to a cell in excel

Created on 5 Sep 2018  路  7Comments  路  Source: Maatwebsite/Laravel-Excel

Prerequisites

  • [X] Able to reproduce the behaviour outside of your code, the problem is isolated to Laravel Excel.
  • [X] Checked that your issue isn't already filed.
  • [X] Checked if no PR was submitted that fixes this problem.

Versions

  • PHP version: 7.1
  • Laravel version: 5.6
  • Package version: 3.0

Description

Steps to Reproduce

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

                        ],
                        'font' => array(
                            'name'      =>  'Calibri',
                            'size'      =>  15,
                            'bold'      =>  true,
                            'color' => ['argb' => 'EB2B02'],
                        )
                    ]
                );
            },
        ];
    }

Expected behavior:
it should changed the font color

Actual behavior:
nothing is working

Additional Information

Any additional information, configuration or data that might be necessary to reproduce the issue.

Most helpful comment

@iSarahSajjad Please read the API docs before you rising the issue!
Your solution is there https://phpspreadsheet.readthedocs.io/en/develop/topics/recipes/#formatting-cells
```
$event->sheet->styleCells(
'C7:G7',
[
'alignment' => [
'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
],
'font' => [
'name' => 'Century Gothic',
'size' => 11,
'bold' => true,
'color' => ['argb' => 'EB2B02'],
]
]
);

$event->sheet->getStyle('C7:G7')->getFill()
->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
->getStartColor()->setARGB('FFFF0000');
```

All 7 comments

nothing is working is a bit of a vague statement. Please be more specific in the future. It's extremely hard to give support to vague descriptions.

Make sure you have imported the AfterSheet namespace in the top or your class. Put a dd($event) in the callback to make sure it get's called at all.

nothing is working of above code, i need to fill color to the cell

yes i imported class fonts changed but color of cell not

Your code is not trying to change the cell background color. Please refer to the PhpSpreadsheet documentation.

@iSarahSajjad Please read the API docs before you rising the issue!
Your solution is there https://phpspreadsheet.readthedocs.io/en/develop/topics/recipes/#formatting-cells
```
$event->sheet->styleCells(
'C7:G7',
[
'alignment' => [
'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
],
'font' => [
'name' => 'Century Gothic',
'size' => 11,
'bold' => true,
'color' => ['argb' => 'EB2B02'],
]
]
);

$event->sheet->getStyle('C7:G7')->getFill()
->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
->getStartColor()->setARGB('FFFF0000');
```

@iSarahSajjad Please read the API docs before you rising the issue!
Your solution is there https://phpspreadsheet.readthedocs.io/en/develop/topics/recipes/#formatting-cells

$event->sheet->styleCells(
                   'C7:G7',
    [
        'alignment' => [
               'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
        ],                        
       'font' => [
                'name' => 'Century Gothic',
                'size' => 11,
                'bold' => true,
                'color' => ['argb' => 'EB2B02'],
         ]
      ]
);

$event->sheet->getStyle('C7:G7')->getFill()
          ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
          ->getStartColor()->setARGB('FFFF0000');

what if i want it for just one cell ?
not 'C7:G7' but 'C7' only

As per documentation, you can do it by following:

$spreadsheet->getActiveSheet()->getStyle('B2')
->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_RED);

Please refer the docs here:
https://phpspreadsheet.readthedocs.io/en/latest/topics/recipes/#conditional-formatting-a-cell

Was this page helpful?
0 / 5 - 0 ratings