This tutorial explains how to convert Excel chart to JPG in Java. Using the same code, you can convert the chart to any other format as well by setting the desired image type. Following are the steps to convert excel graph to jpg in Java.
Steps to Convert Excel Chart to JPG in Java
- Using Maven, add Aspose.Cells library to your project
- Add references to Workbook and Chart classes using the Import statement
- Load the source Excel file into the Workbook object having a chart
- Get a reference to the chart object in the respective workbook sheet
- Save the chart object as JPG image file
In these steps, we learned how to save Excel chart as JPG in Java. This process is quite simple where first the Excel file having a chart is loaded into Workbook object and then chart reference is obtained from the desired sheet. Once the chart object reference is available, you can save the chart as JPG image file.
Code to Convert Excel Graph to JPG in Java
This code demonstrates how to save Excel chart to JPG in Java. First, a Workbook class object is loaded with the target Excel file having a chart. Using the worksheets collection in the workbook object, you can refer to any sheet using the zero-based index. Finally, you can access any chart from the charts collection in the target worksheet using a zero-based index. This chart object is saved as JPG file by just providing the output image file name with extension JPG.
If you want to save this chart object to any other format, you may use the ImageOrPrintOptions class object in Chart.toImage() function. For converting the complete workbook to any other format say PDF, refer to the article how to generate PDF from Excel in Java. Note that this conversion is neither dependent on MS Excel nor Interop.