【发布时间】: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