Apache POI 3.9条件格式(字符串值)

时间:2013-05-20 13:36:19

标签: apache-poi conditional-formatting

我在servlet中设置了以下代码来格式化列 在字符串值上但是,我在尝试编译时遇到错误(org.apache.poi.ss.formula.FormulaParseException:指定的命名范围'green'在当前工作簿中不存在。)。我该如何测试字符串值?

SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();

    // Condition 1: Cell Value is equal to green (Green Fill)
    ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule(ComparisonOperator.EQUAL, "green");
    PatternFormatting fill1 = rule1.createPatternFormatting();
    fill1.setFillBackgroundColor(IndexedColors.GREEN.index);
    fill1.setFillPattern(PatternFormatting.SOLID_FOREGROUND);

    // Condition 2: Cell Value Is  equal to yellow   (Yellow Fill)
    ConditionalFormattingRule rule2 = sheetCF.createConditionalFormattingRule(ComparisonOperator.EQUAL, "yellow");
    PatternFormatting fill2 = rule2.createPatternFormatting();
    fill2.setFillBackgroundColor(IndexedColors.YELLOW.index);
    fill2.setFillPattern(PatternFormatting.SOLID_FOREGROUND);


    CellRangeAddress[] regions = {
            CellRangeAddress.valueOf("B1:B44")
    };

    sheetCF.addConditionalFormatting(regions, rule1, rule2);

2 个答案:

答案 0 :(得分:3)

我遇到了这个问题并通过在字符串上添加双引号来解决它,以便"绿色"变成" \"绿色\""。

ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule(ComparisonOperator.EQUAL, "\"green\"");

希望它有效。

答案 1 :(得分:2)

使用更新的...当你单独应用两个规则时它的工作

 SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();

// Condition 1: Cell Value is equal to green (Green Fill)
ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule(ComparisonOperator.EQUAL, "green");
PatternFormatting fill1 = rule1.createPatternFormatting();
fill1.setFillBackgroundColor(IndexedColors.GREEN.index);
fill1.setFillPattern(PatternFormatting.SOLID_FOREGROUND);
CellRangeAddress[] regions = {CellRangeAddress.valueOf("B1:B44")};
sheetCF.addConditionalFormatting(regions, rule1);

// Condition 2: Cell Value Is  equal to yellow   (Yellow Fill)
ConditionalFormattingRule rule2 = sheetCF.createConditionalFormattingRule(ComparisonOperator.EQUAL, "yellow");
PatternFormatting fill2 = rule2.createPatternFormatting();
fill2.setFillBackgroundColor(IndexedColors.YELLOW.index);
fill2.setFillPattern(PatternFormatting.SOLID_FOREGROUND);
sheetCF.addConditionalFormatting(regions, rule2);