Java ApachePOI条件格式空白单元格 我试图用黄色背景格式化低于5的单元格,并且这个范围内的所有空白单元格也会被高亮显示。尝试使用null“”和“\”\“”的相等规则,但这两个规则似乎都不起作用

Java ApachePOI条件格式空白单元格 我试图用黄色背景格式化低于5的单元格,并且这个范围内的所有空白单元格也会被高亮显示。尝试使用null“”和“\”\“”的相等规则,但这两个规则似乎都不起作用,java,apache-poi,Java,Apache Poi,通过将其设置为GT 5作为第二条规则进行测试,并将红色设置为dos似乎起作用的颜色 空白字段条件格式是可以在excel中完成的,到目前为止,我还没有找到任何与使用ApachePOI实现相同内容相关的内容 不知道是否有人能让它工作(这是在groovy上,所以区域看起来可能不同) 嗯,对于空白,我使用了一个公式,值为ISBLACK()。该公式需要范围的第一个单元格,在我的例子A1中,可能在你的例子M11中。此外,规则的顺序似乎很重要,在添加条件格式化时,我必须先把空白规则放进去。 // GET CO

通过将其设置为GT 5作为第二条规则进行测试,并将红色设置为dos似乎起作用的颜色

空白字段条件格式是可以在excel中完成的,到目前为止,我还没有找到任何与使用ApachePOI实现相同内容相关的内容

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


嗯,对于空白,我使用了一个公式,值为ISBLACK()。该公式需要范围的第一个单元格,在我的例子A1中,可能在你的例子M11中。此外,规则的顺序似乎很重要,在添加条件格式化时,我必须先把空白规则放进去。

// GET CONDITIONAL FORMATTING
SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
// IS BLANK RULE
ConditionalFormattingRule ruleISBLANK = sheetCF.createConditionalFormattingRule("=ISBLANK(A1)");
PatternFormatting fill2 = ruleISBLANK.createPatternFormatting();
fill2.setFillBackgroundColor(IndexedColors.RED.index);
fill2.setFillPattern(PatternFormatting.SOLID_FOREGROUND);
// IS LESS THAN RULE
ConditionalFormattingRule ruleLT = sheetCF.createConditionalFormattingRule(ComparisonOperator.LT, "5");
PatternFormatting fill1 = ruleLT.createPatternFormatting();
fill1.setFillBackgroundColor(IndexedColors.LIGHT_GREEN.index);
fill1.setFillPattern(PatternFormatting.SOLID_FOREGROUND);
// RANGE
CellRangeAddress[] regions = {CellRangeAddress.valueOf("A1:A10")};
// ADD CONDITIONAL FORMATTING
sheetCF.addConditionalFormatting(regions, ruleISBLANK);
sheetCF.addConditionalFormatting(regions, ruleLT);

以上答案仅限于设置公式的字段。昨晚我在想这个问题,尝试了一些不同的方法,可以处理空白单元格,但单元格必须设置为“”才能工作

                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)

说到这里,我想我会回到实用的方式,通过设置样式(因为它目前将样式设置为一个数字字段),但更重要的是,每行的结果大小不同,并且我并不总是得到相同的范围作为字段放在“”中。

我不知道什么范围
“M11:t${counter}”
应该选择。对于空白单元格,POI有时有点奇怪。您是否调试了代码以验证单元格本身是否为空?@Nicholas在本例中,范围并不重要,可能是M11:T18。计数器是由动态内容生成的。@这些字段由代码生成,并以数字或“”结束。我让它像在高亮显示中一样工作,同时迭代通过值,并且在值进入时检查以应用一种样式,如果低于5,则高亮显示或不高亮显示。这是一次尝试使用通用规则的尝试。我想我需要尝试将其设置为null而不是“”以查看结果是否有所不同。我需要NULL在生成的文件中显示为空字段。我尝试了一些基本代码,它成功了,也许空白是@Frank提到的。
                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)