PHPExcel - duplicateStyle() 似乎不起作用/什么都不做

PHPExcel - duplicateStyle() seems not to work / does nothing

我正在为我的公司导出数据,通过 PHP 获取 soma 数据,并将它们输出到 excel 电子表格。

除一件小事外,一切正常。我有条件地格式化一些单元格以具有特定颜色。我既没有收到错误,也没有得到其他单元格的正确背景颜色,它们只是保持白色。

我正在使用 PHPExcel 创建输出,以下是我的代码:

//just for information:
// $spreadsheet = $objPHPExcel->getActiveSheet();

//normal
$conditionalStyleNormal = new PHPExcel_Style_Conditional();
$conditionalStyleNormal->setConditionType(PHPExcel_Style_Conditional::CONDITION_CONTAINSTEXT)
    ->setOperatorType(PHPExcel_Style_Conditional::OPERATOR_CONTAINSTEXT)
    ->setText('Normal (Mittagspause)')
    ->getStyle()->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getEndColor()
    ->setARGB(PHPExcel_Style_Color::COLOR_LIGHTYELLOW);

//apply style
$conditionalStyles = $spreadsheet->getStyle('A5:A50')->getConditionalStyles();
array_push($conditionalStyles, $conditionalStyleNormal);
$spreadsheet->getStyle('A5:I50')->setConditionalStyles($conditionalStyles);

//copy style to other cells (does not work)
$spreadsheet->duplicateStyle($spreadsheet->getStyle('A5'), 'C5:I5');

最后一行对文件没有影响。

PHPExcel 的文档说:

If you want to copy the ruleset to other cells, you can duplicate the style object: $objPHPExcel->getActiveSheet()->duplicateStyle($objPHPExcel->getActiveSheet()->getStyle('B2'), 'B3:B7');

我是不是忽略了什么?或者它只是一个错误?如果是这样,有什么好的解决方法吗?

找不到解决方案,只是发现该函数在早期版本中有错误行为。我正在使用 PHPExcel 1.8.0.

提前致谢!

我现在已经有了自己的小解决方法。我现在没有向单元格添加条件,而是在我的 PHP 脚本中包含了全部内容。我检查有条件的单元格,如果它有特定值,如果有,我给旁边的其他单元格上色。

for ($row=1; $row <= 50; $row++) {
    $cellvalue = $spreadsheet->getCell('A'.$row)->getValue();
    switch ($cellvalue) {
        // a case for each value where the row should be colored,
        // will not list the others, so it uses not as much space
        case 'Frühschicht':
                for ($col="A"; $col <= "I"; $col++) {
                    $spreadsheet->getStyle($col.$row)->getFill()
                        ->setFillType(PHPExcel_Style_Fill::FILL_SOLID)
                        ->getStartColor()->setARGB($lightblue);
                }
            break;

        default:
            break;
    }
}

可能不是最好的解决方案,但对于像这样的小项目,它应该没问题,但我认为可能会更糟 xD

今天早上我遇到了同样的问题,并找到了对我自己来说不那么痛苦的解决方法。 key 点是:在调用 duplicateStyle() 之后,您必须重新设置执行此方法的单元格的值。 例如:



    $sheet = $phpExcel->getActiveSheet();
    <b>key step 1</b>: $style= $sheet->getStyle('D1');
    <b>key step 2</b>: $sheet->duplicateStyle($style,'A5');
    <b>key step 3</b>: $sheet->setCellValue('A5','NIDONGDE');

以上解决方案对我来说效果很好。