How to find the background fill color of a cell?

Topics: User Forum
Jul 22, 2010 at 7:37 PM
$excel->getActiveSheet()->getStyleByColumnAndRow($columnIndex, $rowIndex)->getFill() returns object, not a string. Any suggestion? Thanks
Jul 23, 2010 at 4:57 AM
Edited Jul 23, 2010 at 5:00 AM

Hi,

I needed to find a way to do the same thing.  I came across a issue here that shows how to output the RGB code:

http://phpexcel.codeplex.com/workitem/7033?ProjectName=phpexcel

Code:

$reader = new PHPExcel_Reader_Excel2007();
$phpExcel = $reader->load('bg-test.xlsx');
echo 'A1: ' . $phpExcel->getActiveSheet()->getStyle('A1')->getFill()->getStartColor()->getRGB() . '<br>';
echo 'B1: ' . $phpExcel->getActiveSheet()->getStyle('B1')->getFill()->getStartColor()->getRGB() . '<br>';


 

 

 

Jul 23, 2010 at 7:24 PM
Ah... interesting one. However, even if the cell background is colored green or blue, the returned value is always 000000 (black) or FF000000 if using getARGB() Perhaps the developers could explain this anomaly? Thanks
Coordinator
Jul 25, 2010 at 1:03 PM
gbudiman wrote:
Ah... interesting one. However, even if the cell background is colored green or blue, the returned value is always 000000 (black) or FF000000 if using getARGB() Perhaps the developers could explain this anomaly? Thanks

 Not without some testing ourselves. This should return the cell background colours correctly

Jul 26, 2010 at 8:07 PM
Edited Jul 26, 2010 at 8:09 PM
Okay, this is the code that I used to 'read' the background color $excelSaveDirectory = {PATH_TO_FILE}; $excel = PHPExcel_IOFactory::load($excelSaveDirectory); $excel->setActiveSheetIndex(0); for ($i = 0; $i < 12; $i++) { echo $excel->getActiveSheet()->getCellByColumnAndRow($i, 2)->getValue() .' - '; echo $excel->getActiveSheet()->getStyleByColumnAndRow($i, 2)->getFill()->getStartColor()->getRGB() .' - '; echo $excel->getActiveSheet()->getStyleByColumnAndRow($i, 2)->getFill()->getEndColor()->getRGB() .' - '; echo '
'; } And this is the result IRD Serial - FFFFFF - FFFFFF - Entry Date - FFFFFF - FFFFFF - HDD Serial - FFFFFF - FFFFFF - Model Serial - FFFFFF - FFFFFF - Activities - FFFFFF - FFFFFF - Attachments - FFFFFF - FFFFFF - BDF - FFFFFF - FFFFFF - BDG - 000000 - FFFFFF - BDH - FFFFFF - FFFFFF - ##ACB - FFFFFF - FFFFFF - ##ACC - 000000 - FFFFFF - AY - FFFFFF - FFFFFF - File attached at http://phpexcel.codeplex.com/Project/Download/AttachmentDownload.ashx?ProjectName=PHPExcel&WorkItemId=13790&FileAttachmentId=4680 P.S. OMG How do I apply formatting and code block and newline and stuff?
Jul 26, 2010 at 8:15 PM
Edited Jul 26, 2010 at 8:19 PM
EDIT: IGNORE THIS POST. The post below is made after another anomaly. Doing the following still produce incorrect background color result. Additional details: The anomaly above occurs only when I use the 'Fill Color' in the 'Font' ribbon (I'm using Excel 2007). If I right click on a cell and manually choose background color under the tab 'Fill', the value returned is correct. Thanks