java - apache poi 条件格式空白单元格

标签 java apache-poi

我试图用黄色背景格式化小于 5 的单元格,这样该范围内的所有空白单元格也会突出显示。尝试使用 null ""和 "\"\""的 Equal 规则似乎都不起作用。

通过将其设置为 GT 5 作为第二条规则并将红色设置为看起来有效的颜色进行了测试。

空白字段条件格式可以在 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()

最佳答案

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

                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)

综上所述,我想我将恢复到通过设置样式(因为它当前将样式设置为数字字段)的实用方法,但更真实的是,结果大小每行不同,我不始终获取相同的范围以将“”作为字段放入。

关于java - apache poi 条件格式空白单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35089038/

相关文章:

java - 使用 SQLite 数据库时我的 Activity 无法打开

java - 使用从 Kura OSGi 包中的库创建的现有插件项目

java - 解密时不能去掉0

java - 写入docx文件

java - 如何使用 Apache POI 获取数据透视表选项并取消选中显示选项卡中的 "show the Values row"

java - 如何缓存 InputStream 以供多次使用

java - 从共享首选项中检索数据

java - 我的 ImageView 的动画高度

java - apache poi excel小时格式设置单元格值java

java - 解决 Java 中的运行时依赖关系