I use the following code to set background color for cells:
XSSFCellStyle cellStyle = (XSSFCellStyle) excelStyle.getCellStyle();
cellStyle.setFillForegroundColor(new XSSFColor(java.awt.Color.decode("#FFFF99")));
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
But it is not working for cells which are merged, it always becomes black no matter of what I try to set. It is working only if I set a predefined color like:
cellStyle.setFillForegroundColor(IndexedColors.CORAL.index);
The color is defined by user so I can't use IndexedColors
. Also, I can't create a custom palette color (as explained here ) because my workbook is of type SXSSFWorkbook
, not HSSFWorkbook
.
How to set background color for merged cells?
I have tried below steps and working for me.
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 3));
After lot of debugging I found issue which was causing the merged cell to be always black was below line.
CellUtil.setAlignment(sheetSummary.getRow(0).getCell(4), HorizontalAlignment.CENTER_SELECTION);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.