Contrôler les ressources externes à l'aide de WorkbookSetting.StreamProvider
Scénarios d’utilisation possibles
Parfois, votre fichier Excel contient des ressources externes, par exemple des images liées, etc. Aspose.Cells vous permet de contrôler ces ressources externes en utilisantWorkbook.Settings.StreamProviderWorkbook.Settings.StreamProviderqui prend la mise en œuvre deIStreamProviderinterface. Chaque fois que vous essayez de rendre votre feuille de calcul contenant des ressources externes, par exemple des images liées, les méthodes deIStreamProvidersera invoquée, ce qui vous permettra de prendre les mesures appropriées pour vos ressources externes.
Contrôler les ressources externes à l’aide de WorkbookSetting.StreamProvider
L’exemple de code suivant explique l’utilisation deWorkbook.Settings.StreamProviderWorkbook.Settings.StreamProvider. Il charge leexemple de fichier Excelcontenant une image liée. Le code remplace l’image liée parAspose Logoet rend la feuille entière en une seule image en utilisantFeuilleRenduclasse. La capture d’écran suivante montre l’exemple de fichier Excel et sonimage de sortie renduepour une référence. Comme vous pouvez le voir, l’image liée cassée est remplacée par le logo Aspose.
Exemple de code
// 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(); | |
} |