使用APACHE POI在Excel中使用的条件格式

时间:2014-03-04 09:52:51

标签: java excel apache-poi

我在使用SheetConditionalFormatting时遇到问题,仅用于测试单元格是否包含特定字符串(在我的情况下只是“test”)我运行以下代码:

    SheetConditionalFormatting sheetConditionalFormatting = excelSheet.getSheetConditionalFormatting();

    ConditionalFormattingRule rule = sheetConditionalFormatting.createConditionalFormattingRule(ComparisonOperator.EQUAL, "test");
    PatternFormatting fill1 = rule.createPatternFormatting();
    fill1.setFillBackgroundColor(IndexedColors.BLUE.index);
    fill1.setFillPattern(PatternFormatting.SOLID_FOREGROUND);

    CellRangeAddress[] regions = {
            CellRangeAddress.valueOf("A1")
    };

    sheetConditionalFormatting.addConditionalFormatting(regions, rule);

我得到的消息是工作区中不存在'test'。这是我在Console中的错误:

Exception in thread "main" org.apache.poi.ss.formula.FormulaParseException: Specified named range 'test' does not exist in the current workbook.
    at org.apache.poi.ss.formula.FormulaParser.parseNonRange(FormulaParser.java:569)
    at org.apache.poi.ss.formula.FormulaParser.parseRangeable(FormulaParser.java:429)
    at org.apache.poi.ss.formula.FormulaParser.parseRangeExpression(FormulaParser.java:268)
    at org.apache.poi.ss.formula.FormulaParser.parseSimpleFactor(FormulaParser.java:1119)
    at org.apache.poi.ss.formula.FormulaParser.percentFactor(FormulaParser.java:1079)
    at org.apache.poi.ss.formula.FormulaParser.powerFactor(FormulaParser.java:1066)
    at org.apache.poi.ss.formula.FormulaParser.Term(FormulaParser.java:1426)
    at org.apache.poi.ss.formula.FormulaParser.additiveExpression(FormulaParser.java:1526)
    at org.apache.poi.ss.formula.FormulaParser.concatExpression(FormulaParser.java:1510)
    at org.apache.poi.ss.formula.FormulaParser.comparisonExpression(FormulaParser.java:1467)
    at org.apache.poi.ss.formula.FormulaParser.unionExpression(FormulaParser.java:1447)
    at org.apache.poi.ss.formula.FormulaParser.parse(FormulaParser.java:1568)
    at org.apache.poi.ss.formula.FormulaParser.parse(FormulaParser.java:176)
    at org.apache.poi.hssf.model.HSSFFormulaParser.parse(HSSFFormulaParser.java:70)
    at org.apache.poi.hssf.record.CFRuleRecord.parseFormula(CFRuleRecord.java:525)
    at org.apache.poi.hssf.record.CFRuleRecord.create(CFRuleRecord.java:146)
    at org.apache.poi.hssf.usermodel.HSSFSheetConditionalFormatting.createConditionalFormattingRule(HSSFSheetConditionalFormatting.java:80)
    at org.apache.poi.hssf.usermodel.HSSFSheetConditionalFormatting.createConditionalFormattingRule(HSSFSheetConditionalFormatting.java:32)
    at MainApp.main(MainApp.java:26)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

2 个答案:

答案 0 :(得分:0)

它发现引入createConditionalFormattingRule的字符串必须是单元格坐标

ConditionalFormattingRule rule = sheetConditionalFormatting.createConditionalFormattingRule(ComparisonOperator.EQUAL, "B1");

答案 1 :(得分:0)

要将字符串用作比较,需要将其用引号引起来,例如:

“”“字符串”“”