WorkbookSetting.StreamProvider を使用して外部リソースを制御する
可能な使用シナリオ
時には、Excelファイルにはリンクされた画像などの外部リソースが含まれています。Aspose.Cellsを使用すると、Workbook.Settings.StreamProviderというインターフェースの実装を使用して、これらの外部リソースを制御することができます。ワークシートに外部リソース(リンクされた画像など)を含む場合、IStreamProviderインターフェースのメソッドが呼び出され、外部リソースに対する適切なアクションを取ることが可能になります。
Workbook.Settings.StreamProviderを使用して外部リソースを制御する
次のサンプルコードは、Workbook.Settings.StreamProviderの使用方法を説明しています。サンプルExcelファイルをロードし、リンクされた画像をAsposeロゴで置き換え、SheetRenderクラスを使用してシート全体を単一の画像にレンダリングします。以下のスクリーンショットは、サンプルExcelファイルとレンダリングされた出力画像を参照のこと。壊れたリンクされた画像がAsposeロゴで置き換えられていることが分かります。
サンプルコード
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
class ControlExternalResourcesUsingWorkbookSetting_StreamProvider | |
{ | |
//Source directory | |
static string sourceDir = RunExamples.Get_SourceDirectory(); | |
//Output directory | |
static string outputDir = RunExamples.Get_OutputDirectory(); | |
//Implementation of IStreamProvider | |
class SP : IStreamProvider | |
{ | |
public void CloseStream(StreamProviderOptions options) | |
{ | |
} | |
public void InitStream(StreamProviderOptions options) | |
{ | |
//string sourceDir = RunExamples.Get_SourceDirectory(); | |
//Open the filestream of Aspose Logo and assign it to StreamProviderOptions.Stream property | |
FileStream fi = new FileStream(sourceDir + "sampleControlExternalResourcesUsingWorkbookSetting_StreamProvider.png", FileMode.OpenOrCreate, FileAccess.Read); | |
options.Stream = fi; | |
} | |
} | |
public static void Run() | |
{ | |
//Load sample Excel file containing the external resource e.g. linked image etc. | |
Workbook wb = new Workbook(sourceDir + "sampleControlExternalResourcesUsingWorkbookSetting_StreamProvider.xlsx"); | |
//Provide your implementation of IStreamProvider | |
wb.Settings.ResourceProvider = new SP(); | |
//Access first worksheet | |
Worksheet ws = wb.Worksheets[0]; | |
//Specify image or print options, we need one page per sheet and png output | |
ImageOrPrintOptions opts = new ImageOrPrintOptions(); | |
opts.OnePagePerSheet = true; | |
opts.ImageType = 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, outputDir + "outputControlExternalResourcesUsingWorkbookSetting_StreamProvider.png"); | |
Console.WriteLine("ControlExternalResourcesUsingWorkbookSetting_StreamProvider executed successfully."); | |
} | |
} |