如何使用Apache POI 4.1.0设置单元格的背景色

时间:2019-10-20 17:05:47

标签: java excel apache-poi

我正在尝试为excel文件中的标头设置背景色,但是却获得了黑色,单元格上没有可见的内容。我正在使用apache poi 4.1.0和poi-ooxml 4.1.0。

这是我尝试使用的代码。

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);
    }
}

我在这里找到了类似的问题[how to set background color of a cell using apache pio 4.1.0,但答案没有帮助。

您能指导我解决这个问题吗?

谢谢。

2 个答案:

答案 0 :(得分:1)

正如我的链接答案中所述:单元内部使用模式填充。填充背景颜色是图案的后面的颜色。填充前景的颜色是图案的颜色。要使用纯色填充单元格,您需要使用填充前景颜色CellStyle.setFillForegroundColor和实心图案FillPatternType.SOLID_FOREGROUND

让我们再举一个完整的例子:

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();
 }
}

这正是您的工作,除了我的代码仅在第一行使用标头单元格样式。它使用白色字体,因为黑色字体在50%灰色上的可读性不是很好。

再次:灰色填充前景色和纯色填充图案会生成纯灰色单元格填充。

结果:

enter image description here

答案 1 :(得分:0)

您可以执行以下操作:

byte[] rgb=DefaultIndexedColorMap.getDefaultRGB(IndexedColors.RED.getIndex());
cellStyle.setFillBackgroundColor(new XSSFColor(rgb,null));