Convert Excel Charts to SVG using Java

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

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:

  1. Firstly, load an Excel file using the Workbook class.
  2. Next, access the worksheet that has a chart to convert from worksheets collection, either by its index (zero-based) or by name.
  3. Then, access the chart to convert by its index (zero-based) from charts collection.
  4. After that, set the ImageOrPrintOptions.setSaveFormat to SVG.
  5. 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.

Convert Excel Charts to SVG in Java

Convert Excel Charts to SVG in Java.

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:

  1. Firstly, load an Excel file using the Workbook class.
  2. Next, access the worksheet that has a chart to convert from worksheets collection, either by its index (zero-based) or by name.
  3. Then, access the chart to export by its index (zero-based) from charts collection.
  4. Set the ImageOrPrintOptions.setSaveFormat to SVG.
  5. After that, set ImageOrPrintOptions.setSVGFitToViewPort to true.
  6. 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.

Export Chart and Scale SVG to Fit Viewport in Java

Export Chart and Scale SVG to Fit Viewport in Java.

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.

See Also