Convert JSON to an Excel file from your C#/VB.NET applications

The following example shows how you can convert a JSON file, or import data from a JSON file, to an Excel file by using Json.NET and GemBox.Spreadsheet .NET components.

With GemBox.Spreadsheet you can create an empty Excel file or load an Excel template, insert data from your JSON file, and either save the Excel file on a disk or stream the file to a client's web browser. While saving you can choose between different file formats such as XLSX, XLS, XLSB, CSV, ODS, PDF, and HTML.

Steps for converting JSON to an Excel file

You can do a basic conversion/import with just a few lines of code. You only need an IDE like Visual Studio or JetBrains Rider and .NET Framework, .NET Core, or any other platform that implements .NET Standard.

Follow these steps to convert from one file format to another:

Convert JSON to an Excel file online

You can test converting a JSON file to an Excel file with the interactive example below. Just choose the output file format, click Run Example, and download the generated file.

The C#/VB.NET code will be compiled, and your file will be converted with only the .NET framework, Json.NET and GemBox.Spreadsheet components.

Screenshot of Excel file created by importing JSON data
Screenshot of Excel file created by importing JSON data
using GemBox.Spreadsheet;
using Newtonsoft.Json;
using System.Collections.Generic;

class Program
{
    static void Main(string[] args)
    {
        // If you are using the Professional version, enter your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");

        // Definition of JSON file
        string jsonString = @"{
                ""0"": {
                    ""firstName"": ""John"",
                    ""lastName"": ""Smith"",
                    ""age"": 27,
                    ""email"": ""john.smith@gmail.com""
                },
                ""1"" : {
                    ""firstName"": ""Ann"",
                    ""lastName"": ""Doe"",
                    ""age"": 25,
                    ""email"": ""ann.doe@gmail.com""
                }
            }";

        // Deserialize JSON string
        Dictionary<string, User> users = JsonConvert.DeserializeObject<Dictionary<string, User>>(jsonString);

        // Create empty excel file with a sheet
        ExcelFile workbook = new ExcelFile();
        ExcelWorksheet worksheet = workbook.Worksheets.Add("Users");

        // Define header values
        worksheet.Cells[0, 0].Value = "First name";
        worksheet.Cells[0, 1].Value = "Last name";
        worksheet.Cells[0, 2].Value = "Age";
        worksheet.Cells[0, 3].Value = "Email";

        // Write deserialized values to a sheet
        int row = 0;
        foreach (User user in users.Values)
        {
            worksheet.Cells[++row, 0].Value = user.FirstName;
            worksheet.Cells[row, 1].Value = user.LastName;
            worksheet.Cells[row, 2].Value = user.Age;
            worksheet.Cells[row, 3].Value = user.Email;
        }

        // Save excel file
        workbook.Save("JsonToExcel.%OutputFileType%");
    }
}

class User
{
    [JsonProperty("firstName")]
    public string FirstName { get; set; }

    [JsonProperty("lastName")]
    public string LastName { get; set; }

    [JsonProperty("age")]
    public int Age { get; set; }

    [JsonProperty("email")]
    public string Email { get; set; }
}
Imports GemBox.Spreadsheet
Imports Newtonsoft.Json
Imports System.Collections.Generic

Module Program
    Sub Main()
        ' If you are using the Professional version, enter your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY")

        ' Definition of JSON file
        Dim jsonString As String = "{
                ""0"": {
                    ""firstName"": ""John"",
                    ""lastName"": ""Smith"",
                    ""age"": 27,
                    ""email"": ""john.smith@gmail.com""
                },
                ""1"" : {
                    ""firstName"": ""Ann"",
                    ""lastName"": ""Doe"",
                    ""age"": 25,
                    ""email"": ""ann.doe@gmail.com""
                }
            }"

        ' Deserialize JSON string
        Dim users As Dictionary(Of String, User) = JsonConvert.DeserializeObject(Of Dictionary(Of String, User))(jsonString)

        ' Create empty excel file with a sheet
        Dim workbook As ExcelFile = New ExcelFile()
        Dim worksheet As ExcelWorksheet = workbook.Worksheets.Add("Users")

        ' Define header values
        worksheet.Cells(0, 0).Value = "First name"
        worksheet.Cells(0, 1).Value = "Last name"
        worksheet.Cells(0, 2).Value = "Age"
        worksheet.Cells(0, 3).Value = "Email"

        ' Write deserialized values to a sheet
        Dim row As Integer = 0
        For Each user As User In users.Values
            worksheet.Cells(System.Threading.Interlocked.Increment(row), 0).Value = user.FirstName
            worksheet.Cells(row, 1).Value = user.LastName
            worksheet.Cells(row, 2).Value = user.Age
            worksheet.Cells(row, 3).Value = user.Email
        Next

        ' Save excel file
        workbook.Save("JsonToExcel.%OutputFileType%")
    End Sub

    Class User
        <JsonProperty("firstName")>
        Public Property FirstName As String
        <JsonProperty("lastName")>
        Public Property LastName As String
        <JsonProperty("age")>
        Public Property Age As Integer
        <JsonProperty("email")>
        Public Property Email As String
    End Class
End Module

Alternatives for parsing JSON files

The example above uses the Json.NET library for deserializing a JSON file. If you can use .NET Core 3.1+ you can also use the System.Text.Json namespace.

Supported file formats

GemBox.Spreadsheet supports converting JSON to the following file formats: XLSX, XLS, XLSB, ODS, CSV, TXT, XLSM, XLTX, XLTM, HTML, MHTML, PDF, XPS, PNG, JPEG, GIF, BMP, TIFF, WMP, and SVG.

For more details about supported file formats in GemBox.Spreadsheet, see the Supported File Formats help page.

See also


Next steps

GemBox.Spreadsheet is a .NET component that enables you to read, write, edit, convert, and print spreadsheet files from your .NET applications using one simple API.

Download Buy