기본 그래디언트 apache poi
버전을 사용하여 그래디언트 셀 채우기를 설정하는 것은 항상 가능하지 않습니다.
그래서 난 당신이 볼 수있는 코드가 XSSF
(*.xlsx
)에 대한 것을 생각하고 코드를 당신은 faq-N10025에서 언급 한 바와 같이 그냥이 코드는 클래스 경로의 스키마 ooxml-schemas-1.3.jar
모두의 전체 항아리 필요하다고 언급되지 않았다 발견 .
다음 예제는 작동하지만 faq-N10025에서 언급 한대로 클래스 경로에있는 스키마 ooxml-schemas-1.3.jar
의 전체 병이 모두 필요합니다.
먼저 패턴 채우기 설정을 CellStyle
에 설정하여 일부 채우기 만 채우기 인덱스를 가져옵니다. 그런 다음이 CellStyle
에 사용 된 낮은 레벨 CTFill
을 얻습니다. 그런 다음 패턴 채우기를 설정 해제 한 다음 그래디언트 채우기를 설정합니다.
사용 방법에 대한 정보는 grepcode.com입니다.
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();
}
}
정말 내 문제는 내가 내 프로젝트에 종속 Maven 종속성을 포함하고 그 때문에 일부 클래스를 해결할 수 없습니다 명확히합니다. poi-ooxml-schemas를 종속성으로 잘못 사용했습니다. 이제 ooxml-schemas로 변경되었으며 모든 클래스를 찾을 수 있습니다. 귀하의 설명에 감사드립니다. –