Java: Convert Excel to SVG

2022-07-05 09:19:00 Written by  support iceblue
Rate this item
(0 votes)

SVG is an XML-based scalable vector graphic format and an open standard make up language for describing graphics. SVG is now very common in webpage making because it works well with other web standards, including CSS, DOM, and JavaScript. To add office documents like Excel worksheets on webpages to display them directly is a real challenge, but this can be achieved easily by converting them to SVG images. This article will demonstrate how to convert Excel documents to SVG files with the help of Spire.XLS for Java.

Install Spire.XLS for Java

First of all, you're required to add the Spire.Xls.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<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>14.11.0</version>
    </dependency>
</dependencies>
    

Convert a Specific Sheet of an Excel Document to an SVG File

The steps are as follows:

  • Create an object of Workbook class.
  • Load an Excel document from disk using Workbook.loadFromFile() method.
  • Get the second sheet using Workbook.getWorksheets().get() method.
  • Convert the sheet to an SVG file using Worksheet.toSVGStream() method.
  • Java
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();

    }
}

Java: Convert Excel to SVG

Convert Every Sheet of an Excel Document to an SVG File

The steps are as follows:

  • Create an object of Workbook class.
  • Load an Excel document from disk using Workbook.loadFromFile() method.
  • Loop through the document to get its sheets and convert every sheet to an SVG file using Worksheet.toSVGStream() method.
  • Java
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();
        }
    }
}

Java: Convert Excel to SVG

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Additional Info

  • tutorial_title:
Last modified on Tuesday, 27 September 2022 02:33