Set background cell color in PHPExcel

How to set specific color for active cell when creating XLS document in PHPExcel?

+80
phpexcel
Jul 21 '11 at 8:32
source share
8 answers
$sheet->getStyle('A1')->applyFromArray( array( 'fill' => array( 'type' => PHPExcel_Style_Fill::FILL_SOLID, 'color' => array('rgb' => 'FF0000') ) ) ); 

Source: http://bayu.freelancer.web.id/2010/07/16/phpexcel-advanced-read-write-excel-made-simple/

+138
Jul 21 2018-11-21T00:
source share
 function cellColor($cells,$color){ global $objPHPExcel; $objPHPExcel->getActiveSheet()->getStyle($cells)->getFill()->applyFromArray(array( 'type' => PHPExcel_Style_Fill::FILL_SOLID, 'startcolor' => array( 'rgb' => $color ) )); } cellColor('B5', 'F28A8C'); cellColor('G5', 'F28A8C'); cellColor('A7:I7', 'F28A8C'); cellColor('A17:I17', 'F28A8C'); cellColor('A30:Z30', 'F28A8C'); 

enter image description here

+76
Jul 19 '13 at 1:41 on
source share

This code should work for you:

  $PHPExcel->getActiveSheet() ->getStyle('A1') ->getFill() ->setFillType(PHPExcel_Style_Fill::FILL_SOLID) ->getStartColor() ->setRGB('FF0000') 

But if you keep using it over and over again, I recommend using applyFromArray .

+27
Jul 02 '15 at 7:50
source share

There seems to be a bug with applyFromArray right now that won't take color, but this worked for me:

 $objPHPExcel ->getActiveSheet() ->getStyle('A1') ->getFill() ->getStartColor() ->setRGB('FF0000'); 
+8
Apr 24 '13 at 18:47
source share

It always works!

$sheet->getActiveSheet()->getStyle('A1')->getFill()->getStartColor()->setRGB('FF0000');

+8
Jul 20 '14 at 13:35
source share
 $objPHPExcel ->getActiveSheet() ->getStyle('A1') ->getFill() ->setFillType(PHPExcel_Style_Fill::FILL_SOLID) ->getStartColor() ->setRGB('colorcode'); //ie,colorcode=D3D3D3 
+6
Jul 11 '17 at 11:17
source share
 $objPHPExcel ->getActiveSheet() ->getStyle('A1') ->getFill() ->getStartColor() ->getRGB(); 
+1
Apr 05 2018-12-12T00:
source share
  $objPHPExcel->getActiveSheet()->getStyle('B3:B7')->getFill() ->setFillType(PHPExcel_Style_Fill::FILL_SOLID) ->getStartColor()->setARGB('FFFF0000'); 

This is in the documentation located here: https://github.com/PHPOffice/PHPExcel/wiki/User-Documentation-Overview-and-Quickstart-Guide

-one
Jan 27 '19 at 4:53 on
source share



All Articles