如何使用 Apache POI XSSF Excel 的 IndexedColors 中没有的颜色?

Ale*_*dez 5 java excel apache-poi

我正在查看一个我想要复制的 Excel 工作表,我遇到的唯一问题是颜色。我想要复制的颜色是Blue, Accent 5, Lighter 40%Light Green来自该Standard Colors部分。我正在查看有关在 XSSF 工作簿中使用自定义颜色的文档,它指出执行此操作的方法如下:

XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet();
XSSFRow row = sheet.createRow(0);
XSSFCell cell = row.createCell(0);
cell.setCellValue("custom XSSF colors");

XSSFCellStyle style1 = wb.createCellStyle();
style1.setFillForegroundColor(new XSSFColor(new java.awt.Color(128, 0, 128), new DefaultIndexedColorMap()));
style1.setFillPattern(FillPatternType.SOLID_FOREGROUND);
Run Code Online (Sandbox Code Playgroud)

当我尝试使用时style1.setFillForegroundColor(new XSSFColor(new java.awt.Color(128, 0, 128), new DefaultIndexedColorMap()));出现错误,因为唯一的参数.setFillForegroundColor()只接受一个参数,即 ashort而不是XSSFColor.

有人有这样的运气吗?我已经找了几个小时了,找不到任何 8 年前或不起作用的东西。

Axe*_*ter 5

使用 current中apache poi 4.1.1存在public void setFillForegroundColor(XSSFColor color)XSSFCellStyle

应使用构造函数public XSSFColor(byte[] rgb, IndexedColorMap colorMap)XSSFColor创建,因为所有其他构造函数已被弃用或标记或不可用于创建自定义颜色。TEST ONLY

RGB可以Excel通过从调色板中设置颜色来获取所需颜色的值,然后选择Fill Color- More Colors- Custom。不幸的是,IndexedColorsapache poi与当前版本的颜色不再精确。它们是版本。因此它们也可以使用,但以后的版本可能会显示不同的颜色。Excel2007Excel

使用 current 的完整示例apache poi 4.1.1

import java.io.FileOutputStream;

import org.apache.poi.ss.usermodel.*;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.DefaultIndexedColorMap;

public class CreateExcelXSSFCellFillColor {

 public static void main(String[] args) throws Exception {
  XSSFWorkbook workbook = new XSSFWorkbook();

  java.util.List<XSSFCellStyle> cellStyles = new java.util.ArrayList<XSSFCellStyle>();
  XSSFCellStyle cellStyle; byte[] rgb; XSSFColor color;

  //Your custom color #800080
  //create cell style on workbook level
  cellStyle = workbook.createCellStyle();
  //set pattern fill settings
  cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  //create the RGB byte array
  rgb = new byte[3];
  rgb[0] = (byte) 128; // red
  rgb[1] = (byte) 0; // green
  rgb[2] = (byte) 128; // blue
  //create XSSFColor
  color = new XSSFColor(rgb, new DefaultIndexedColorMap());
  //set fill color to cell style
  cellStyle.setFillForegroundColor(color);

  cellStyles.add(cellStyle);

  //Light Green
  cellStyle = workbook.createCellStyle();
  cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  rgb = new byte[3];
  rgb[0] = (byte) 146; // red
  rgb[1] = (byte) 208; // green
  rgb[2] = (byte) 80; // blue
  color = new XSSFColor(rgb, new DefaultIndexedColorMap());
  cellStyle.setFillForegroundColor(color);
  cellStyles.add(cellStyle);

  //Blue, Accent 5, Lighter 40%
  cellStyle = workbook.createCellStyle();
  cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  rgb = new byte[3];
  rgb[0] = (byte) 155; // red
  rgb[1] = (byte) 194; // green
  rgb[2] = (byte) 230; // blue
  color = new XSSFColor(rgb, new DefaultIndexedColorMap());
  cellStyle.setFillForegroundColor(color);
  cellStyles.add(cellStyle);

  Sheet sheet = workbook.createSheet();
  for (int r = 0; r < cellStyles.size(); r++) {
   Row row = sheet.createRow(r);
   row.setHeight((short)(20*20));
   Cell cell = row.createCell(0);
   cell.setCellValue("cell style " + (r+1));
   cell.setCellStyle(cellStyles.get(r));
  }
  sheet.setColumnWidth(0, 20*256);

  FileOutputStream out = new FileOutputStream("CreateExcelXSSFCellFillColor.xlsx");
  workbook.write(out);
  out.close();
  workbook.close();
 }
}
Run Code Online (Sandbox Code Playgroud)

  • 为什么您总是创建新的 DefaultIndexedColorMap,而不是为每个工作簿重复使用一个? (3认同)