Conditional formatting using in Excel using APACHE

2019-08-19 03:59发布

I have a problem using the SheetConditionalFormatting, just for testing if the cell contains particular string (in my case just "test") I run the following code:

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

And I got message that 'test' does not exist in the workspace. This is my error from 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)

1条回答
淡お忘
2楼-- · 2019-08-19 04:53

it found out that string introduced into the createConditionalFormattingRule must be cell coordinates

ConditionalFormattingRule rule = sheetConditionalFormatting.createConditionalFormattingRule(ComparisonOperator.EQUAL, "B1");
查看更多
登录 后发表回答