We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Syncfusion Feedback


Trusted by the world’s leading companies

Syncfusion Trusted Companies

Overview

The Syncfusion .NET Excel (XlsIO) library provides support for effortlessly converting Excel to JSON format using the convenient SaveAsJson method with and without JSON schema. This support encompasses a range of essential features, such as:

  • Convert workbook to JSON
  • Convert worksheet to JSON
  • Convert a range to JSON

Learn about Excel to JSON conversion

Examples of Excel to JSON conversion

.NET Excel to JSON


Workbook to JSON

The entire Excel document can be converted to JSON using the IWorkbook.SaveAsJson method with and without schema.


How to convert an Excel workbook to JSON using C#

  • Install the Syncfusion.XlsIO.Net.Core NuGet package in your .NET application.
  • Initialize the ExcelEngine and IApplication objects.
  • Load the Excel document into IWorkbook.
  • Save the workbook to a JSON file stream with schema by default.
  • Save the workbook to a JSON file stream without schema by making the schema parameter as false.

The following code illustrates how to convert an Excel workbook to a JSON file with and without schema using C#.

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Xlsx;
  using (FileStream fileStream = new FileStream("Sample.xlsx", FileMode.Open, FileAccess.Read))
  {
      IWorkbook workbook = application.Workbooks.Open(fileStream);
      IWorksheet worksheet = workbook.Worksheets[0];
 
      //Save the workbook to a JSON file stream with schema by default
      using (FileStream stream = new FileStream("Excel-Workbook-To-JSON-as-schema-default.json", FileMode.Create, FileAccess.ReadWrite))
      {
          workbook.SaveAsJson(stream);
 
          //Save the workbook to a JSON file stream without schema
         using (FileStream stream1 = new FileStream("Excel-Workbook-To-JSON-as-schema.json", FileMode.Create, FileAccess.ReadWrite))
         {
              workbook.SaveAsJson(stream1, true);
         }
      }
  }
}

Worksheet to JSON

A specific worksheet in the Excel document can be converted to JSON using the IWorkbook.SaveAsJson method with and without schema by passing the worksheet instance to the method.


How to convert an Excel worksheet to JSON using C#

The following code illustrates converting an Excel worksheet to a JSON file with and without schema using C#.

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Xlsx;
  using (FileStream fileStream = new FileStream("Sample.xlsx", FileMode.Open, FileAccess.Read);
  IWorkbook workbook = application.Workbooks.Open(fileStream))
  {
      IWorksheet worksheet = workbook.Worksheets[0];
 
      //Save the worksheet to a JSON file stream with schema by default
     using (FileStream stream = new FileStream("Excel-Worksheet-To-JSON-file stream-as-schema-default.json", FileMode.Create, FileAccess.ReadWrite))
      {
           workbook.SaveAsJson(stream, worksheet);
 
           //Save the worksheet to a JSON file stream without schema
           using (FileStream stream1 = new FileStream("Excel-Worksheet-To-JSON-file stream-as-schema.json", FileMode.Create, FileAccess.ReadWrite))
          {
               workbook.SaveAsJson(stream1, worksheet, false);
          }
     }
  }
}

Range to JSON

A specific cell range in the Excel document can be converted to JSON using the IWorkbook.SaveAsJson method with and without schema by passing the IRange instance to the method.


How to convert an Excel range to JSON using C#

The following code illustrates converting an Excel range to JSON with and without schema using C#.

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel; 
  application.DefaultVersion = ExcelVersion.Xlsx;
  using (FileStream fileStream = new FileStream("Sample.xlsx", FileMode.Open, FileAccess.Read))
  {
      IWorkbook workbook = application.Workbooks.Open(fileStream);
      IWorksheet worksheet = workbook.Worksheets[0];
 
      //Custom range
      IRange range = worksheet.Range["A2:A5"];
 
     //Save the Range to a JSON file stream as schema by default
     using (FileStream stream = new FileStream("Excel-Range-To-JSON-as-schema-default.json", FileMode.Create, FileAccess.ReadWrite))
     {
         workbook.SaveAsJson(stream, range);
 
         //Save the Range to a JSON file stream without schema
         using (FileStream stream = new FileStream("Excel-Range-To-JSON-as-schema.json", FileMode.Create, FileAccess.ReadWrite))
         {
             workbook.SaveAsJson(stream, range, false);
         }
     }
  }
}


Awards

Greatness—it’s one thing to say you have it, but it means more when others recognize it. Syncfusion is proud to hold the following industry awards.

Scroll up icon