2016-01-29 167 views
1

我正在尝试格式化5以下的黄色背景的单元格,并且此范围内的所有空白单元格也会突出显示。尝试与null“”和“\”\“”相等的规则似乎没有工作。apache poi条件格式化空白单元格

已经通过将GT 5设置为第二条规则进行了测试,并将红色设置为可以正常工作的颜色。

空白字段条件格式是可以在Excel中完成的事情,并且我还没有设法找到任何与使用apache-poi实现相同目的相关的任何内容。

好奇,如果任何人已设法使其工作(这是常规所以地区可能会有所不同)

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() 
+0

我没有看到''M11:T $ {counter}“'应该选择什么范围。 –

+0

有关空白单元格的POI有时会有点奇怪。您是否调试过您的代码以验证_cell本身_不为null? – Frank

+0

@Nicholas范围在这个例子中并不重要,可能M11:T18。该计数器是由于动态内容。 – Vahid

回答

1

那么,对于空白的我用了一个公式,用值= 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); 
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) 

说了这么多,我想我会恢复到务实的方式,也就是通过设置样式(因为它目前设置的风格是数值型字段),但更多的真圆导致大小每改变的事实排和我并不总是有相同的范围来放入“”作为一个字段。