How to set cell color in phpexcel?

by wilmer.lemke , in category: PHP General , a year ago

How to set cell color in phpexcel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by tressie.damore , a year ago

@wilmer.lemke 

To set cell color in PHPExcel, you can use the following code:

  1. Load the PHPExcel library:
1
require 'PHPExcel/Classes/PHPExcel.php';


  1. Create a new PHPExcel object:
1
$objPHPExcel = new PHPExcel();


  1. Set the cell color for a specific cell:
1
2
$objPHPExcel->getActiveSheet()->getStyle('A1')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
$objPHPExcel->getActiveSheet()->getStyle('A1')->getFill()->getStartColor()->setRGB('FF0000');


In the above example, we set the color of cell 'A1' to red.

  1. Save the Excel file:
1
2
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('test.xlsx');


Note: Make sure you have included the PHPExcel library in your project and have the appropriate write permissions for the directory where you are saving the Excel file.