How to Convert Excel Worksheet to SVG (Scalable Vector Graphics) in C#, VB.NET

In this article, we will introduce the way of converting Excel worksheet to SVG(Scalable Vector Graphics) file format in C# and VB.NET with the help of Spire.XLS, which is a 100% standalone Excel .NET API that allows developers to create and manipulate Excel spreadsheets without requiring Microsoft Excel or Microsoft Office to be installed on system.

Please see the detail steps in the following contents.

Step 1: Instantiate a Workbook object and load the Excel workbook.

Workbook workbook = new Workbook();
workbook.LoadFromFile("Sample.xlsx");

Step 2: Create a file stream, call ToSVGStream(Stream stream, int firstRow, int firstColumn, int lastRow, int lastColumn) method to save each worksheet of the workbook to SVG stream and then save to the file stream. Finally, close the file stream.

for (int i = 0; i < workbook.Worksheets.Count; i++)
{
    FileStream fs = new FileStream(string.Format("E:\\Program Files\\sheet-{0}.svg", i), FileMode.Create);
    workbook.Worksheets[i].ToSVGStream(fs, 0, 0, 0, 0);
    fs.Flush();
    fs.Close();
}

Effective screenshot:

For better demonstration, we used a sample Excel workbook which contains two worksheets, below are the comparisons between the source worksheets and the result SVG files after converting.

How to Convert Excel Worksheet to SVG (Scalable Vector Graphics) in C#, VB.NET

How to Convert Excel Worksheet to SVG (Scalable Vector Graphics) in C#, VB.NET

Full code:

[C#]
using System.IO;
using Spire.Xls;

namespace Convert_Excel_to_SVG
{
    class Program
    {
        static void Main(string[] args)
        {
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("Sample.xlsx");
            for (int i = 0; i < workbook.Worksheets.Count; i++)
            {
                FileStream fs = new FileStream(string.Format("E:\\Program Files\\sheet-{0}.svg", i), FileMode.Create);
                workbook.Worksheets[i].ToSVGStream(fs, 0, 0, 0, 0);
                fs.Flush();
                fs.Close();
            }
        }
    }
}
[VB.NET]
Imports System.IO
Imports Spire.Xls

Namespace Convert_Excel_to_SVG
	Class Program
		Private Shared Sub Main(args As String())
			Dim workbook As New Workbook()
			workbook.LoadFromFile("Sample.xlsx")
			For i As Integer = 0 To workbook.Worksheets.Count - 1
				Dim fs As New FileStream(String.Format("sheet-{0}.svg", i), FileMode.Create)
				workbook.Worksheets(i).ToSVGStream(fs, 0, 0, 0, 0)
				fs.Flush()
				fs.Close()
			Next
		End Sub
	End Class
End Namespace