Converting Worksheet to Image and Worksheet to Image by Page
This document is designed to provide the developers with a detailed understanding of how to convert a worksheet to an image file & worksheet with multiple pages to an image file per page.
Sometimes, you might need to present worksheets as images, for example, to use them in applications or web pages. You might need to insert the images into a Word document, a PDF file, a PowerPoint presentation, or use them in some other scenario. Simply, you want to render the worksheet as an image. Aspose.Cells APIs support converting worksheets in Microsoft Excel files to images. Also, Aspose.Cells supports converting a workbook to multiple image files, one per page.
Using Aspose.Cells to Convert Worksheet to Image File
This article shows how to use Aspose.Cells for Java API to convert a worksheet to image. The API provides several valuable classes, such as SheetRender, ImageOrPrintOptions, WorkbookRender, and so on. The SheetRender class represents a worksheet to render images for the worksheet and has an overloaded toImage method that can convert a worksheet to image files directly with any attributes or options set.
// 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(ConvertWorksheettoImageFile.class) + "TechnicalArticles/"; | |
// Create a new Workbook object | |
// Open a template excel file | |
Workbook book = new Workbook(dataDir + "book1.xlsx"); | |
// Get the first worksheet | |
Worksheet sheet = book.getWorksheets().get(0); | |
// Define ImageOrPrintOptions | |
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions(); | |
// Specify the image format | |
imgOptions.setImageType(ImageType.JPEG); | |
// Render the sheet with respect to specified image/print options | |
SheetRender render = new SheetRender(sheet, imgOptions); | |
// Render the image for the sheet | |
render.toImage(0, dataDir + "CWToImageFile.jpg"); |
Result
After executing the above code, the worksheet named Sheet1 is converted into an image file SheetImage.jpg.
The output JPG
Using Aspose.Cells to Convert Worksheet to Image File by Page
This example shows how to use Aspose.Cells to convert a worksheet from a template workbook that has several pages to one image file per page.
// 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(ConvertWorksheetToImageByPage.class) + "TechnicalArticles/"; | |
// Create a new Workbook object | |
// Open a template excel file | |
Workbook book = new Workbook(dataDir + "ConvertWorksheetToImageByPage.xlsx"); | |
// Get the first worksheet | |
Worksheet sheet = book.getWorksheets().get(0); | |
// Define ImageOrPrintOptions | |
ImageOrPrintOptions options = new ImageOrPrintOptions(); | |
// Set Resolution | |
options.setHorizontalResolution(200); | |
options.setVerticalResolution(200); | |
options.setImageType(ImageType.TIFF); | |
// Sheet2Image by page conversion | |
SheetRender render = new SheetRender(sheet, options); | |
for (int j = 0; j < render.getPageCount(); j++) { | |
render.toImage(j, dataDir + sheet.getName() + " Page" + (j + 1) + ".tif"); | |
} |
Result
After executing the above code, the worksheet named Sheet1 is converted into two image files (1 per page) Sheet 1 Page 1.Tiff and Sheet 1 Page 2.Tiff.
Generated image file (Sheet 1 Page 1.Tiff)
Generated image file (Sheet 1 Page 2.Tiff)