apache poi条件格式化空白单元格

时间:2016-01-29 16:12:56

标签: java apache-poi

我正在尝试使用黄色背景格式化低于5的单元格,并且此范围内的所有空白单元格也会突出显示。尝试使用null“”和“\”\“”的平等规则,但这些都不起作用。

通过将GT 5设置为第2规则进行测试,将红色设置为dos似乎工作的颜色。

空白字段条件格式是可以在excel中完成的事情,到目前为止我还没有设法找到任何与apache-poi相关的东西。

不知道是否有人设法让它工作(这是在groovy所以区域可能看起来不同)

SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting()
        ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule(ComparisonOperator.LT, "5")
        PatternFormatting fill1 = rule1.createPatternFormatting()
        fill1.setFillBackgroundColor(IndexedColors.YELLOW.index)
        fill1.setFillPattern(PatternFormatting.SOLID_FOREGROUND)
        //try and set the fields that are blank back to white cells - is not working from below unsure if it will work
        //https://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/SheetConditionalFormatting.html
        ConditionalFormattingRule rule2 = sheetCF.createConditionalFormattingRule(ComparisonOperator.EQUAL,"\"\"")
        PatternFormatting fill2 = rule2.createPatternFormatting()
        fill2.setFillBackgroundColor(IndexedColors.WHITE.index)
        fill2.setFillPattern(PatternFormatting.SOLID_FOREGROUND)
        CellRangeAddress[] regions = [ CellRangeAddress.valueOf("M11:T${counter}") ].toArray()

2 个答案:

答案 0 :(得分:2)

上述答案非常局限于您设置公式的字段。昨晚我正在考虑这个问题并尝试了一些与空白单元格不同的东西,但是单元格必须设置为“”才能正常工作。

                def results= parseTotals(inputList)
                results.eachWithIndex{tr,ii ->
                    //11 is where it starts numeric on my report
                    ii=ii+11
                    Cell cell = row.createCell(ii)
                    if (tr) {
                        cell.setCellValue(tr as Double)
                        cell.setCellStyle(twoDecimal)
                    }else {

                        //cell.setCellValue(Cell.CELL_TYPE_BLANK)  //does not work -- below works and matches "\"\"" rule below
                        cell.setCellValue("")
                    }
                }

                SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting()
                ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule(ComparisonOperator.LT, "5")
                PatternFormatting fill1 = rule1.createPatternFormatting()
                fill1.setFillBackgroundColor(IndexedColors.YELLOW.index)
                fill1.setFillPattern(PatternFormatting.SOLID_FOREGROUND)
                ConditionalFormattingRule rule2 = sheetCF.createConditionalFormattingRule(ComparisonOperator.EQUAL,"\"\"")

                PatternFormatting fill2 = rule2.createPatternFormatting()
                fill2.setFillBackgroundColor(IndexedColors.RED.index)
                fill2.setFillPattern(PatternFormatting.SOLID_FOREGROUND)
                CellRangeAddress[] regions = [ CellRangeAddress.valueOf("M11:T${counter}") ].toArray()
                sheetCF.addConditionalFormatting(regions,rule1,rule2)

尽管如此,我认为我将回归到通过设置样式的实用方式(因为它目前将样式设置为数字字段)但更确切地说,每行的结果大小不同而且我不喜欢总是得到相同的范围来放入“”作为一个领域。

答案 1 :(得分:1)

好吧,对于空白,我使用了一个公式,其值为ISBLANK()。该公式需要你的范围的第一个单元格,在我的情况下A1,可能在你的情况下是M11。此外,规则的顺序似乎很重要,我必须在添加条件格式时首先放置空白规则。

coplot()
相关问题