Convert JSON to Excel C#

MS Excel provides a variety of features to keep and organize tabular data in the form of worksheets. Along with data organization, you can perform various operations such as data sorting, graph plotting, mathematical computations and etc. JSON, on the other hand, is a widely used format to store and transmit the data in the form of key-value pairs. Most often, it is used to transmit data from the server to the web pages. There could be the case when you receive the data in the form of JSON, i.e. from a web service, and want to save it into an Excel worksheet. In such a case, the optimal way is automating the process by importing data from JSON to Excel worksheets programmatically.

In order to deal with the above-mentioned scenario in ASP.NET or any C# based application, I’ll show you how to import JSON data into Excel file. In this article, you will learn how to:

  • import data from JSON to Excel worksheet in C#
  • apply cell formatting when converting JSON to Excel in C#

In order to import data from JSON files, we’ll leverage the capabilities of Aspose.Cells for .NET. It is a powerful API to manipulate spreadsheet files in .NET, .NET Core & Xamarin based applications. You can install the API’s package from NuGet or download the DLL directly from the Downloads section.

Import Data from JSON to Excel in C#

The following are the steps to import data from JSON to Excel XLSX:

The following code sample shows how to import data from JSON to Excel in C#.

Input JSON Data

[
   {
       Name: "John Smith",
       Contract:
       [
           {
               Client:
               {
                   Name: "A Company"
               },
               Price: 1200000
           },
           {
               Client:
               {
                   Name: "B Ltd."
               },
               Price: 750000
           },
           {
               Client:
               {
                   Name: "C & D"
               },
               Price: 350000
           }
       ]
   },
   {
       Name: "Tony Anderson",
       Contract:
       [
           {
               Client:
               {
                   Name: "E Corp."
               },
               Price: 650000
           },
           {
               Client:
               {
                   Name: "F & Partners"
               },
               Price: 550000
           }
       ]
   },
   {
       Name: "Jimmy Adams",
       Contract:
       [
           {
               Client:
               {
                   Name: "MIT"
               },
               Price: 800000
           },
           {
               Client:
               {
                   Name: "SnB"
               },
               Price: 520000
           }
       ]
   },
]

Output Excel Worksheet

JSON to Excel

Import JSON to Excel with Formatting Styles in C#

You can also apply different styles to the Excel worksheet when importing the data from JSON file. The CellsFactory class of Aspose.Cells for .NET provides a range of options to set various styling parameters such as font, color, alignment, border styles, etc. The following are the steps to import data from JSON to Excel along with the styling.

The following code sample applies different styles when importing data from JSON to Excel in C#.

Output Excel Worksheet

JSON to Excel C#

See Also

Learn more about Aspose.Cells for .NET

You can explore more about Aspose.Cells for .NET using the documentation and the source code examples.