Excel sheet corrupted after conditional formatting

泄露秘密 提交于 2019-12-10 18:43:56

问题


I am generating an excel sheet with phpexcel, when using conditional formatting with the condition being (search for a text or part of it), i get a validation error when trying to open the generated sheet. Works perfectly with numbers, Less so WITH TEXTS.

Here is my code :

 //conditional formatting
  $objConditional1 = new PHPExcel_Style_Conditional();
  $objConditional1->setConditionType(PHPExcel_Style_Conditional::CONDITION_CONTAINSTEXT)
            ->setOperatorType(PHPExcel_Style_Conditional::OPERATOR_CONTAINSTEXT)
            ->addCondition('X');
 $objConditional1->getStyle()->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_YELLOW);

  $objConditional3 = new PHPExcel_Style_Conditional();
  $objConditional3->setConditionType(PHPExcel_Style_Conditional::CONDITION_CELLIS)
            ->setOperatorType(PHPExcel_Style_Conditional::OPERATOR_GREATERTHANOREQUAL)
            ->addCondition('0');
  $objConditional3->getStyle()->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_GREEN);


$conditionalStyles = $objPHPExcel->getActiveSheet()->getStyle('B2')->getConditionalStyles();
array_push($conditionalStyles, $objConditional3);
array_push($conditionalStyles, $objConditional1);
$objPHPExcel->getActiveSheet()->getStyle('B2')->setConditionalStyles($conditionalStyles);

Does any one know how to work around this ? am i doing anything wrong ? Thanks.


回答1:


As far as i know, though i have not tested with your case specifically, when you try to apply conditional formatting based on CONTAINSTEXT, you shouldn't call

'->addCondition('X')' 

(line 5 in your code), but instead call the method:

->setText('X')

On your PHPExcel_Style_Conditional objects. This is how I specify the text to compare to in my PHPExcel sheets.



来源:https://stackoverflow.com/questions/18441783/excel-sheet-corrupted-after-conditional-formatting

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!