3

我正在尝试将 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++;
      }
4

1 回答 1

4

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

编辑

请注意,您可以使用 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)
    );
于 2013-09-02T22:30:44.853 回答