mini-software / MiniExcel

Fast, Low-Memory, Easy Excel .NET helper to import/export/template spreadsheet (support Linux, Mac)
https://www.nuget.org/packages/MiniExcel/
Apache License 2.0
2.78k stars 338 forks source link

Support Query As DataTable #113

Closed shps951023 closed 3 years ago

shps951023 commented 3 years ago

e.g

stream.Query<DataTable>()
shps951023 commented 3 years ago

Q: How to Query as DataTable

Note : There will be no advantage whatsoever in using MiniExcel for a scenario involving DataSet or DataTable.

public static DataTable QueryAsDataTable(string path)
{
    var rows = MiniExcel.Query(path, true);
    var dt = new DataTable();
    var first = true;
    foreach (IDictionary<string, object> row in rows)
    {
        if (first)
        {
            foreach (var key in row.Keys)
            {
                var type = row[key]?.GetType() ?? typeof(string);
                dt.Columns.Add(key, type);
            }

            first = false;
        }
        dt.Rows.Add(row.Values.ToArray());
    }
    return dt;
}

image-20210415120352604