Articles in this section
Category / Section

How to Export WinForms Excel Data to JSON format in C#, VB.NET?

8 mins read

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  WinForms XIsIO feature tour.

XlsIO provides a method SaveAsJson to convert Excel data to JSON files. With this, you can save an Excel file as a simple JSON file or a JSON file as a schema. The feature includes saving:

  • 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 new C# Windows Forms application

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.

Add XlsIO reference to the project

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 custom range to a JSON file or JSON file stream as a schema and without a 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 Excel data to CSV file

Export DataTable with images to Excel in C#, VB.NET

Export data from excel sheet to a datatable

Take a moment to peruse the documentation where you can find basic Excel document processing options along with the features like import and export data, chart, formulas, conditional formatting, data validation, tables, pivot tables and, protect the Excel documents, and most importantly, the PDF, CSV and Image conversions with code examples.

Note:

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.

 

Conclusion

I hope you enjoyed learning about how to export WinForms Excel data to JSON format in C#, VB.NET.

You can refer to our XIsIO’s feature tour page to learn about its other groundbreaking features. Explore our UG documentation and online demos to understand how to manipulate data in Excel documents.

If you are an existing user, you can access our latest components from the License and Downloads page. For new users, you can try our 30-day free trial to check out XlsIO and other Syncfusion components.

If you have any queries or require clarification, please let us know in the comments below or contact us through our support forumsSupport Tickets, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied