Export Excel data to JSON format in C#, VB.NET
Syncfusion Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. The library also converts Excel documents to PDF, CSV, TSV, HTML, Image, JSON, etc. This article explains how to convert Excel data to JSON files in C#, VB.NET using XlSIO.
XlsIO provides a method SaveAsJson to convert Excel data to JSON files. With this, you can save Excel file as a simple JSON file or a JSON file as schema. The feature includes to save,
- workbook to JSON file,
- worksheet to JSON file,
- a range to JSON file,
- as a stream with the above features.
Steps to export Excel data to JSON file, programmatically:
Step 1: Create a new C# Windows Forms application project.
Create a new C# WinForms application
Step 2: Install the Syncfusion.XlsIO.WinForms NuGet package (18.2.0.44 and newer versions) as reference to your .NET Framework application from NuGet.Org.
Install NuGet package
Step 3: Include the following namespaces in the Form1.cs file to export Excel file to JSON format.
C#
using System.IO; using Syncfusion.XlsIO;
VB.NET
Imports System.IO Imports Syncfusion.XlsIO
Step 4: Use the below code snippets to export Excel data to JSON format in 3 buttons:
- Workbook to JSON
- Worksheet to JSON
- Range to JSON
Workbook to JSON
The following code illustrates how to convert an Excel workbook to the JSON file or JSON file stream as schema and without schema.
C#
using (ExcelEngine excelEngine = new ExcelEngine()) { IApplication application = excelEngine.Excel; application.DefaultVersion = ExcelVersion.Excel2013; IWorkbook workbook = application.Workbooks.Open("Sample.xlsx", ExcelOpenType.Automatic); if (checkBox1.Checked) { //Saves the workbook to a JSON file, as schema by default workbook.SaveAsJson("Excel-Workbook-To-JSON-as-schema-default.json"); //Saves the workbook to a JSON file as schema workbook.SaveAsJson("Excel-Workbook-To-JSON-as-schema.json", true); //Saves the workbook to a JSON filestream, as schema by default FileStream stream = new FileStream("Excel-Workbook-To-JSON-filestream-as-schema-default.json", FileMode.Create); workbook.SaveAsJson(stream); //Saves the workbook to a JSON filestream as schema FileStream stream1 = new FileStream("Excel-Workbook-To-JSON-filestream-as-schema.json", FileMode.Create); workbook.SaveAsJson(stream1, true); } else { //Saves the workbook to a JSON file without schema workbook.SaveAsJson("Excel-Workbook-To-JSON-without-schema.json", false); //Saves the workbook to a JSON filestream without schema FileStream stream = new FileStream("Excel-Workbook-To-JSON-filestream-without-schema.json", FileMode.Create); workbook.SaveAsJson(stream, false); } }
VB.NET
Using excelEngine As ExcelEngine = New ExcelEngine() Dim application As IApplication = excelEngine.Excel application.DefaultVersion = ExcelVersion.Excel2013 Dim workbook As IWorkbook = application.Workbooks.Open("Sample.xlsx", ExcelOpenType.Automatic) If CheckBox1.Checked Then 'Saves the workbook to a JSON file, as schema by default workbook.SaveAsJson("Excel-Workbook-To-JSON-as-schema-default.json") 'Saves the workbook to a JSON file as schema workbook.SaveAsJson("Excel-Workbook-To-JSON-as-schema.json", True) 'Saves the workbook to a JSON filestream as schema by default Dim stream As FileStream = New FileStream("Excel-Workbook-To-JSON-filestream-as-schema-default.json", FileMode.Create) workbook.SaveAsJson(stream) 'Saves the workbook to a JSON filestream as schema Dim stream1 As FileStream = New FileStream("Excel-Workbook-To-JSON-filestream-as-schema.json", FileMode.Create) workbook.SaveAsJson(stream1, True) Else 'Saves the workbook to a JSON file without schema workbook.SaveAsJson("Excel-Workbook-To-JSON-without-schema.json", False) 'Saves the workbook to a JSON filestream without schema Dim stream As FileStream = New FileStream("Excel-Workbook-To-JSON-filestream-without-schema.json", FileMode.Create) workbook.SaveAsJson(stream, False) End If End Using
Worksheet to JSON
The following code illustrates how to convert an Excel worksheet to the JSON file or JSON file stream with schema and without schema.
C#
using (ExcelEngine excelEngine = new ExcelEngine()) { IApplication application = excelEngine.Excel; application.DefaultVersion = ExcelVersion.Excel2013; IWorkbook workbook = application.Workbooks.Open("Sample.xlsx", ExcelOpenType.Automatic); //Active worksheet IWorksheet worksheet = workbook.Worksheets[0]; if(checkBox1.Checked) { //Saves the worksheet to a JSON file, as schema by default workbook.SaveAsJson("Excel-Worksheet-To-JSON-as-schema-default.json", worksheet); //Saves the worksheet to a JSON file as schema workbook.SaveAsJson("Excel-Worksheet-To-JSON-as-schema.json", worksheet, true); //Saves the worksheet to a JSON filestream, as schema by default FileStream stream = new FileStream("Excel-Worksheet-To-JSON-filestream-as-schema-default.json", FileMode.Create); workbook.SaveAsJson("stream", worksheet); //Saves the worksheet to a JSON filestream as schema FileStream stream1 = new FileStream("Excel-Worksheet-To-JSON-filestream-as-schema.json", FileMode.Create); workbook.SaveAsJson(stream1, worksheet, true); } else { //Saves the worksheet to a JSON file without schema workbook.SaveAsJson("Excel-Worksheet-To-JSON-without-schema.json", worksheet, false); //Saves the worksheet to a JSON filestream without schema FileStream stream = new FileStream("Excel-Worksheet-To-JSON-filestream-without-schema.json", FileMode.Create); workbook.SaveAsJson(stream, worksheet, false); } }
VB.NET
Using excelEngine As ExcelEngine = New ExcelEngine() Dim application As IApplication = excelEngine.Excel application.DefaultVersion = ExcelVersion.Excel2013 Dim workbook As IWorkbook = application.Workbooks.Open("Sample.xlsx", ExcelOpenType.Automatic) 'Active worksheet Dim worksheet As IWorksheet = workbook.Worksheets(0) If CheckBox1.Checked Then 'Saves the worksheet to a JSON file, as schema by default workbook.SaveAsJson("Excel-Worksheet-To-JSON-as-schema-default.json", worksheet) 'Saves the worksheet to a JSON file as schema workbook.SaveAsJson("Excel-Worksheet-To-JSON-as-schema.json", worksheet, True) 'Saves the worksheet to a JSON filestream as schema by default Dim stream As FileStream = New FileStream("Excel-Worksheet-To-JSON-filestream-as-schema-default.json", FileMode.Create) workbook.SaveAsJson(stream, worksheet) 'Saves the worksheet to a JSON filestream as schema Dim stream1 As FileStream = New FileStream("Excel-Worksheet-To-JSON-filestream-as-schema.json", FileMode.Create) workbook.SaveAsJson(stream1, worksheet, True) Else 'Saves the worksheet to a JSON file without schema workbook.SaveAsJson("Excel-Worksheet-To-JSON-without-schema.json", worksheet, False) 'Saves the worksheet to a JSON filestream without schema Dim stream As FileStream = New FileStream("Excel-Worksheet-To-JSON-filestream-without-schema.json", FileMode.Create) workbook.SaveAsJson(stream, worksheet, False) End If End Using
Range to JSON
The following code illustrates how to convert an Excel Custom Range to the JSON file or JSON file stream as schema and without schema.
C#
using (ExcelEngine excelEngine = new ExcelEngine()) { IApplication application = excelEngine.Excel; application.DefaultVersion = ExcelVersion.Excel2013; IWorkbook workbook = application.Workbooks.Open("Sample.xlsx", ExcelOpenType.Automatic); //Active worksheet IWorksheet worksheet = workbook.Worksheets[0]; //Custom range IRange range = worksheet.Range["A2:A5"]; if(checkBox1.Checked) { //Saves the range to a JSON file, as schema by default workbook.SaveAsJson("Excel-Range-To-JSON-as-schema-default.json", range); //Saves the range to a JSON file as schema workbook.SaveAsJson("Excel-Range-To-JSON-as-schema.json", range, true); //Saves the range to a JSON filestream, as schema by default FileStream stream = new FileStream("Excel-Range-To-JSON-filestream-as-schema-default.json", FileMode.Create); workbook.SaveAsJson(stream, range); //Saves the range to a JSON filestream as schema FileStream stream1 = new FileStream("Excel-Range-To-JSON-filestream-as-schema.json", FileMode.Create); workbook.SaveAsJson(stream1, range, true); } else { //Saves the range to a JSON file without schema workbook.SaveAsJson("Excel-Range-To-JSON-without-schema.json", range, false); //Saves the range to a JSON filestream without schema FileStream stream = new FileStream("Excel-Range-To-JSON-filestream-without-schema.json", FileMode.Create); workbook.SaveAsJson(stream, range, false); } }
VB.NET
Using excelEngine As ExcelEngine = New ExcelEngine() Dim application As IApplication = excelEngine.Excel application.DefaultVersion = ExcelVersion.Excel2013 Dim workbook As IWorkbook = application.Workbooks.Open("Sample.xlsx", ExcelOpenType.Automatic) 'Active worksheet Dim worksheet As IWorksheet = workbook.Worksheets(0) 'Custom range Dim range As IRange = worksheet.Range("A2:A5") If CheckBox1.Checked Then 'Saves the range to a JSON file, as schema by default workbook.SaveAsJson("Excel-Range-To-JSON-as-schema-default.json", range) 'Saves the range to a JSON file as schema workbook.SaveAsJson("Excel-Range-To-JSON-as-schema.json", range, True) 'Saves the range to a JSON filestream, as schema by default Dim stream As FileStream = New FileStream("Excel-Range-To-JSON-filestream-as-schema-default.json", FileMode.Create) workbook.SaveAsJson(stream, range) 'Saves the range to a JSON filestream as schema Dim stream1 As FileStream = New FileStream("Excel-Range-To-JSON-filestream-as-schema.json", FileMode.Create) workbook.SaveAsJson(stream1, range, True) Else 'Saves the range to a JSON file without schema workbook.SaveAsJson("Excel-Range-To-JSON-without-schema.json", range, False) 'Saves the range to a JSON filestream without schema Dim stream As FileStream = New FileStream("Excel-Range-To-JSON-filestream-without-schema.json", FileMode.Create) workbook.SaveAsJson(stream, range, False) End If End Using
A complete working example to export Excel data to JSON format in C# and VB.NET can be downloaded from Excel to JSON.zip.
Refer here to explore the rich set of Syncfusion Excel (XlsIO) library features.
See Also:
How to convert Excel to PDF using C# and VB.NET
Convert Excel to PDF in Azure platform
How to Export and Save Excel Chart as Image
Download Excel from Ajax call in ASP.NET MVC
Export DataTable with images to Excel in C#, VB.NET
Export data from excel sheet to a datatable
Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer the link to learn about generating and registering Syncfusion license key in your application to use the components without trial message.