PhpSpreadsheet set background color of cell to white

Premlatha picture Premlatha · Mar 6, 2019 · Viewed 20.8k times · Source

Using PhpSpreadsheet, I want to set a white background to the excel cell.

$cells = 'A1';
$spreadsheet
    ->getActiveSheet()
    ->getStyle($cells)
    ->getFill()
    ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
    ->getStartColor(' #FFFFFF')
    ->setARGB('#FFFFFF');

This code makes the cell background black even if I set this white RGB color value: #FFFFFF.

The result I would like to achieve:

BEFORE

AFTER

Answer

Premlatha picture Premlatha · Mar 11, 2019

You don't have to include the # symbol when you specify the ARGB for PhpSpreadsheet. These solutions will be able to set the cell background to white:

Cell by cell

$spreadsheet
    ->getActiveSheet()
    ->getStyle($cells)
    ->getFill()
    ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
    ->getStartColor()
    ->setARGB('ffffff');

Range of cells

$spreadsheet
    ->getActiveSheet()
    ->getStyle('A1:A5')
    ->getFill()
    ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
    ->getStartColor()
    ->setARGB('ffffff');