C#C
C#3y ago
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[...] 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;
}
Was this page helpful?