PHPEXCEL导出表格数据时,如何对多个指定的单元格指填充背景颜色?PHP代码这样写,请下下面示例,注意调用的顺序。
function cellColor($cells,$color){ global $objActSheet; $objActSheet->getStyle($cells)->getFill()->applyFromArray( array( 'type' => PHPExcel_Style_Fill::FILL_SOLID, 'startcolor' => array( 'rgb' => $color ) ) ); } $bg_danyuange=''; $objExcel = new PHPExcel(); $objWriter = new PHPExcel_Writer_Excel5($objExcel); $objExcel->setActiveSheetIndex(0); $objActSheet = $objExcel->getActiveSheet(); $objActSheet->setTitle(iconv('gbk','utf-8','Sheet1'));//表名称 //$bg_danyuange=$bg_danyuange.PHPExcel_Cell::stringFromColumnIndex($max_lie).$hangshu.",";//通过计算 $bg_danyuange=$bg_danyuange."A2,";//也可以直接指定单元格 //单元格填充背景颜色,这段代码一定要放在循环输出数据到单元络结束之后,意思就是数据都输出完不动数据了,再设置颜色,要不然只要数据修改了,颜色就失效了 if (trim($bg_danyuange,",")!=''){ $bg_danyuange=trim($bg_danyuange,","); $bg_danyuange_array=explode(',',$bg_danyuange); for ($b=0;$b<count($bg_danyuange_array);$b++){ cellColor($bg_danyuange_array[$b], 'FF0000'); } }