Export Worksheet or Chart into Image with Desired Width and Height

Export Worksheet into Image with Desired Width and Height

The following code exports the worksheet into an image with 400x400 size.

// 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(ExportWorksheettoImage.class) + "TechnicalArticles/";
// Create workbook object from source file
Workbook workbook = new Workbook(dataDir + "source.xlsx");
// Access first worksheet
Worksheet worksheet = workbook.getWorksheets().get(0);
/*
* Set image or print options, We want one page per sheet, The image format is in png And desired dimensions are
* 400x400
*/
ImageOrPrintOptions opts = new ImageOrPrintOptions();
opts.setOnePagePerSheet(true);
opts.setImageType(ImageType.PNG);
opts.setDesiredSize(400, 400);
// Render sheet into image
SheetRender sr = new SheetRender(worksheet, opts);
sr.toImage(0, dataDir + "EWSheetToImage_out.png");