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

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