【问题标题】:How compare strings in ConditionalFormatting?如何比较条件格式中的字符串?
【发布时间】: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 在字符串 okerror 之后插入了一个等号,类似于 =ok 和 @987654326 @。如果我在 excel 中手动删除相等,则它可以完美运行,并且当数据验证列表更改时单元格背景会更改。

现在的新问题是,如何从代码中删除等号?或者将 Apache POI 中的字符串与条件格式进行比较的正确方法是什么?

【问题讨论】:

  • 请注意,代码中唯一改变的部分是 11 和 10 表示 ok 和 error,它们都有效,但最后一个不改变背景,这没有任何意义。我猜它与excel中的格式有关,数字和字符串之间有什么不同,但我该如何解决?谢谢

标签: java apache-poi comparison-operators


【解决方案1】:

我找到了解决方案。它包括将文本放在一个单元格中,一个单元格表示正常,另一个单元格表示错误,我将字体设置为白色,因此它不会出现,最后我将两个单元格与我想要更改背景的单元格进行比较,它是唯一的我发现使它适用于字符串比较的方法。代码是:

    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】:

    我也没有设法让它像这样工作。但我成功地使用了一个公式。将创建“my_rule”变量的代码替换为以下代码:

    XSSFConditionalFormattingRule my_rule = my_cond_format_layer.createConditionalFormattingRule("C1=\"OK\"");
    

    实际的公式是 C1="OK" 所以我必须在创建公式字符串时转义引号。请注意,我使用了“C1”单元格,它是您指定范围的第一个单元格 (C1:C90)。应用范围时,公式将自动适用于每个单元格,因此上述公式适用于范围内的所有单元格。

    你可以对“ERROR”字符串做同样的事情。

    【讨论】:

      【解决方案3】:

      我找到了一个非常适合我的解决方案:

      ConditionalFormattingRule rule = sheetCF.createConditionalFormattingRule( ComparisonOperator.EQUAL, "\"" + compareItem + "\"" );
      

      请注意,您必须将compareItem(要与之比较的字符串)用额外的双引号括起来

      【讨论】:

        猜你喜欢
        • 2018-01-16
        • 2016-05-02
        • 2022-12-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-24
        • 2020-09-11
        • 1970-01-01
        相关资源
        最近更新 更多