DEV Community

lu liu
lu liu

Posted on

Convert Excel to SVG in Java: A Comprehensive Guide

Transforming tabular data into scalable vector graphics (SVG) is often essential for web applications, high-quality printing, and dynamic visualizations. This tutorial addresses the common challenge of converting Excel files to SVG format using Java. We'll leverage the powerful Spire.XLS for Java library to provide a straightforward and efficient solution for this conversion.

Spire.XLS for Java: Introduction and Installation

Spire.XLS for Java is a professional Excel API that enables developers to create, read, write, convert, and print Excel files in various formats without relying on Microsoft Excel. It offers extensive features for managing worksheets, cells, charts, formulas, and more. Its robust capabilities make it an ideal choice for programmatic Excel manipulation and conversion tasks.

To integrate Spire.XLS into your Java project, you typically add its dependency to your pom.xml file if you're using Maven:

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.xls</artifactId>
        <version>15.9.1</version>
    </dependency>
</dependencies>
Enter fullscreen mode Exit fullscreen mode

After adding the dependency, your project will have access to all the functionalities provided by Spire.XLS for Java.

Convert a Single Excel Worksheet to SVG

Converting a specific worksheet from an Excel file to an SVG image involves loading the workbook, selecting the desired sheet, and then calling the toSVGStream() method. This method allows you to specify the output stream where the SVG data will be written.

Here's a step-by-step guide with a complete code example:

  • Create a Workbook object: This object represents your Excel file.
  • Load the Excel file: Use the loadFromFile() method of the Workbook object, passing the path to your Excel file.
  • Get the desired Worksheet: Access a specific worksheet by its index (e.g., workbook.getWorksheets().get(1) for the first sheet) or by its name.
  • Create a FileOutputStream: This will be used to write the SVG content to a file.
  • Convert the worksheet to SVG: Call the toSVGStream() method on the Worksheet object. This method takes the output stream, and optionally the range of cells to convert. If you want to convert the entire sheet, you can specify the full range.
  • Flush and close the stream: Ensure all data is written to the file and release system resources.
import com.spire.xls.*;
import java.io.FileOutputStream;
import java.io.IOException;

public class ExcelToSVG {
    public static void main(String[] args) throws IOException {

        //Create an object of Workbook class
        Workbook workbook = new Workbook();

        //Load an Excel document from disk
        workbook.loadFromFile("C:/Samples/Sample.xlsx");

        //Get the second sheet
        Worksheet sheet = workbook.getWorksheets().get(1);

        //Convert the worksheet to an SVG file
        FileOutputStream stream = new FileOutputStream("heet.svg");
        sheet.toSVGStream(stream, sheet.getFirstRow(), sheet.getFirstColumn(), sheet.getLastRow(), sheet.getLastColumn());
        stream.flush();
        stream.close();

    }
}
Enter fullscreen mode Exit fullscreen mode

This code snippet effectively demonstrates how to save excel as svg for a single worksheet, providing a clean and scalable vector representation of your data.

Convert an Entire Excel Workbook to SVG

When you need to convert an entire Excel workbook, which might contain multiple worksheets, to SVG, you'll typically iterate through each worksheet and save each one as a separate SVG file. This approach ensures that all relevant data is converted, with each sheet maintaining its independent SVG representation.

Here's the process:

  • Create a Workbook object and load your Excel file as shown previously. Iterate through all worksheets: Use a loop to access each Worksheet object within the workbook.getWorksheets() collection.
  • For each worksheet:
    • Construct a unique output file name for the SVG (e.g., using the sheet name or index).
    • Create a FileOutputStream for that specific SVG file.
    • Call the toSVGStream() method on the current Worksheet to convert it to SVG.
  • Flush and close the stream.

Handle potential chart sheets: If your workbook contains chart sheets (sheets dedicated entirely to a chart), Spire.XLS provides a toSVGStream() method specifically for ChartSheet objects as well.

import com.spire.xls.*;
import java.io.FileOutputStream;
import java.io.IOException;

public class ExcelToSVG {
    public static void main(String[] args) throws IOException {

        //Create an object of Workbook class
        Workbook workbook = new Workbook();

        //Load an Excel document from disk
        workbook.loadFromFile("C:/Samples/Sample.xlsx");

        //Loop through the document to get its worksheets
        for (int i = 0; i < workbook.getWorksheets().size(); i++)
        {
            FileOutputStream stream = new FileOutputStream("sheet"+i+".svg");

            //Convert a worksheet to an SVG file
            Worksheet sheet = workbook.getWorksheets().get(i);
            sheet.toSVGStream(stream, sheet.getFirstRow(), sheet.getFirstColumn(), sheet.getLastRow(), sheet.getLastColumn());
            stream.flush();
            stream.close();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

This comprehensive example demonstrates the complete excel to svg conversion process for an entire workbook, including handling both data worksheets and chart sheets, making it highly versatile.

Conclusion

This tutorial has demonstrated how to convert excel to svg in java using the Spire.XLS for Java library. By following the provided steps and code examples, you can efficiently transform single worksheets or entire workbooks into scalable vector graphics. Spire.XLS simplifies complex Excel manipulations, offering a reliable solution for integrating Excel data into various applications as high-quality SVG images. Explore its documentation for more advanced features and conversion options.

Top comments (0)