MiniExcel icon indicating copy to clipboard operation
MiniExcel copied to clipboard

DynamicColumn with DataTable not work

Open ryanlin1986 opened this issue 3 years ago • 5 comments

Is this by design or bug?

ryanlin1986 avatar Apr 22 '22 08:04 ryanlin1986

It's not support now

shps951023 avatar Apr 22 '22 12:04 shps951023

I also think it's not implemented in DataReader? I only want to set the Format with generic columns and I think it is the only way to do it with DynamicColumns

maui87 avatar May 04 '22 07:05 maui87

@maui87 yes, they have to implement with unique different logic feature

shps951023 avatar May 16 '22 03:05 shps951023

As for now, we can workaround this by using Dictionary with DynamicColumn.

ryanlin1986 avatar May 30 '22 22:05 ryanlin1986

I solved this by preprocessing my DataTable to the generic list of dictionaries seen in your examples. I recommend deleting the code path GenerateSheetByDataTable and instead converting the different inputs, like DataTable to an object you can feed to a shared GenerateSheet method. This way, it doesn't matter what the input is, the way it is processed should be the same. I have begun looking at your code but I'm not familiar enough with it to issue a PR yet. I hope you understand my suggestion. It's a simple solution.

private static IEnumerable<IDictionary<string, object>> DataTableToListOfDictionary(DataTable dt)
{
    foreach (DataRow row in dt.Rows)
    {
        var rowData = new Dictionary<string, object>();
        foreach (DataColumn col in dt.Columns)
        {
            var header = col.Caption ?? col.ColumnName );
            rowData.Add(header, row(col));
        }

        yield return rowData;
    }
}

A9G-Data-Droid avatar Aug 24 '22 16:08 A9G-Data-Droid