首页 > 解决方案 > 如何使用 apache poi 4.1.0 设置单元格的背景颜色

问题描述

我正在尝试为 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);
    }
}

我在这里发现了一个类似的问题[如何使用 apache pio 4.1.0 设置单元格的背景颜色,但答案没有帮助。

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

谢谢。

标签: javaexcelapache-poi

解决方案


正如我在链接的答案中所说:单元内部使用图案填充。填充背景色是图案背后的颜色。填充前景色是图案的颜色。要使用纯色填充单元格,您需要使用填充前景色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% 灰色上的黑色字体可读性不好。

再次:灰色填充前景色和实心填充图案会产生实心灰色单元格填充。

结果:

在此处输入图像描述


推荐阅读