2013-05-20 63 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\"");

希望工程。