更改数据时,以下代码可以正常工作并更改背景:
constraint = validationHelper.createExplicitListConstraint(new String[]{"10","11"});
XSSFSheetConditionalFormatting my_cond_format_layer = sheet.getSheetConditionalFormatting();
XSSFConditionalFormattingRule my_rule = my_cond_format_layer.createConditionalFormattingRule(ComparisonOperator.EQUAL,"10");
PatternFormatting fill1 = my_rule.createPatternFormatting();
fill1.setFillBackgroundColor(IndexedColors.GREEN.index);
fill1.setFillPattern(PatternFormatting.SOLID_FOREGROUND);
CellRangeAddress[] my_data_range = {CellRangeAddress.valueOf("C1:C90")};
my_cond_format_layer.addConditionalFormatting(my_data_range,my_rule);
XSSFSheetConditionalFormatting my_cond_format_layer2 = sheet.getSheetConditionalFormatting();
XSSFConditionalFormattingRule my_rule2 = my_cond_format_layer2.createConditionalFormattingRule(ComparisonOperator.EQUAL,"11");
PatternFormatting fill12 = my_rule2.createPatternFormatting();
fill12.setFillBackgroundColor(IndexedColors.RED.index);
fill12.setFillPattern(PatternFormatting.SOLID_FOREGROUND);
CellRangeAddress[] my_data_range2 = {CellRangeAddress.valueOf("C1:C90")};
my_cond_format_layer.addConditionalFormatting(my_data_range2,my_rule2);
虽然以下操作不起作用且背景不会改变:
constraint = validationHelper.createExplicitListConstraint(new String[]{"OK","ERROR"});
XSSFSheetConditionalFormatting my_cond_format_layer = sheet.getSheetConditionalFormatting();
XSSFConditionalFormattingRule my_rule = my_cond_format_layer.createConditionalFormattingRule(ComparisonOperator.EQUAL,"OK");
PatternFormatting fill1 = my_rule.createPatternFormatting();
fill1.setFillBackgroundColor(IndexedColors.GREEN.index);
fill1.setFillPattern(PatternFormatting.SOLID_FOREGROUND);
CellRangeAddress[] my_data_range = {CellRangeAddress.valueOf("C1:C90")};
my_cond_format_layer.addConditionalFormatting(my_data_range,my_rule);
XSSFSheetConditionalFormatting my_cond_format_layer2 = sheet.getSheetConditionalFormatting();
XSSFConditionalFormattingRule my_rule2 = my_cond_format_layer2.createConditionalFormattingRule(ComparisonOperator.EQUAL,"ERROR");
PatternFormatting fill12 = my_rule2.createPatternFormatting();
fill12.setFillBackgroundColor(IndexedColors.RED.index);
fill12.setFillPattern(PatternFormatting.SOLID_FOREGROUND);
CellRangeAddress[] my_data_range2 = {CellRangeAddress.valueOf("C1:C90")};
my_cond_format_layer.addConditionalFormatting(my_data_range2,my_rule2);
你能告诉我这里有什么问题吗?我浏览了文档,找不到问题。
修改
最后我发现了问题,当我打开excel并检查条件格式时,我看到POI或Excel在字符串ok
或error
之后插入一个相等的内容,类似于此{{ 1}}和=ok
。如果我在excel中手动删除等值,它将完美地工作,并且当数据验证列表更改时,单元格背景会更改。
现在新问题是,如何从代码中删除等号?或者将Apache POI中的字符串与条件格式进行比较的正确方法是什么?
答案 0 :(得分:2)
我也没办法让它像这样工作。但我成功地使用了一个公式。替换您创建" my_rule"的代码。变量如下:
XSSFConditionalFormattingRule my_rule = my_cond_format_layer.createConditionalFormattingRule("C1=\"OK\"");
实际公式为C1 =" OK"所以我在创建公式String时必须转义引号。请注意,我使用了" C1"单元格,它是您指定范围的第一个单元格(C1:C90)。应用范围时,公式将自动适应每个单元格,因此上述内容适用于该范围内的所有单元格。
您可以使用" ERROR"字符串。
答案 1 :(得分:0)
我找到了解决方案。它包括将文本放在单元格中,一个单元格用于确定,另一个单元格用于错误,我将字体设置为白色,因此它不会出现,最后我将两个单元格与我想要更改背景的单元格进行比较,它是唯一的我发现它使字符串比较的方式。代码是:
constraint=validationHelper.createExplicitListConstraint(new String[]{"Ok","Error","Sin revisar"});
dataValidation = validationHelper.createValidation(constraint, addressList);
dataValidation.setSuppressDropDownArrow(true);
//ERROR
XSSFConditionalFormattingRule regla2 = condicion2.createConditionalFormattingRule(ComparisonOperator.EQUAL,"$C$"+2,"$C$"+7);
PatternFormatting fill12 = regla2.createPatternFormatting();
fill12.setFillBackgroundColor(IndexedColors.RED.index);
fill12.setFillPattern(PatternFormatting.SOLID_FOREGROUND);
CellRangeAddress[] rango2 = {CellRangeAddress.valueOf("C3:C90")};
//OK
XSSFConditionalFormattingRule regla = condicion.createConditionalFormattingRule(ComparisonOperator.EQUAL,"$C$"+1,"$C$"+7);
PatternFormatting fill1 = regla.createPatternFormatting();
fill1.setFillBackgroundColor(IndexedColors.GREEN.index);
fill1.setFillPattern(PatternFormatting.SOLID_FOREGROUND);
CellRangeAddress[] rango = {CellRangeAddress.valueOf("C3:C90")};
答案 2 :(得分:0)
我找到了一个适合我的解决方案:
ConditionalFormattingRule rule = sheetCF.createConditionalFormattingRule( ComparisonOperator.EQUAL, "\"" + compareItem + "\"" );
请注意,您必须将compareItem
(要比较的字符串)与其他双引号括起来