How to Convert Excel Sheet to Image in Java

This quick tutorial directs how to convert Excel sheet to image in Java. It contains the guidance to configure the environment and detailed steps along with the runnable sample code to demonstrate the conversion of a sheet in XLSX to JPEG. While using Java Excel to image conversion can be performed with a few lines of code without using any other third-party tool or software installed on the system.

Steps to Convert Excel Sheet to Image in Java

  1. Add a reference to Aspose.Cells from the Maven repository to convert sheet to the image
  2. Load the source Excel file for conversion to image using the Workbook class instance
  3. Instantiate the ImageOrPrintOptions class object to customize the output image
  4. Set the flag to auto-fit columns based on the cell contents and image type
  5. Select the desired sheet to be rendered in the image
  6. Create a SheetRender class object for the selected sheet using the configured ImageOrPrintOptions settings
  7. Parse through all the pages in the print preview and render each page as an image

These steps describe the order of operations required to convert Excel file to image in Java such that each page in a particular worksheet is rendered to a separate image. You can parse through all the worksheets one by one to convert the entire workbook to images. After loading the source Excel file, you can use ImageOrPrintOptions class object to configure output image(s) like set image type or auto-fit the columns to display entire contents in each cell and then use this configuration while rendering of sheets with the help of SheetRender class object.

Code to Create Excel Worksheet Image in Java

import com.aspose.cells.ImageOrPrintOptions;
import com.aspose.cells.ImageType;
import com.aspose.cells.License;
import com.aspose.cells.SheetRender;
import com.aspose.cells.Workbook;
import com.aspose.cells.Worksheet;
public class ConvertExcelSheetToImageInJava {
public static void main(String[] args) throws Exception {//main function to convert Excel sheet to image
// Instantiate the license to avoid trial version watermark in the output images
License licenseForExcelToImage = new License();
licenseForExcelToImage.setLicense("Aspose.Cells.lic");
// Load the Excel file required to be converted to images
Workbook bookToImages = new Workbook("MyTestBook1.xlsx");
// Create an instance of ImageOrPrintOptions to customize the output images
ImageOrPrintOptions exportedImgOptions = new ImageOrPrintOptions();
// Set the flag to auto-fit column width of each cell according to the size of contents
exportedImgOptions.setCellAutoFit(true);
// Set the image type to JPEG exported from the Excel worksheet
exportedImgOptions.setImageType(ImageType.JPEG);
// Select the sheet from the collection that is to be rendered to images
Worksheet sheetToImage = bookToImages.getWorksheets().get(0);
// Create and initialize an instance of SheetRender with target sheet and image configurations
SheetRender sheetRenderToImage = new SheetRender(sheetToImage, exportedImgOptions);
// Parse through all the pages in sheet to render as image
for (int j = 0; j < sheetRenderToImage.getPageCount(); j++)
{
// Save each image to file generated by the SheetRender class object
sheetRenderToImage.toImage(j, "ToImage-out" + j + ".jpg");
}
System.out.println("Done");
}
}

ImageOrPrintOptions class object is used to configure the output images that contains a lot of other properties like you can use setAllColumnsInOnePagePerSheet(true) to display all columns in one page, setDefaultFont(fontName) to set font when characters in the Excel file are Unicode, setHorizontalResolution() and setVerticalResolution() to set the image resolution, setTextCrossType() to define style when text length is more than the cell width, to name a few. Similarly while writing Excel to image converter in Java, a method setDesiredSize() is available to set the size of the output image which needs width and height as parameters.

We have learned how using Java Excel to image conversion can be performed. If you want to learn the conversion of Excel to HTML, refer to the article on how to convert Excel to HTML in Java.

 English