JanKallman / EPPlus

Create advanced Excel spreadsheets using .NET
3.75k stars 1.17k forks source link

Exporting DataTable with columns whose type is a collection #595

Open mdmilic opened 4 years ago

mdmilic commented 4 years ago

I'm currently trying to export some DataTable data to excel. One of the columns in my table is of type List<string>. I first create a DataTableReader and then try to load the data into a worksheet using LoadFromDataReader like so:

public static ToReader(List<object> data)
{
    var toReturn = new DataTable();
    // Complex code about creating the data table
    return new DataTableReader(toReturn);
}
//...
public static Export(List<object> data)
{
    using (var pck = new ExcelPackage())
    {
        // add the content into the Excel file
        ExcelWorksheet ws = pck.Workbook.Worksheets["Data"];
        ws.Cells[startCell].LoadFromDataReader(ToReader(data), true);
        //....
    }
}

All the data gets exported to excel just fine except for the column which contains List<string>. For this column I would expect the cell value to be comma separated values of that list, instead I just get the first element of the list.

E.g. if the list was {"Fee", "Fi", "Fo", "Fum"}, I would expect the cell value to be "Fee, Fi, Fo, Fum", instead I get "Fee".

From debugging I see that data is correctly written to the DataTableReader, so the problem must be with loading it into excel.

From SO answers it seems that the culprit is this part of EPPlus code.

Is this an issue with EPPlus or for some reason this is desired behavior? I couldn't find the issue mentioned in the EPPlus code.