WorkbookSetting.StreamProvider を使用して外部リソースを制御する
Contents
[
Hide
]
考えられる使用シナリオ
Excel ファイルには、リンクされた画像などの外部リソースが含まれている場合があります。Aspose.Cells を使用すると、これらの外部リソースを制御できます。Workbook.Settings.StreamProviderの実装を取りますIStreamProviderインターフェース。リンクされた画像などの外部リソースを含むワークシートをレンダリングしようとするときはいつでも、IStreamProviderインターフェイスが呼び出され、外部リソースに対して適切なアクションを実行できるようになります。
WorkbookSetting.StreamProvider を使用して外部リソースを制御する
次のサンプル コードは、Workbook.Settings.StreamProvider.それはサンプル Excel ファイルリンクされた画像が含まれています。コードは、リンクされた画像を次のように置き換えますAspose ロゴを使用して、シート全体を単一の画像にレンダリングしますシートレンダリングクラス。次のスクリーンショットは、サンプルの Excel ファイルとそのレンダリングされた出力イメージ参考までに。ご覧のとおり、壊れたリンク画像は Aspose ロゴに置き換えられています。
サンプルコード
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
//Implementation of IStreamProvider | |
class SP implements IStreamProvider | |
{ | |
public void closeStream(StreamProviderOptions arg0) throws Exception { | |
} | |
public void initStream(StreamProviderOptions options) throws Exception { | |
//Open the filestream of Aspose Logo and assign it to StreamProviderOptions.Stream property | |
File imgFile = new File(srcDir + "sampleControlExternalResourcesUsingWorkbookSetting_StreamProvider.png"); | |
byte[] bts = new byte[(int) imgFile.length()]; | |
FileInputStream fin = new FileInputStream(imgFile); | |
fin.read(bts); | |
fin.close(); | |
ByteArrayOutputStream baout = new ByteArrayOutputStream(); | |
baout.write(bts); | |
baout.close(); | |
options.setStream(baout); | |
} | |
} | |
public void Run() throws Exception { | |
System.out.println("Aspose.Cells for Java Version: " + CellsHelper.getVersion()); | |
//String srcDir = Utils.Get_SourceDirectory(); | |
//String outDir = Utils.Get_OutputDirectory(); | |
//Load sample Excel file containing the external resource e.g. linked image etc. | |
Workbook wb = new Workbook(srcDir + "sampleControlExternalResourcesUsingWorkbookSetting_StreamProvider.xlsx"); | |
//Provide your implementation of IStreamProvider | |
wb.getSettings().setStreamProvider(new SP()); | |
//Access first worksheet | |
Worksheet ws = wb.getWorksheets().get(0); | |
//Specify image or print options, we need one page per sheet and png output | |
ImageOrPrintOptions opts = new ImageOrPrintOptions(); | |
opts.setOnePagePerSheet(true); | |
opts.setImageType(ImageType.PNG); | |
//Create sheet render by passing required parameters | |
SheetRender sr = new SheetRender(ws, opts); | |
//Convert your entire worksheet into png image | |
sr.toImage(0, outDir + "outputControlExternalResourcesUsingWorkbookSettingStreamProvider.png"); | |
// Print the message | |
System.out.println("ControlExternalResourcesUsingWorkbookSetting_StreamProvider executed successfully."); | |
} | |
public static void main(String[] args) throws Exception { | |
new ControlExternalResourcesUsingWorkbookSetting_StreamProvider().Run(); | |
} |