SVG (Scalable Vector Graphics) is an XML-based vector image format that stores an image in a two-dimensional vector graphic format. SVG images can also be edited with any text editor. We can convert data charts from Excel workbooks to SVG files programmatically. In this article, we will learn how to convert Excel charts to SVG using Java.
The following topics shall be covered in this article:
- Java API to Convert Excel Charts to SVG
- Convert Excel Charts to SVG in Java
- Export Chart and Scale SVG to Fit Viewport
Java API to Convert Excel Charts to SVG
For converting charts from XLSX files to SVG, we will be using Aspose.Cells for Java API. It allows performing Excel automation features programmatically without needing a Microsoft Excel application. Please either download the JAR of the API or just add the following pom.xml configuration in a Maven-based Java application.
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://repository.aspose.com/repo/</url>
</repository>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-cells</artifactId>
<version>21.12</version>
</dependency>
Convert Excel Charts to SVG in Java
We can convert charts from Excel worksheets to SVG by following the steps given below:
- Firstly, load an Excel file using the Workbook class.
- Next, access the worksheet that has a chart to convert from worksheets collection, either by its index (zero-based) or by name.
- Then, access the chart to convert by its index (zero-based) from charts collection.
- After that, set the ImageOrPrintOptions.setSaveFormat to SVG.
- Finally, convert the chart to SVG using the Chart.toImage() method and save the output file.
The following sample code shows how to convert a chart from Excel to SVG using Java.
// This code example demonstrates how to convert chart from Excel to SVG | |
// Load Excel file in workbook object | |
Workbook workbook = new Workbook("C:\\Files\\Cells\\Sample_Chart.xlsx"); | |
// Access the first worksheet | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Access the first chart inside the worksheet | |
Chart chart = worksheet.getCharts().get(0); | |
// Save the chart into image in SVG format | |
ImageOrPrintOptions options = new ImageOrPrintOptions(); | |
options.setSaveFormat(SaveFormat.SVG); | |
chart.toImage("C:\\Files\\Cells\\Sample_Chart_out.svg", options); |
Export Chart and Scale SVG to Fit Viewport in Java
In XML, the viewBox attribute defines the position and dimension for the content of the SVG viewport. We can export any chart from Excel worksheets to SVG and set it to fit in the viewport by following the steps given below:
- Firstly, load an Excel file using the Workbook class.
- Next, access the worksheet that has a chart to convert from worksheets collection, either by its index (zero-based) or by name.
- Then, access the chart to export by its index (zero-based) from charts collection.
- Set the ImageOrPrintOptions.setSaveFormat to SVG.
- After that, set ImageOrPrintOptions.setSVGFitToViewPort to true.
- Finally, call the Chart.toImage() method to save the output file.
The following sample code shows how to export a chart from Excel to SVG to fit in the viewport using Java.
// This code example demonstrates how to convert chart from Excel to SVG and set it to fit in viewport | |
// Load Excel file in workbook object | |
Workbook workbook = new Workbook("C:\\Files\\Cells\\Sample_Chart.xlsx"); | |
// Access the first worksheet | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Access the first chart inside the worksheet | |
Chart chart = worksheet.getCharts().get(0); | |
// Set image or print options | |
// with SVGFitToViewPort true | |
ImageOrPrintOptions options = new ImageOrPrintOptions(); | |
options.setSaveFormat(SaveFormat.SVG); | |
options.setSVGFitToViewPort(true); | |
chart.toImage("C:\\Files\\Cells\\Sample_Chart_ViewPort_out.svg", options); |
Get a Free License
Please try the API without evaluation limitations by requesting a free temporary license.
Conclusion
In this article, we have learned how to convert a chart from Excel to SVG in Java. We have also seen how to export an Excel chart to SVG to fit in the viewport programmatically. Besides, you can learn more about Aspose.Cells for Java API using the documentation. In case of any ambiguity, please feel free to contact us on the forum.