Reputation: 403
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.
Upvotes: 6
Views: 52675
Reputation: 61852
As said in my linked answer: Cell interior uses pattern fills. The fill background color is the color behind the pattern.The fill foreground color is the color of the pattern. To fill the cell using a plain color, you need using fill foreground color CellStyle.setFillForegroundColor
and solid pattern FillPatternType.SOLID_FOREGROUND
.
Let's have a complete example again:
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.util.*;
public class CreateExcelCellFillColor2 {
public static void main(String[] args) throws Exception {
//Workbook workbook = new HSSFWorkbook();
Workbook workbook = new XSSFWorkbook();
// Create a blank sheet
Sheet sheet = workbook.createSheet("student Details");
// Create header CellStyle
Font headerFont = workbook.createFont();
headerFont.setColor(IndexedColors.WHITE.index);
CellStyle headerCellStyle = sheet.getWorkbook().createCellStyle();
// fill foreground color ...
headerCellStyle.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.index);
// and solid fill pattern produces solid grey cell fill
headerCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
headerCellStyle.setFont(headerFont);
// 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
Row 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
Cell cell = row.createCell(cellnum++);
// if rownum is 1 (first row was created before) then set header CellStyle
if (rownum == 1) cell.setCellStyle(headerCellStyle);
if (obj instanceof String)
cell.setCellValue((String)obj);
else if (obj instanceof Integer)
cell.setCellValue((Integer)obj);
}
}
for (int c = 0; c < 3; c++) {
sheet.autoSizeColumn(c);
}
FileOutputStream out = null;
if (workbook instanceof HSSFWorkbook) {
out = new FileOutputStream("CreateExcelCellFillColor2.xls");
} else if (workbook instanceof XSSFWorkbook) {
out = new FileOutputStream("CreateExcelCellFillColor2.xlsx");
}
workbook.write(out);
out.close();
workbook.close();
}
}
This is exactly what you are doing except my code uses header cell style only for first row. And it uses a white font since black font on 50% grey is not really good readable.
Again: A grey fill foreground color and solid fill pattern produces solid grey cell fill.
Result:
Upvotes: 28