【问题标题】:apache poi conditional formatting blank cellsapache poi条件格式空白单元格
【发布时间】:2016-01-29 16:12:55
【问题描述】:

我正在尝试用黄色背景格式化小于 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()

【问题讨论】:

  • 我看不出"M11:T${counter}"应该选择什么范围。
  • POI 有时对于空白单元格有点奇怪。您是否调试过代码以验证 cell 本身 不为空?
  • @Nicholas 范围在这个例子中并不重要,可能是 M11:T18。计数器是由于动态内容。
  • @Frank 字段由代码生成,并以数字或“”结尾。我让它在迭代值时突出显示,当它们进入时我检查是否应用低于 5 的突出显示的样式。这是尝试使用通用规则的尝试。我想我需要尝试将其设置为 null 而不是 '' 以查看结果是否有所不同。我需要空值在生成的文件中显示为空字段
  • 我尝试了一些基本代码并且它工作正常,也许是@Frank提到的空白。

标签: java apache-poi


【解决方案1】:

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

                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)

话虽如此,我认为我将恢复到通过设置样式(因为它当前将样式设置为数字字段)的务实方式,但更真正地围绕结果大小每行不同的事实,我不'' 并不总是得到相同的范围来作为字段放入“”。

【讨论】:

    【解决方案2】:

    好吧,对于空白,我使用了一个公式,其值为 =ISBLANK()。该公式需要您范围的第一个单元格,在我的情况下是 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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-23
      相关资源
      最近更新 更多