内部循环中的PHPExcel条件格式

时间:2013-09-02 22:16:42

标签: php formatting phpexcel

我正在尝试将PHPExcel条件格式应用于columm 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++;
      }

1 个答案:

答案 0 :(得分:4)

setConditionalStyles()需要一个条件样式数组,而不是一个条件样式对象

/**
 * Set Conditional Styles. Only used on supervisor.
 *
 * @param PHPExcel_Style_Conditional[] $pValue Array of condtional styles
 * @return PHPExcel_Style
 */

e.g。

$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++;
}

修改

请注意,您可以使用duplicateConditionalStyle()方法在完成后在一个步骤中将样式应用于一系列单元格,而不是为循环中的每个单独单元格设置条件样式。循环

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