How to Convert JSON file to Excel using C#

This short tutorial explains how to convert JSON file to Excel using C# by reading JSON data from any source and then saving it as an Excel file say XLSX after defining the formatting of imported data. An empty or existing workbook will be loaded and JSON data will be imported into a user-defined starting cell in the selected worksheet. During this conversion of JSON to Excel in C#, you will have complete control over the font settings, grid display, text alignment, page setup, and a variety of other parameters.

Steps to Convert JSON File to Excel using C#

  1. Add the reference to the Aspose.Cells library from NuGet Package Manager to convert JSON file to Excel
  2. Create a style for the JSON data titles by setting different properties like alignment, font color, and Bold flag
  3. Initialize JsonLayoutOptions class object using the above style and other flags
  4. Create an empty Workbook and get a reference to the target worksheet
  5. Read the entire JSON content into a string variable
  6. Call ImportData function of JsonUtility class to convert JSON string to Excel
  7. Save the output Excel file containing imported JSON data

Here we first initialize the optional formatting parameters required for the titles in the output Excel file. In the next step, the workbook is initialized and the reference to the target worksheet is obtained. Finally, the source JSON data is loaded into a string variable and used in the ImportData function as a parameter along with other required information. To export JSON to Excel C# sample code is demonstrated below.

Code to Convert JSON to Excel in C#

This code uses CellsFactory to create a style for the output JSON data titles by defining the horizontal alignment and font settings that are later used in the JsonLayoutOptions object. The JsonUtility.ImportData() function requires the source JSON string, reference to the Cells collection of the target worksheet, the first row and column where data is to be imported, and the layout options set at the beginning of the program.

This short tutorial has guided us how using C# convert JSON to Excel. However if you want to learn how to convert JSON to CSV, refer to the article on how to convert JSON to CSV in C#.

 English