GcExcel allows you to convert a worksheet, any specified range and various shape types to images. Hence, making it convenient to use the converted images directly in other documents, like Word, PDF or a PPT document. The supported image formats for conversion are PNG, JPG/JPEG, EMF, GIF and WMF.
A worksheet can be converted to image using the toImage method of IWorksheet interface. The converted image displays the rectangular area of the worksheet enclosed under cell A1 and the last cell where any data or shape is present. For eg, if a worksheet contains a shape or data in the range D5:F9, the converted image will display the area under the range A1:F9.
A blank worksheet cannot be converted to image.
Refer to the following example code to convert a worksheet to image.
Java |
Copy Code |
---|---|
Workbook workbook = new Workbook(); IWorksheet worksheet = workbook.getWorksheets().get(0); //Add data worksheet.getRange("S50").setValue(10); //Save worksheet to image worksheet.toImage("ConvertWorksheetToImage.png"); |
Refer to the following example code to convert a worksheet to image from existing file.
Java |
Copy Code |
---|---|
// create a png file stream FileOutputStream outputStream = new FileOutputStream("ConvertWorksheetToImage.png"); // create a new workbook Workbook workbook = new Workbook(); FileInputStream fileStream = new FileInputStream("Worksheet.xlsx"); // Open a xlsx file contains a group shape workbook.open(fileStream); IWorksheet worksheet = workbook.getWorksheets().get(0); // Export the shape to image worksheet.toImage(outputStream, ImageType.PNG); // close the image stream outputStream.close(); |
A specific range in a worksheet can be converted to image using the toImage method of the IRange interface. The resulting image displays the rectangular area of the worksheet enclosed under the specified range.
Refer to the following example code to convert a specified range to image.
Java |
Copy Code |
---|---|
Workbook workbook = new Workbook(); IWorksheet worksheet = workbook.getWorksheets().get(0); //Add data worksheet.getRange("D10:F14").setValue(new Object[][] {{ "Device", "Quantity", "Unit Price" },{ "T540p", 12, 9850 }, { "T570", 5, 7460 }, { "Y460", 6, 5400 }, { "Y460F", 8, 6240 }}); IRange range = worksheet.getRange("D10:F14"); //Save range to image range.toImage("ConvertRangeToImage.png"); |
Refer to the following example code to convert a specified range to image from an existing file.
Java |
Copy Code |
---|---|
// create a png file stream FileOutputStream outputStream = new FileOutputStream("ConvertRangeToImage.png"); // create a new workbook Workbook workbook = new Workbook(); FileInputStream fileStream = new FileInputStream("RangeWorkbook.xlsx"); // Open a xlsx file contains a group shape workbook.open(fileStream); IWorksheet worksheet = workbook.getWorksheets().get(0); // Export the shape to image worksheet.getRange("A1:C5").toImage(outputStream, ImageType.PNG); // close the image stream outputStream.close(); |
GcExcel allows you to convert various shape types to image using the toImage method of the IShape interface. The shape types include shapes like chart, picture, slicer and autoshape. The resulting image displays the rectangular area of the worksheet enclosed under the shape.
Refer to the following example code to convert an autoshape to image.
Java |
Copy Code |
---|---|
Workbook workbook = new Workbook(); IWorksheet worksheet = workbook.getWorksheets().get(0); //Add an oval IShape shape = worksheet.getShapes().addShape(AutoShapeType.Oval, 20, 20, 200, 100); //Save oval to image shape.toImage("ConvertShapeToImage.png"); |
Refer to the following example code to convert an autoshape to image from existing file.
Java |
Copy Code |
---|---|
// create a png file stream FileOutputStream outputStream = new FileOutputStream("ConvertShapeToImage.png"); // create a new workbook Workbook workbook = new Workbook(); FileInputStream fileStream = new FileInputStream("ShapeWorkbook.xlsx"); // Open a xlsx file contains a group shape workbook.open(fileStream); IWorksheet worksheet = workbook.getWorksheets().get(0); // Export the shape to image worksheet.getShapes().get(0).toImage(outputStream, ImageType.PNG); // close the image stream outputStream.close(); |