Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Apache POI XSSF Excel中使用非IndexedColors中的颜色?_Java_Excel_Apache Poi - Fatal编程技术网

Java 如何在Apache POI XSSF Excel中使用非IndexedColors中的颜色?

Java 如何在Apache POI XSSF Excel中使用非IndexedColors中的颜色?,java,excel,apache-poi,Java,Excel,Apache Poi,我正在看一张excel表格,我想复制它,唯一的问题是颜色。我想要复制的颜色是标准颜色部分中的蓝色、重音5、浅40%和浅绿色。我正在研究在XSSF工作簿中使用自定义颜色的方法,该方法如下所示: XSSFWorkbook wb = new XSSFWorkbook(); XSSFSheet sheet = wb.createSheet(); XSSFRow row = sheet.createRow(0); XSSFCell cell = row.createCell(0); cell.setCel

我正在看一张excel表格,我想复制它,唯一的问题是颜色。我想要复制的颜色是
标准颜色部分中的
蓝色、重音5、浅40%
浅绿色。我正在研究在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(新的XSSFColor(新的java.awt.Color(128,0,128),新的DefaultIndexedColorMap())
我得到一个错误,因为
.setFillForegroundColor()
的唯一参数只接受一个参数,它是
short
而不是
XSSFColor


有人在这件事上运气好吗?我已经搜索了几个小时,找不到任何不是8年前的或不起作用的东西。

使用当前的
ApachePOI 4.1.1
XSSFCellStyle
中有

应使用构造函数创建
XSSFColor
,因为所有其他构造函数都已弃用或标记为
TEST ONLY
,或者不可用于创建自定义颜色

所需颜色的
RGB
值可以从
Excel
中获得,方法是从调色板中设置颜色,然后选择
填充颜色
-
更多颜色
-
自定义
。不幸的是,
apachepoi
的颜色与当前的
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();
 }
}
import java.io.FileOutputStream;
导入org.apache.poi.ss.usermodel.*;
导入org.apache.poi.xssf.usermodel.xssf工作簿;
导入org.apache.poi.xssf.usermodel.XSSFColor;
导入org.apache.poi.xssf.usermodel.XSSFCellStyle;
导入org.apache.poi.xssf.usermodel.DefaultIndexedColorMap;
公共类CreateExcelXSSFCellFillColor{
公共静态void main(字符串[]args)引发异常{
XSSFWorkbook工作簿=新XSSFWorkbook();
java.util.List cellStyles=new java.util.ArrayList();
XSSFCellStyle cellStyle;字节[]rgb;XSSFColor color;
//您的自定义颜色#800080
//在工作簿级别创建单元格样式
cellStyle=workbook.createCellStyle();
//设置填充图案设置
cellStyle.setFillPattern(FillPatternType.SOLID\u前景);
//创建RGB字节数组
rgb=新字节[3];
rgb[0]=(字节)128;//红色
rgb[1]=(字节)0;//绿色
rgb[2]=(字节)128;//蓝色
//创建XSSFColor
颜色=新XSSFColor(rgb,新的DefaultIndexedColorMap());
//将填充颜色设置为单元格样式
cellStyle.setFillForegroundColor(颜色);
cellStyles.add(cellStyle);
//浅绿色
cellStyle=workbook.createCellStyle();
cellStyle.setFillPattern(FillPatternType.SOLID\u前景);
rgb=新字节[3];
rgb[0]=(字节)146;//红色
rgb[1]=(字节)208;//绿色
rgb[2]=(字节)80;//蓝色
颜色=新XSSFColor(rgb,新的DefaultIndexedColorMap());
cellStyle.setFillForegroundColor(颜色);
cellStyles.add(cellStyle);
//蓝色,口音5,浅色40%
cellStyle=workbook.createCellStyle();
cellStyle.setFillPattern(FillPatternType.SOLID\u前景);
rgb=新字节[3];
rgb[0]=(字节)155;//红色
rgb[1]=(字节)194;//绿色
rgb[2]=(字节)230;//蓝色
颜色=新XSSFColor(rgb,新的DefaultIndexedColorMap());
cellStyle.setFillForegroundColor(颜色);
cellStyles.add(cellStyle);
工作表=工作簿.createSheet();
对于(int r=0;r
你试过了吗?@VladBochenin我没有试过,但看起来他正在向XSSFColor构造函数传递一种颜色,这种颜色现在已被弃用。不推荐使用的构造函数是(IndexedColorMap),(Color,IndexedColorMap),(byte[],IndexedColorMap)和(IndexedColors,IndexedColorMap)@ArvindKumarAvinash答案建议使用IndexedColors,我的问题是我要查找的颜色不在IndexedColors枚举中,所以我需要创建自己的自定义颜色。使用What
ApachePOI
version会出现什么错误?使用当前的
apache poi 4.1.1
XSSFCellStyle
中有。为什么您一直在创建新的DefaultIndexedColorMap,而不是每个woorkbook重复使用一个?如何在现代poi(4.1.2)中从工作簿返回索引映射?
cellStyle.setFillForegroundColor(XSSFColor)
-现代poi中没有这种方法。我们可以通过字体设置,但它看起来有点难看。