以下代码可以完美运行,并且当您更改数据时背景会发生变化:
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
之后插入等号,类似于 =ok
和 =error
。如果我在 Excel 中手动删除等号,它会完美工作,并且当数据验证列表更改时单元格背景也会更改。
现在的新问题是,如何从代码中删除 equal ?或者将 Apache POI 中的字符串与条件格式进行比较的正确方法是什么?
最佳答案
我也没有设法让它像这样工作。但我成功地用公式代替了。将创建“my_rule”变量的代码替换为以下代码:
XSSFConditionalFormattingRule my_rule = my_cond_format_layer.createConditionalFormattingRule("C1=\"OK\"");
实际的公式是 C1="OK",所以我在创建公式字符串时必须转义引号。请注意,我使用的“C1”单元格是您指定的范围的第一个单元格 (C1:C90)。应用范围时,公式将自动针对每个单元格进行适当调整,因此上述内容应适用于范围内的所有单元格。
您可以对“ERROR”字符串执行相同的操作。
关于java - 如何比较条件格式中的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24268477/