简体   繁体   English

PHPExcel 如何设置条件格式以根据单元格值更改单元格背景颜色

[英]PHPExcel How to set conditional formatting to change cell background color based on cells values

Good morning,早上好,

I need to set cells background colors using PHPExcel, but I don't know how to do it.我需要使用 PHPExcel 设置单元格背景颜色,但我不知道该怎么做。

This is for my header and It's working:这是我的标题,它正在工作:

$styleArray = array(
    'font' => array(
        'bold' => true,
    ),
    'alignment' => array(
        'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
    ),
    'borders' => array(
        'allborders' => array(
            'style' => PHPExcel_Style_Border::BORDER_THIN,
        )
    )
);

$objPHPExcel->getActiveSheet()->getStyle('A1:F1')->applyFromArray($styleArray);

Now I'd like to set cells bg colors in this way:现在我想以这种方式设置单元格背景颜色:

Columns J:Q, if cell's value is "OK", bg color is green; J:Q列,如果单元格值为“OK”,bg颜色为绿色; if it's "NO", bg color is red.如果为“NO”,则 bg 颜色为红色。

Thanks谢谢

function cellColor($col, $row){
    global $objPHPExcel;

    $cell = $col.$row;
    $cellValue = $objWorksheet->getCellByColumnAndRow($col, $row)->getValue();

    $color = 'ffffff';
    if($cellValue == 'ok')
       $color = 'ff0000';
    if($cellValue == 'no')
       $color = '30a30a';

    $objPHPExcel->getActiveSheet()->getStyle($cell)->getFill()->applyFromArray(array(
        'type' => PHPExcel_Style_Fill::FILL_SOLID,
        'startcolor' => array(
             'rgb' => $color
        )
    ));
}

For example, you can use like this: 例如,您可以这样使用:

cellColor('B', '5');

I know this answer is pretty late, but I don't think the question is answered correctly here. 我知道这个答案还很晚,但是我认为这里的问题没有得到正确回答。 This has to be done with conditional formatting because if the cell value changes after PHP generates the excel the color will not change. 这必须使用条件格式完成,因为如果在PHP生成excel后单元格值发生更改,则颜色不会更改。 ie: Excel handles the logic for the formatting. 即:Excel处理格式的逻辑。 I kept trying to get this to work and apparently getEndColor()->setRGB() has to be used and not getStartColor(). 我一直试图使它工作,显然必须使用getEndColor()-> setRGB()而不是getStartColor()。 See example below: 请参见下面的示例:

 $conditional1 = new \PhpOffice\PhpSpreadsheet\Style\Conditional();
    $conditional1->setConditionType(\PhpOffice\PhpSpreadsheet\Style\Conditional::CONDITION_CONTAINSTEXT);
    $conditional1->setOperatorType(\PhpOffice\PhpSpreadsheet\Style\Conditional::OPERATOR_CONTAINSTEXT);
    $conditional1->setText('No');
    $conditional1->getStyle()->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_RED);
    $conditional1->getStyle()->getFont()->setBold(true);
    $conditional1->getStyle()->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID);

    $conditional1->getStyle()->getFill()->getEndColor()->setRGB('FFC7CE');
    $conditionalStyles[] = $conditional1;

    $spreadsheet->getActiveSheet()->getStyle('E4')->setConditionalStyles($conditionalStyles);

https://phpoffice.github.io/PhpSpreadsheet/classes/PhpOffice-PhpSpreadsheet-Style-Conditional.html https://phpoffice.github.io/PhpSpreadsheet/classes/PhpOffice-PhpSpreadsheet-Style-Conditional.html

https://phpspreadsheet.readthedocs.io/en/latest/topics/recipes/#formatting-cells https://phpspreadsheet.readthedocs.io/en/latest/topics/recipes/#formatting-cells

$conditional1 = new \PhpOffice\PhpSpreadsheet\Style\Conditional();
                    $conditional1->setConditionType(\PhpOffice\PhpSpreadsheet\Style\Conditional::OPERATOR_BEGINSWITH);
                    $conditional1->setOperatorType(\PhpOffice\PhpSpreadsheet\Style\Conditional::OPERATOR_EQUAL);
                    $conditional1->addCondition('2');
                    $conditional1->getStyle()->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_RED);
                    $conditional1->getStyle()->getFont()->setBold(true);
    
                    $conditionalStyles = $spreadsheet->getActiveSheet()->getStyle('E2')->getConditionalStyles();
                    $conditionalStyles[] = $conditional1;
    
    $spreadsheet->getActiveSheet()->getStyle('D2')->setConditionalStyles($conditionalStyles);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM