我想在使用 Apache POI 生成的 Excel 工作表中应用格式以实现行条纹格式(奇数行:灰色,偶数行:无背景色)。
以下是相关代码:
val sheetCF = sheet.sheetConditionalFormatting
val rule1 = sheetCF.createConditionalFormattingRule(
"MOD(ROW(), 2) = 0")
val fill1 = rule1.createPatternFormatting()
fill1.setFillForegroundColor( XSSFColor(Color(245,245,245),DefaultIndexedColorMap())) // Light Gray color
fill1.fillPattern = PatternFormatting.SOLID_FOREGROUND // this is causing issue
val regions1 = arrayOf(CellRangeAddress.valueOf("A1:D10"))
sheetCF.addConditionalFormatting(regions1, rule1)
由于 fillPattern,白色被用作前景。但如果使用任何其他 fillPattern,那么它就可以工作。例子:
fill1.fillPattern = PatternFormatting.BIG_SPOTS
但我需要应用扎实的模式。 任何帮助将不胜感激。
最佳答案
与通常为单元格着色的方式相反,使用 XSSFConditionalFormattingRule
,您实际上必须使用 setFillBackgroundColor()
而不是 setFillForegroundColor()
。
XSSFConditionalFormattingRule rule = sheet.getSheetConditionalFormatting().createConditionalFormattingRule("MOD(ROW(), 2) = 0");
XSSFPatternFormatting fill = rule.createPatternFormatting();
fill.setFillBackgroundColor(IndexedColors.BLUE.getIndex());
fill.setFillPattern(FillPatternType.SOLID_FOREGROUND.getCode());
CellRangeAddress[] regions = { CellRangeAddress.valueOf("A2:Z" + (sheet.getLastRowNum() + 1)) };
sheet.getSheetConditionalFormatting().addConditionalFormatting(regions, rule);
关于java - Apache POI : Issue during setting foreground color in conditionalFormatting with SOLID_FILL pattern,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59368696/