SVG, short for Scalable Vector Graphics, is a web-friendly vector image format. SVG has many advantages over other image formats. One of the most significant advantages is resolution independence, which means you can resize SVG images as needed without losing image quality. Sometimes, you may need to convert Excel files to SVG for web viewing. This article will demonstrate how to programmatically convert Excel to SVG in C# and VB.NET using Spire.XLS for .NET.
Install Spire.XLS for .NET
To begin with, you need to add the DLL files included in the Spire.XLS for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.XLS
Convert an Excel Worksheet to SVG using C# and VB.NET
Spire.XLS provides the Worksheet.SaveToSVGStream() method to convert an Excel worksheet to SVG. The detailed steps are as follows:
- Initialize an instance of the Workbook class.
- Load an Excel file using Workbook.LoadFromFile() method.
- Get a specific worksheet by its index through Workbook.Worksheets[int] property.
- Initialize an instance of the FileStream class.
- Save the worksheet to SVG using Worksheet.ToSVGStream(Stream, int, int, int, int) method.
- C#
- VB.NET
using Spire.Xls; using System.IO; namespace ConvertWorksheetToSVG { class Program { static void Main(string[] args) { //Create an instance of Workbook class Workbook workbook = new Workbook(); //Load an Excel file workbook.LoadFromFile("Sample1.xlsx"); //Get the first worksheet Worksheet sheet = workbook.Worksheets[0]; //Create an instance of FileStream class FileStream fs = new FileStream("E:\\Program Files\\WorksheetToSVG.svg", FileMode.Create); //Save the worksheet to SVG sheet.ToSVGStream(fs, 0, 0, 0, 0); fs.Flush(); fs.Close(); } } }
Convert an Excel Chart Sheet to SVG using C# and VB.NET
A chart sheet is a worksheet that only contains a chart. Spire.XLS allows you to convert a chart sheet to SVG by using the ChartSheet.ToSVGStream() method. The detailed steps are as follows:
- Initialize an instance of the Workbook class.
- Load an Excel file using Workbook.LoadFromFile() method.
- Get a specific chart sheet by its index through Workbook.Chartsheets[int] property.
- Initialize an instance of the FileStream class.
- Save the chart sheet to SVG using ChartSheet.ToSVGStream(Stream) method.
- C#
- VB.NET
using Spire.Xls; using System.IO; namespace ConvertChartSheetToSVG { class Program { static void Main(string[] args) { //Create an instance of Workbook class Workbook workbook = new Workbook(); //Load an Excel file workbook.LoadFromFile("Sample2.xlsx"); //Get the first chart sheet ChartSheet chartSheet = workbook.Chartsheets[0]; //Create an instance of FileStream class FileStream fs = new FileStream("E:\\ProgramFiles\\ChartSheetToSVG.svg", FileMode.Create); //Save the chart sheet to SVG chartSheet.ToSVGStream(fs); fs.Flush(); fs.Close(); } } }
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.