2013-05-20 38 views
2

我在我的servlet中设置了以下代码,以基于字符串值的 对列进行格式化,但是在尝试编译时出现错误(org.apache.poi.ss .formula.FormulaParseException:指定的命名范围'green'在当前工作簿中不存在)。我应该如何测试字符串值?Apache POI 3.9条件格式(字符串值)

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

使用更新的一个...它的工作,当你申请无论是单独

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); 
2

我有这个问题,并通过对字符串加双引号,让“绿色”成为“解决它的规则\“绿色\””。

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

希望工程。