Java ApachePOI将渐变颜色应用于单元格

Java ApachePOI将渐变颜色应用于单元格,java,excel,apache,apache-poi,Java,Excel,Apache,Apache Poi,我一直在网上搜索,没有发现使用ApachePOI将渐变颜色应用于excel表格单元格的真正好例子 我发现的示例非常古老,在当前的ApachePOI版本中,这些类实际上已经不存在了。我目前正在使用ApachePOI版本3.16 有人能指出使用poi库将渐变颜色应用于excel工作表所需的步骤吗。感谢所有提示。始终无法使用默认的实际apache poi版本设置渐变单元格填充 因此,我怀疑您找到的代码是针对XSSF(*.xlsx)的,对于您找到的代码,只是没有提到此代码需要类路径中所有模式的完整jar

我一直在网上搜索,没有发现使用ApachePOI将渐变颜色应用于excel表格单元格的真正好例子

我发现的示例非常古老,在当前的ApachePOI版本中,这些类实际上已经不存在了。我目前正在使用ApachePOI版本3.16


有人能指出使用poi库将渐变颜色应用于excel工作表所需的步骤吗。感谢所有提示。

始终无法使用默认的实际
apache poi
版本设置渐变单元格填充

因此,我怀疑您找到的代码是针对
XSSF
*.xlsx
)的,对于您找到的代码,只是没有提到此代码需要类路径中所有模式的完整jar,如中所述

下面的示例可以工作,但也需要类路径中所有模式
ooxml-schemas-1.3.jar
的完整jar,如中所述

它首先将图案填充设置设置为
CellStyle
,然后进行一些填充以从中获取填充索引。然后,它获取此
单元格样式中使用的低级别
CTFill
。然后取消模式填充,然后设置渐变填充

获取我正在使用的有关如何使用的信息


始终无法使用默认的实际apache poi版本设置渐变单元格填充

因此,我怀疑您找到的代码是针对
XSSF
*.xlsx
)的,对于您找到的代码,只是没有提到此代码需要类路径中所有模式的完整jar,如中所述

下面的示例可以工作,但也需要类路径中所有模式
ooxml-schemas-1.3.jar
的完整jar,如中所述

它首先将图案填充设置设置为
CellStyle
,然后进行一些填充以从中获取填充索引。然后,它获取此
单元格样式中使用的低级别
CTFill
。然后取消模式填充,然后设置渐变填充

获取我正在使用的有关如何使用的信息


感谢这真的澄清了我的问题,我已经在我的项目中包含了wrondmaven依赖项,因为一些类无法解决。我使用poi ooxml模式作为依赖项,这是错误的。现在改为ooxml模式,可以找到所有类。谢谢你的澄清。谢谢你澄清了我的问题。我已经在我的项目中包含了这个依赖项,由于这个原因,一些类无法解决。我使用poi ooxml模式作为依赖项,这是错误的。现在改为ooxml模式,可以找到所有类。谢谢你的澄清。
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.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTGradientFill;

public class CreateExcelCellGradientFillColor {

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

  Sheet sheet = workbook.createSheet();
  Row row = sheet.createRow(0);

  XSSFCellStyle cellstyle = workbook.createCellStyle();
  //set pattern fill settings only to have some fill to get the fill index from it
  cellstyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);

  //get fill index used in this CellStyle
  int fillidx = (int)cellstyle.getCoreXf().getFillId();

  //get the low level CTFill used in this CellStyle
  CTFill ctfill = workbook.getStylesSource().getFillAt(fillidx).getCTFill();
System.out.println(ctfill);

  //unset the pattern fill
  ctfill.unsetPatternFill();

  //now low level set the gradient fill
  byte[] rgb1 = new byte[3];
  rgb1[0] = (byte) 0; // red
  rgb1[1] = (byte) 0; // green
  rgb1[2] = (byte) 255; // blue

  byte[] rgb2 = new byte[3];
  rgb2[0] = (byte) 255; // red
  rgb2[1] = (byte) 255; // green
  rgb2[2] = (byte) 255; // blue

  CTGradientFill ctgradientfill = ctfill.addNewGradientFill();
  ctgradientfill.setDegree(90.0);
  ctgradientfill.addNewStop().setPosition(0.0);
  ctgradientfill.getStopArray(0).addNewColor().setRgb(rgb1);
  ctgradientfill.addNewStop().setPosition(0.5);
  ctgradientfill.getStopArray(1).addNewColor().setRgb(rgb2);
  ctgradientfill.addNewStop().setPosition(1.0);
  ctgradientfill.getStopArray(2).addNewColor().setRgb(rgb1);
System.out.println(ctfill);

  Cell cell = row.createCell(0);
  cell.setCellValue("");
  cell.setCellStyle(cellstyle);

  workbook.write(new FileOutputStream("CreateExcelCellGradientFillColor.xlsx"));
  workbook.close();
 }
}