Converting Worksheet to Different Image Formats

Converting Worksheet to Image

Sometimes, it is useful to save a picture of a worksheet. Images can be shared online, inserted into other documents (reports written in Microsoft Word, for example, or PowerPoint presentations).

Aspose.Cells provides image export through the SheetRender class. This class represents the worksheet that will be rendered to an image. The SheetRender class provides the toImage() method for converting a worksheet to an image file. BMP, PNG, JPEG, TIFF, and EMF formats are supported.

The code below shows how to convert a worksheet in a Microsoft Excel file to a PNG file.

// 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(WorksheetToImage.class) + "LoadingSavingConvertingAndManaging/";
// Instantiate a new workbook with path to an Excel file
Workbook book = new Workbook(dataDir + "MyTestBook1.xlsx");
// Create an object for ImageOptions
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
// Set the image type
imgOptions.setImageType(ImageType.PNG);
// Get the first worksheet.
Worksheet sheet = book.getWorksheets().get(0);
// Create a SheetRender object for the target sheet
SheetRender sr = new SheetRender(sheet, imgOptions);
for (int j = 0; j < sr.getPageCount(); j++) {
// Generate an image for the worksheet
sr.toImage(j, dataDir + "WToImage-out" + j + ".png");
}

Converting Worksheet to SVG

SVG stands for Scalable Vector Graphics. SVG is a specification based on XML standards for two-dimensional vector graphics. It is an open standard that has been under development by the World Wide Web Consortium (W3C) since 1999.

Since the release of v7.1.0, Aspose.Cells for Java can convert worksheets into SVG images.

To use this feature, you need to import the com.aspose.cells namespace to your program or project. It has several valuable classes for rendering and printing, for example, SheetRender, ImageOrPrintOptions, WorkbookRender, and others.

The com.aspose.cells.ImageOrPrintOptions class specifies that the worksheet will be saved in SVG format.

The SheetRender class takes the object of ImageOrPrintOptions as a parameter that sets the save format to SVG format.

The following code snippet shows how to convert a worksheet in an Excel file to an SVG image file.

// 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(ConvertingWorksheetToSVG.class) + "loading_saving/";
String path = dataDir + "Book1.xlsx";
// Create a workbook object from the template file
Workbook workbook = new Workbook(path);
// Convert each worksheet into svg format in a single page.
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
imgOptions.setSaveFormat(SaveFormat.SVG);
imgOptions.setOnePagePerSheet(true);
// Convert each worksheet into svg format
int sheetCount = workbook.getWorksheets().getCount();
for (int i = 0; i < sheetCount; i++) {
Worksheet sheet = workbook.getWorksheets().get(i);
SheetRender sr = new SheetRender(sheet, imgOptions);
for (int k = 0; k < sr.getPageCount(); k++) {
// Output the worksheet into Svg image format
sr.toImage(k, path + sheet.getName() + k + "_out.svg");
}
}
// Print message
System.out.println("Excel to SVG conversion completed successfully.");

Render active worksheet in a workbook

A simple way to convert active worksheet in a workbook is to set the active sheet index and then save the workbook as SVG. It will render the active sheet to SVG. Following sample code demonstrates this feature:

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
String outputDir = Utils.Get_OutputDirectory();
// Instantiate a workbook
Workbook workbook = new Workbook();
// Put sample text in the first cell of first worksheet in the newly created workbook
workbook.getWorksheets().get(0).getCells().get("A1").setValue("DEMO TEXT ON SHEET1");
// Add second worksheet in the workbook
workbook.getWorksheets().add(SheetType.WORKSHEET);
// Set text in first cell of the second sheet
workbook.getWorksheets().get(1).getCells().get("A1").setValue("DEMO TEXT ON SHEET2");
// Set currently active sheet index to 1 i.e. Sheet2
workbook.getWorksheets().setActiveSheetIndex(1);
// Save workbook to SVG. It shall render the active sheet only to SVG
workbook.save(outputDir + "ConvertActiveWorksheetToSVG_out.svg");
  • 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