简体   繁体   中英

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.

Tries to look this link Here

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

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

Tries to look this link

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM