Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change cell color in PHPSpreadsheet

Are you able to change the color of a cell in a .xlsx file with PHPspreadsheet? Couldn't find anything in their function list or on Stack Overflow.

like image 687
jdwee Avatar asked Feb 09 '18 19:02

jdwee


2 Answers

For those who understand "change cell color" as Background color and not Text color, the right code is:

$spreadsheet->getActiveSheet()->getStyle('[YOUR_CELL_OR_RANGE_HERE]')->getFill()
    ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
    ->getStartColor()->setARGB('YOUR_COLOR_CODE_HERE');

Example:

$spreadsheet->getActiveSheet()->getStyle('E2')->getFill()
    ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
    ->getStartColor()->setARGB('FFFF0000');
like image 181
Mannyfatboy Avatar answered Oct 31 '22 16:10

Mannyfatboy


Tries to look this link Here

$spreadsheet->getActiveSheet()->getStyle('B2')
->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_RED);
like image 24
Natan Augusto Avatar answered Oct 31 '22 15:10

Natan Augusto