簡體   English   中英

apache poi條件格式化空白單元格

[英]apache poi conditional formatting blank cells

我正在嘗試使用黃色背景格式化低於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()

上面的答案非常局限於您設置公式的字段。 昨晚我正在考慮這個問題並嘗試了一些與空白單元格不同的東西,但是單元格必須設置為“”才能正常工作。

                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)

盡管如此,我認為我將回歸到通過設置樣式的實用方式(因為它目前將樣式設置為數字字段)但更確切地說,結果大小每行變化的事實並不總是如此獲得相同的范圍以放入“”作為字段。

好吧,對於空白我使用了一個公式,其值為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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM