Conversione del foglio di lavoro in diversi formati di immagine
Conversione del foglio di lavoro in immagine
A volte, è utile salvare un’immagine di un foglio di lavoro. Le immagini possono essere condivise online, inserite in altri documenti (report scritti in Word Microsoft, ad esempio, o presentazioni PowerPoint).
Aspose.Cells fornisce l’esportazione di immagini tramite il file**SheetRender** classe. Questa classe rappresenta il foglio di lavoro di cui verrà eseguito il rendering in un’immagine. Il**SheetRender** la classe fornisce il**toImage()**metodo per convertire un foglio di lavoro in un file immagine. Sono supportati i formati BMP, PNG, JPEG, TIFF e EMF.
Il codice seguente mostra come convertire un foglio di lavoro in un file Excel Microsoft in un file PNG.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(WorksheetToImage.class) + "LoadingSavingConvertingAndManaging/"; | |
// Instantiate a new workbook with path to an Excel file | |
Workbook book = new Workbook(dataDir + "MyTestBook1.xlsx"); | |
// Create an object for ImageOptions | |
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions(); | |
// Set the image type | |
imgOptions.setImageType(ImageType.PNG); | |
// Get the first worksheet. | |
Worksheet sheet = book.getWorksheets().get(0); | |
// Create a SheetRender object for the target sheet | |
SheetRender sr = new SheetRender(sheet, imgOptions); | |
for (int j = 0; j < sr.getPageCount(); j++) { | |
// Generate an image for the worksheet | |
sr.toImage(j, dataDir + "WToImage-out" + j + ".png"); | |
} |
Conversione del foglio di lavoro in SVG
SVG sta perGrafica vettoriale scalabileSVG è una specifica basata sugli standard XML per la grafica vettoriale bidimensionale. È uno standard aperto che è stato sviluppato dal World Wide Web Consortium (W3C) dal 1999.
Dal rilascio della v7.1.0,Aspose.Cells for Java può convertire i fogli di lavoro in SVG immagini.
Per utilizzare questa funzione, devi importare lo spazio dei nomi com.aspose.cells nel tuo programma o progetto. Ha diverse classi preziose per il rendering e la stampa, ad esempio,SheetRender, ImageOrPrintOptions, Rendering cartella di lavoro, e altri.
Il**com.aspose.cells.ImageOrPrintOptions** class specifica che il foglio di lavoro verrà salvato nel formato SVG.
Il**SheetRender**la classe prende l’oggetto di**ImageOrPrintOptions** come parametro che imposta il formato di salvataggio sul formato SVG.
Il seguente frammento di codice mostra come convertire un foglio di lavoro in un file Excel in un file immagine SVG.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(ConvertingWorksheetToSVG.class) + "loading_saving/"; | |
String path = dataDir + "Book1.xlsx"; | |
// Create a workbook object from the template file | |
Workbook workbook = new Workbook(path); | |
// Convert each worksheet into svg format in a single page. | |
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions(); | |
imgOptions.setSaveFormat(SaveFormat.SVG); | |
imgOptions.setOnePagePerSheet(true); | |
// Convert each worksheet into svg format | |
int sheetCount = workbook.getWorksheets().getCount(); | |
for (int i = 0; i < sheetCount; i++) { | |
Worksheet sheet = workbook.getWorksheets().get(i); | |
SheetRender sr = new SheetRender(sheet, imgOptions); | |
for (int k = 0; k < sr.getPageCount(); k++) { | |
// Output the worksheet into Svg image format | |
sr.toImage(k, path + sheet.getName() + k + "_out.svg"); | |
} | |
} | |
// Print message | |
System.out.println("Excel to SVG conversion completed successfully."); |
Renderizza il foglio di lavoro attivo in una cartella di lavoro
Un modo semplice per convertire il foglio di lavoro attivo in una cartella di lavoro consiste nell’impostare l’indice del foglio attivo e quindi salvare la cartella di lavoro come SVG. Verificherà il foglio attivo su SVG. Il seguente codice di esempio dimostra questa funzione:
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
String outputDir = Utils.Get_OutputDirectory(); | |
// Instantiate a workbook | |
Workbook workbook = new Workbook(); | |
// Put sample text in the first cell of first worksheet in the newly created workbook | |
workbook.getWorksheets().get(0).getCells().get("A1").setValue("DEMO TEXT ON SHEET1"); | |
// Add second worksheet in the workbook | |
workbook.getWorksheets().add(SheetType.WORKSHEET); | |
// Set text in first cell of the second sheet | |
workbook.getWorksheets().get(1).getCells().get("A1").setValue("DEMO TEXT ON SHEET2"); | |
// Set currently active sheet index to 1 i.e. Sheet2 | |
workbook.getWorksheets().setActiveSheetIndex(1); | |
// Save workbook to SVG. It shall render the active sheet only to SVG | |
workbook.save(outputDir + "ConvertActiveWorksheetToSVG_out.svg"); |