【问题标题】:How to use colors not in IndexedColors for Apache POI XSSF Excel?如何为 Apache POI XSSF Excel 使用 IndexedColors 中没有的颜色?
【发布时间】:2019-11-22 21:57:06
【问题描述】:

我正在查看一个我想要复制的 Excel 表,我遇到的唯一问题是颜色。我要复制的颜色是来自Standard Colors 部分的Blue, Accent 5, Lighter 40%Light Green。我正在查看 docs 在 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);

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

有没有人遇到过这种情况?我一直在寻找几个小时,但找不到任何 8 岁以下或不起作用的东西。

【问题讨论】:

  • @VladBochenin 我没有,但看起来他正在将 Color 传递给 XSSFColor 构造函数,现在已弃用。不推荐使用的构造函数是 (IndexedColorMap), (Color, IndexedColorMap), (byte[], IndexedColorMap) 和 (IndexedColors, IndexedColorMap)
  • @ArvindKumarAvinash 这个答案建议使用 IndexedColors,我的问题是我正在寻找的颜色不在 IndexedColors 枚举中,所以我需要创建自己的自定义颜色。
  • 使用apache poi 版本时会出现什么确切错误?使用当前的apache poi 4.1.1XSSFCellStyle 中有public void setFillForegroundColor(XSSFColor color)

标签: java excel apache-poi


【解决方案1】:

使用当前的apache poi 4.1.1XSSFCellStyle 中有public void setFillForegroundColor(XSSFColor color)

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

所需颜色的RGB 值可以从Excel 获得,方法是从调色板设置颜色,然后选择Fill Color - More Colors - Custom。不幸的是,apache poiIndexedColors 与当前Excel 版本的颜色不再完全相同。它们的版本为2007。所以它们也可以使用,但以后的Excel 版本可能会显示不同的颜色。

使用当前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();
 }
}

【讨论】:

  • 为什么你一直在创建新的 DefaultIndexedColorMap 而不是在每个工作簿中重复使用一个?
  • 如何在现代 POI (4.1.2) 中从 Workbook 返回索引地图?
  • cellStyle.setFillForegroundColor(XSSFColor) — 现代 POI 中没有这种方法。我们可以通过 Font 来设置,但是看起来有点难看。
猜你喜欢
  • 2011-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-27
  • 2021-02-10
相关资源
最近更新 更多