我正在尝试将 PHPExcel 条件格式应用于 B 列中值低于 50 的所有单元格。我从开发人员文档中获取了示例,但到目前为止我无法使其工作。有任何想法吗?
这是有条件的:
$objConditional1 = new PHPExcel_Style_Conditional();
$objConditional1->setConditionType(PHPExcel_Style_Conditional::CONDITION_CELLIS);
$objConditional1->setOperatorType(PHPExcel_Style_Conditional::OPERATOR_LESSTHAN);
$objConditional1->addCondition('50');
$objConditional1->getStyle()->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_RED);
$objConditional1->getStyle()->getFont()->setBold(true);
这是循环:
$rowCount = 3;
foreach ($biocount as $value) {
$objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowCount, $value);
$objPHPExcel->getActiveSheet()->getStyle('B'. $rowCount)->getFont()->setSize(14);
$objPHPExcel->getActiveSheet()->getStyle('B'. $rowCount)->setConditionalStyles($objConditional1);
$rowCount++;
}
最佳答案
setConditionalStyles() 需要一个条件样式数组,而不是单个条件样式对象
/**
* Set Conditional Styles. Only used on supervisor.
*
* @param PHPExcel_Style_Conditional[] $pValue Array of condtional styles
* @return PHPExcel_Style
*/
例如
$rowCount = 3;
foreach ($data as $value) {
$objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowCount, $value);
$objPHPExcel->getActiveSheet()->getStyle('B'. $rowCount)
->getFont()->setSize(14);
$objPHPExcel->getActiveSheet()->getStyle('B'. $rowCount)
->setConditionalStyles(array($objConditional1));
$rowCount++;
}
编辑
请注意,与为循环中的每个单元格设置条件样式的开销不同,您可以使用重复条件样式()方法在完成循环后一步将样式应用于一系列单元格
$rowCount = 3;
foreach ($data as $value) {
$objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowCount, $value);
$objPHPExcel->getActiveSheet()->getStyle('B'. $rowCount)
->getFont()->setSize(14);
$rowCount++;
}
$objPHPExcel->getActiveSheet()
->duplicateConditionalStyle(
array($objConditional1),
'B3:B' .($rowCount - 1)
);
关于PHPExcel 条件格式在循环内,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18581458/