Control External Resources using WorkbookSetting.StreamProvider
Possible Usage Scenarios
Sometimes, your Excel file contains external resources e.g. linked images, etc. Aspose.Cells allows you to control these external resources using Workbook.Settings.StreamProvider that takes the implementation of the IStreamProvider interface. Whenever you will try to render your worksheet containing external resources e.g. linked images, the methods of the IStreamProvider interface will be invoked which will enable you to take appropriate actions for your external resources.
Control External Resources using WorkbookSetting.StreamProvider
The following sample code explains the usage of the Workbook.Settings.StreamProvider. It loads the sample Excel file containing a linked image. The code replaces the linked image with Aspose Logo and renders the entire sheet into a single image using SheetRender class. The following screenshot shows the sample Excel file and its rendered output image for a reference. As you can see, the broken linked image is replaced with Aspose Logo.
Sample Code
// 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."); | |
} | |
} |