2017-09-06 17 views
0

私はウェブを検索しており、Apache Poiを使用してシートセルにグラデーションカラーを適用した実際の良い例は見つけられませんでした。Apache Poiセルにグラデーションカラーを適用する

私が見つけた例はかなり古く、現在のApache Poiバージョンではクラスが実際には存在しません。私は現在、Apache Poiバージョン3.16を使用しています。

誰かがpoiライブラリを使ってシートをExcelに変換するために必要なステップを指摘できますか?すべてのヒントは高く評価されています。

答えて

1

デフォルトの実際のapache poiバージョンを使用して、グラデーションセルの塗りつぶしを設定することは、まだ可能ではありません。

だから私は、あなたが見つけたコードがXSSF*.xlsx)のためだったとコードのためにあなたはそれがちょうどfaq-N10025で述べたように、このコードは、クラスパス内のスキーマooxml-schemas-1.3.jarのすべての完全なjarファイルを必要としていることを言及していなかったたと思われます。

次の例は機能しますが、faq-N10025で説明されているクラスパス内のすべてのスキーマooxml-schemas-1.3.jarの完全なjarも必要です。

最初にパターンの塗りつぶし設定をCellStyleに設定し、塗りつぶしインデックスを取得するために塗りつぶしを行うだけです。次に、で使用される低レベルCTFillが得られます。次に、パターンの塗りが解除され、グラデーションの塗りが設定されます。

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(); 
} 
} 
+0

私のプロジェクトにMavenの依存関係が含まれていて、いくつかのクラスを解決できなかったため、実際に私の問題を明確にしました。 poi-ooxml-schemasを依存関係として間違って使用しました。 ooxml-schemasに変更され、すべてのクラスが見つかりました。ご清聴ありがとうございます。 –

関連する問題