Rendere il foglio di lavoro e il workbook come immagine utilizzando ImageOrPrintOptions
Panoramica
A volte potresti aver bisogno di presentare i tuoi fogli di lavoro come rappresentazione pittorica. Devi presentare le immagini del foglio di lavoro nelle tue applicazioni o pagine web. Potresti aver bisogno di inserire le immagini in un documento di Word, un file PDF, una presentazione PowerPoint, o utilizzarle in qualche altro scenario. Semplicemente desideri un foglio di lavoro rappresentato come un’immagine in modo da poterlo utilizzare altrove. Aspose.Cells supporta la conversione di fogli di lavoro in file Excel in immagini. Inoltre, Aspose.Cells supporta impostare diverse opzioni come formato immagine, risoluzione (sia verticale che orizzontale), qualità dell’immagine e altre opzioni di immagine e stampa.
L’API fornisce diverse classi preziose, ad esempio, SheetRender, ImageOrPrintOptions, WorkbookRender, ecc.
La classe SheetRender gestisce il compito di renderizzare le immagini per il foglio di lavoro, mentre la classe WorkbookRender fa lo stesso per un workbook. Entrambe le suddette classi hanno diverse versioni sovraccaricate del metodo toImage che possono convertire direttamente un foglio di lavoro o un workbook in file di immagine specificati con i tuoi attributi o opzioni desiderati. Puoi salvare il file immagine su disco/stream. Ci sono diversi formati di immagine supportati, ad esempio BMP, PNG, GIFF, JPEG, TIFF, EMF e così via.
Convertire foglio di lavoro in immagine
// 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(ConvertWorksheettoImage.class) + "TechnicalArticles/"; | |
//Instantiate a new Workbook object | |
//Open template | |
Workbook book = new Workbook(dataDir + "book1.xlsx"); | |
//Get the first worksheet | |
Worksheet sheet = book.getWorksheets().get(0); | |
//Apply different Image and Print options | |
ImageOrPrintOptions options = new ImageOrPrintOptions(); | |
//Set Horizontal Resolution | |
options.setHorizontalResolution(300); | |
//Set Vertical Resolution | |
options.setVerticalResolution(300); | |
//Set TiffCompression | |
options.setTiffCompression(TiffCompression.COMPRESSION_LZW); | |
//Set Image Format | |
options.setImageType(ImageType.TIFF); | |
//Set printing page type | |
options.setPrintingPage(PrintingPageType.DEFAULT); | |
//Render the sheet with respect to specified image/print options | |
SheetRender sr = new SheetRender(sheet, options); | |
//Render/save the image for the sheet | |
sr.toImage(0, dataDir + "CWorksheettoImage_out.tiff"); |
Opzioni di conversione
È possibile salvare pagine specifiche in immagine. Il codice seguente converte il primo e il secondo foglio di lavoro in un libro di lavoro in immagini JPG.
// 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(ConversionOptions.class) + "TechnicalArticles/"; | |
// Instantiate a new Workbook object | |
// Open template | |
Workbook book = new Workbook(dataDir + "book1.xlsx"); | |
// Get the first worksheet | |
Worksheet sheet = book.getWorksheets().get(0); | |
// Apply different Image and Print options | |
ImageOrPrintOptions options = new ImageOrPrintOptions(); | |
// Set Horizontal Resolution | |
options.setHorizontalResolution(300); | |
// Set Vertical Resolution | |
options.setVerticalResolution(300); | |
// Set Image Format | |
options.setImageType(ImageType.JPEG); | |
// If you want entire sheet as a single image | |
options.setOnePagePerSheet(true); | |
// Render the sheet with respect to specified image/print options | |
SheetRender sr = new SheetRender(sheet, options); | |
// Render/save the image for the sheet | |
sr.toImage(0, dataDir + "ConversionOptions_out.jpg"); |
Oppure è possibile ciclare attraverso il workbook e renderizzare ogni foglio di lavoro in una immagine separata:
// 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(WorksheetToSeparateImage.class) + "TechnicalArticles/"; | |
// Instantiate a new Workbook object | |
// Open template | |
Workbook book = new Workbook(dataDir + "book1.xlsx"); | |
// Iterate over all worksheets in the workbook | |
for (int i = 0; i < book.getWorksheets().getCount(); i++) { | |
Worksheet sheet = book.getWorksheets().get(i); | |
// Apply different Image and Print options | |
ImageOrPrintOptions options = new ImageOrPrintOptions(); | |
// Set Horizontal Resolution | |
options.setHorizontalResolution(300); | |
// Set Vertical Resolution | |
options.setVerticalResolution(300); | |
// Set Image Format | |
options.setImageType(ImageType.JPEG); | |
// If you want entire sheet as a single image | |
options.setOnePagePerSheet(true); | |
// Render to image | |
SheetRender sr = new SheetRender(sheet, options); | |
sr.toImage(0, dataDir + "WSheetToSImage_out-" + sheet.getName() + ".jpg"); | |
} |
Convertire workbook in immagine:
Per renderizzare l’intero workbook in formato immagine, è possibile utilizzare l’approccio sopra descritto o semplicemente utilizzare la classe WorkbookRender che accetta un’istanza di Workbook e l’oggetto di ImageOrPrintOptions.
È possibile salvare l’intero workbook in un’unica immagine TIFF con più fotogrammi o pagine:
// 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(ConvertWorkbooktoImage.class) + "TechnicalArticles/"; | |
// Instantiate a new Workbook object | |
Workbook book = new Workbook(dataDir + "book1.xlsx"); | |
// Apply different Image and Print options | |
ImageOrPrintOptions options = new ImageOrPrintOptions(); | |
// Set Image Format | |
options.setImageType(ImageType.TIFF); | |
// If you want entire sheet as a single image | |
options.setOnePagePerSheet(true); | |
// Render to image | |
WorkbookRender render = new WorkbookRender(book, options); | |
render.toImage(dataDir + "CWorkbooktoImage_out.tiff"); |