I am trying to set the background color for headers in excel file, But getting the black color with no content visible on the cell. I am using apache poi 4.1.0 and poi-ooxml 4.1.0.
Here is the code I am trying with.
XSSFWorkbook workbook = new XSSFWorkbook();
// Create a blank sheet
XSSFSheet sheet = workbook.createSheet("student Details");
XSSFCellStyle cellStyle = sheet.getWorkbook().createCellStyle();
cellStyle.setFillBackgroundColor(IndexedColors.GREY_50_PERCENT.index);
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
// This data needs to be written (Object[])
Map<String, Object[]> data = new TreeMap<String, Object[]>();
data.put("1", new Object[] { "ID", "NAME", "LASTNAME" });
data.put("2", new Object[] { 1, "Pankaj", "Kumar" });
data.put("3", new Object[] { 2, "Prakashni", "Yadav" });
data.put("4", new Object[] { 3, "Ayan", "Mondal" });
data.put("5", new Object[] { 4, "Virat", "kohli" });
// Iterate over data and write to sheet
Set<String> keyset = data.keySet();
int rownum = 0;
for (String key : keyset) {
// this creates a new row in the sheet
XSSFRow row = sheet.createRow(rownum++);
Object[] objArr = data.get(key);
int cellnum = 0;
for (Object obj : objArr) {
// this line creates a cell in the next column of that row
XSSFCell cell = row.createCell(cellnum++);
cell.setCellStyle(cellStyle);
if (obj instanceof String)
cell.setCellValue((String) obj);
else if (obj instanceof Integer)
cell.setCellValue((Integer) obj);
}
}
A similar problem I found here[how to set background color of a cell using apache pio 4.1.0 but the answer did not help.
Could you please guide me to resolve this.
Thanks.
CellStyle.setFillForegroundColor
and solid patternFillPatternType.SOLID_FOREGROUND
. So waht happens if you are usingcellStyle.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.index); cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
in your code instaad ofcellStyle.setFillBackgroundColor
? - Axel Richter