© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
2 replies
malkav

❔ JSON To Excel

I have a console app that takes a folder of JSON objects, and deserializes them into Classes
From here, I want to map these classes into excel sheets
(I will link the models in a sec via hastebin)
What I want is to map these json to excel tables by making a worksheet based on the "skill" property
The thing is that these things are nested, and so far it results in my excel giving me a single sheet, with
System.Collection[...]
System.Collection[...]
values in the cells, instead of recursively getting all nested properties, and mapping them internally..

So basically here's the schema I use for the JSONs https://pastebin.com/CbgdUpt0
And these are the models I made: https://pastebin.com/N4y3ciRy

Most of my program works fine, but what I can't seem to figure is the excel part.
Here's the method I use to generate a DataTable, which is not looking into nested properties, and I don't know how
public static DataTable ConvertToDataTable<T>(this IEnumerable<T> models)
{
    DataTable dataTable = new DataTable(typeof(T).Name);
    PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
    foreach (PropertyInfo prop in props)
    {
        dataTable.Columns.Add(prop.Name);
    }
    foreach (T item in models)
    {
        object[] values = new object[props.Length];
        for (int i = 0; i < props.Length; i++)
        {
            values[i] = props[i].GetValue(item, null);
        }
        dataTable.Rows.Add(values);
    }
    return dataTable;
}
public static DataTable ConvertToDataTable<T>(this IEnumerable<T> models)
{
    DataTable dataTable = new DataTable(typeof(T).Name);
    PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
    foreach (PropertyInfo prop in props)
    {
        dataTable.Columns.Add(prop.Name);
    }
    foreach (T item in models)
    {
        object[] values = new object[props.Length];
        for (int i = 0; i < props.Length; i++)
        {
            values[i] = props[i].GetValue(item, null);
        }
        dataTable.Rows.Add(values);
    }
    return dataTable;
}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ I want to export JSON to Excel file.
C#CC# / help
3y ago
✅ Excel
C#CC# / help
3y ago
excel modification
C#CC# / help
2y ago
Excel modify
C#CC# / help
2y ago