Render the Worksheet and Workbook to Image using ImageOrPrintOptions

Overview

Sometimes, you might require presenting your worksheets as a pictorial representation. You do need to present the worksheet images into your 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 a worksheet rendered as an image so that you can use it elsewhere. Aspose.Cells supports converting worksheets in Excel files to images. Also, Aspose.Cells supports setting different options like image format, resolution (both vertical and horizontal), image quality, and other image and print options.

The API provides several valuable classes, for example, SheetRender, ImageOrPrintOptions, WorkbookRender, etc.

The SheetRender class handles the task of rendering images for the worksheet whereas the WorkbookRender does the same for a workbook. Both aforesaid classes have several overloaded versions of the toImage method that can directly convert a worksheet or a workbook to image file(s) specified with your desired attributes or options. You can save the image file to the disk/stream. There are several image formats supported, e.g BMP, PNG, GIFF, JPEG, TIFF, EMF, and so on.

Convert Worksheet to Image

// 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");

Conversion Options

It is possible to save specific pages to image. The following code converts the first and second worksheets in a workbook to JPG images.

// 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");

Or you can cycle through the workbook and render each worksheet in it to a separate image:

// 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");
}

Convert Workbook to Image:

In order to render the complete workbook to image format, you may either use the above approach or simply use the WorkbookRender class that accepts an instance of Workbook as well as the object of ImageOrPrintOptions.

You can save the whole workbook to a single TIFF image with multiply frames or pages:

// 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");
  • Converting Worksheet to Different Image Formats
  • Export Chart to SVG with viewBox attribute
  • Export Worksheet or Chart into Image with Desired Width and Height
  • Converting Worksheet to Image and Worksheet to Image by Page