gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 04:52:03 26 4
gpt4 key购买 nike

我试图用黄色背景格式化小于 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com