【发布时间】:2014-08-07 17:47:21
【问题描述】:
以下代码可以完美运行,并且当您更改数据时背景会发生变化:
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 和 @987654326 @。如果我在 excel 中手动删除相等,则它可以完美运行,并且当数据验证列表更改时单元格背景会更改。
现在的新问题是,如何从代码中删除等号?或者将 Apache POI 中的字符串与条件格式进行比较的正确方法是什么?
【问题讨论】:
-
请注意,代码中唯一改变的部分是 11 和 10 表示 ok 和 error,它们都有效,但最后一个不改变背景,这没有任何意义。我猜它与excel中的格式有关,数字和字符串之间有什么不同,但我该如何解决?谢谢
标签: java apache-poi comparison-operators