Skip to content

Instantly share code, notes, and snippets.

@aspose-com-kb
Last active March 29, 2022 02:26
Show Gist options
  • Save aspose-com-kb/a4619c69ac2e7a96cdbc493fbdd7351f to your computer and use it in GitHub Desktop.
Save aspose-com-kb/a4619c69ac2e7a96cdbc493fbdd7351f to your computer and use it in GitHub Desktop.
Code to Convert Excel Sheet To Image In Java. Refer here for more details: https://kb.aspose.com/cells/java/how-to-convert-excel-sheet-to-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");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment