Java Apache Poi, how to set background color and borders at same time
As of POI 3.x, cell fill color is set as follows:
CellStyle cs = wb.createCellStyle();
cs.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
cs.setFillPattern(FillPatternType.SOLID_FOREGROUND);
change backgroundStyle.setFillBackgroundColor(IndexedColors.GREY_50_PERCENT.getIndex());
to
backgroundStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
And you can set border as like below :
backgroundStyle.setBorderBottom(CellStyle.BORDER_THIN);
backgroundStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
backgroundStyle.setBorderLeft(CellStyle.BORDER_THIN);
backgroundStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
backgroundStyle.setBorderRight(CellStyle.BORDER_THIN);
backgroundStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
backgroundStyle.setBorderTop(CellStyle.BORDER_THIN);
backgroundStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
This will give you yellow color and border as required
Your real problem is that you have two styles, one named backgroundStyle, and the other named borderStyle. You then apply both styles to the same cell, but a cell can only have one style, so instead of adding the second style, you are overwriting the first style with the second style.
Instead of:
CellStyle backgroundStyle = workbook.createCellStyle();
backgroundStyle.setFillBackgroundColor(IndexedColors.GREY_50_PERCENT.getIndex());
backgroundStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
CellStyle borderStyle = workbook.createCellStyle();
borderStyle.setBorderBottom(CellStyle.BORDER_THIN);
borderStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
borderStyle.setBorderLeft(CellStyle.BORDER_THIN);
borderStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
borderStyle.setBorderRight(CellStyle.BORDER_THIN);
borderStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
borderStyle.setBorderTop(CellStyle.BORDER_THIN);
borderStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
Just create a single style like this:
CellStyle backgroundStyle = workbook.createCellStyle();
backgroundStyle.setFillBackgroundColor(IndexedColors.GREY_50_PERCENT.getIndex());
backgroundStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
backgroundStyle.setBorderBottom(CellStyle.BORDER_THIN);
backgroundStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
backgroundStyle.setBorderLeft(CellStyle.BORDER_THIN);
backgroundStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
backgroundStyle.setBorderRight(CellStyle.BORDER_THIN);
backgroundStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
backgroundStyle.setBorderTop(CellStyle.BORDER_THIN);
backgroundStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
Then apply that to your cell:
Sheet sheet = workbook.createSheet();
Row row = sheet.createRow(1);
Cell cell = row.createCell(1);
cell.setCellStyle(backgroundStyle);
NOTE: As mentioned in other answers here, Background Color is ignored for FillPattern = SOLID_FOREGROUND, you have to set the Foreground color for that pattern. This can be confusing because you are trying to set the cell background to a solid color. But cell background
is not the same as background color
. Cell background
is the same as Fill Pattern
which has two colors a Foreground Color
and a Background Color
these are displayed based on the specific Fill Pattern
selected. The SOLID_FOREGROUND
fill uses only the Foreground Color
.