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 (rapporti scritti in Microsoft Word, ad esempio, o presentazioni PowerPoint).
Aspose.Cells fornisce l’esportazione di immagini attraverso la classe SheetRender. Questa classe rappresenta il foglio di lavoro che verrà reso in un’immagine. La classe SheetRender fornisce il metodo toImage() 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 PNG in un file Microsoft Excel.
// 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 per Scalable Vector Graphics. SVG è una specifica basata su standard XML per grafica vettoriale bidimensionale. È uno standard aperto che è stato in fase di sviluppo da parte del World Wide Web Consortium (W3C) dal 1999.
Dalla versione v7.1.0, Aspose.Cells for Java può convertire i fogli di lavoro in immagini SVG.
Per utilizzare questa funzionalità, è necessario importare lo spazio dei nomi com.aspose.cells nel proprio programma o progetto. Ha diverse classi preziose per il rendering e la stampa, ad esempio, SheetRender, ImageOrPrintOptions, WorkbookRender, e altri.
La classe com.aspose.cells.ImageOrPrintOptions specifica che il foglio di lavoro verrà salvato in formato SVG.
La classe SheetRender prende l’oggetto di ImageOrPrintOptions come parametro che imposta il formato di salvataggio in formato SVG.
Il seguente frammento di codice mostra come convertire un foglio di lavoro 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 attivo in un libro di lavoro
Un modo semplice per convertire il foglio attivo in un libro di lavoro è impostare l’indice del foglio attivo e quindi salvare il libro di lavoro come SVG. Renderizzerà il foglio attivo in SVG. Il codice di esempio seguente 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"); |