Controlar Recursos Externos usando WorkbookSetting.StreamProvider
Escenarios de uso posibles
A veces, su archivo de Excel contiene recursos externos, como imágenes vinculadas, etc. Aspose.Cells le permite controlar estos recursos externos utilizando Workbook.Settings.StreamProvider, que toma la implementación de IStreamProvider interfaz. Cada vez que intente mostrar su hoja de cálculo que contiene recursos externos, como imágenes vinculadas, se invocarán los métodos de IStreamProvider interfaz, lo que le permitirá tomar medidas apropiadas para sus recursos externos.
Controlar Recursos Externos usando WorkbookSetting.StreamProvider
El siguiente código de muestra explica el uso de Workbook.Settings.StreamProvider. Carga el archivo Excel de muestra que contiene una imagen vinculada. El código reemplaza la imagen vinculada con el logotipo de Aspose y representa toda la hoja en una sola imagen usando la clase SheetRender. La siguiente captura de pantalla muestra el archivo Excel de muestra y su imagen de salida representada para su referencia. Como puede ver, la imagen vinculada rota se reemplaza con el logo de Aspose.
Código de muestra
// 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(); | |
} |