在PHPExcel中设置背景单元格颜色

在PHPExcel中创buildXLS文档时如何将特定颜色设置为活动单元格?

$sheet->getStyle('A1')->applyFromArray( array( 'fill' => array( 'type' => PHPExcel_Style_Fill::FILL_SOLID, 'color' => array('rgb' => 'FF0000') ) ) ); 

资料来源: http : //bayu.freelancer.web.id/2010/07/16/phpexcel-advanced-read-write-excel-made-simple/

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

在这里输入图像说明

这个代码应该适合你:

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

但是如果你反复使用这个,我推荐使用applyFromArray

好像有一个错误,现在applyFromArray不会接受颜色,但这对我applyFromArray

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

这总是运行!

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

 $objPHPExcel ->getActiveSheet() ->getStyle('A1') ->getFill() ->setFillType(PHPExcel_Style_Fill::FILL_SOLID) ->getStartColor() ->setRGB('colorcode'); //ie,colorcode=D3D3D3 
 $objPHPExcel ->getActiveSheet() ->getStyle('A1') ->getFill() ->getStartColor() ->getRGB();