如何将单元格phpexcel的颜色设置为foreach循环 [英] How to set color for cell phpexcel into foreach loop
问题描述
现在,我想将phpexcel中单元格的文本颜色设置为foreach循环. foreach循环类似于:
Now I want set text color for cell in phpexcel into foreach loop. The foreach loop is something like :
$redBold = array(
"font" => array(
"bold" => true,
"color" => array("rgb" => "FF0000"),
),
);
$row = 5;
$count = 0
foreach ($data as $key => $value) {
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($count++, $row, $value['type']?$value['type']:0);
if ($value['type'] == 1) {
$objPHPExcel->getActiveSheet()->getStyle($count . $row)->applyFromArray($redBold);
}
}
该代码无法理解getStyle($count . $row )
,因为$count . $row
应该为A6
...在这种情况下是否可以设置文本?请帮忙!
this code is not understand getStyle($count . $row )
because $count . $row
should be A6
... Is there way to set text in this case? Please help!
推荐答案
连接$count . $row
会得到什么?
getStyle()
需要一个单元格引用(例如A1
,C3
,IV256
)或一个单元格范围(例如A1:C3
,B2:D4
,A2:IV256
等).
getStyle()
expects either a cell reference (e.g. A1
, C3
, IV256
) or a cell range (e.g. A1:C3
, B2:D4
, A2:IV256
etc.
您只是将两个数字连接起来,例如0
和5
给出05
,对于单元格引用/范围而言,这是毫无意义的
You're simply concatenating two numbers, e.g. 0
and 5
to give 05
which is meaningless in terms of cell references/ranges
在连接之前,您需要将$count
(用作列索引)转换为实际的列地址
You need to convert $count
(which you're using as a column index, to an actual column address before concatenating
$objPHPExcel->getActiveSheet()->getStyle(PHPExcel_Cell::stringFromColumnIndex($count) . $row)->applyFromArray($redBold);
还请注意,您尝试使用后递增运算符之前来递增列,从而尝试设置单元格的样式,因此它可能不会为您提供您所需要的单元格引用想要
Note also that you're incrementing the column using the post-increment operator before trying to set the style for the cell, so it probably isn't going to give you the cell reference that you want
这篇关于如何将单元格phpexcel的颜色设置为foreach循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!