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:
The entire Excel document can be converted to JSON using the IWorkbook.SaveAsJson method with and without schema.
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);
}
}
}
}
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.
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);
}
}
}
}
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.
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);
}
}
}
}
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.