Search code examples
c#excelopenxml

ClosedXML. Adding DataTable to existing Excel worksheet


How using ClosedXML library in fastest way (from performance point of view) add values from DataTable to already existing Excel worksheet?

NOTE: There is way to create new worksheet with DataTable parameter, but the main issue is in adding values to existing worksheet.


Solution

  • If you're dealing with millions of cells and you want to insert the data as fast as possible while consuming the minimum amount of memory then SAX is the way to go.

    If you want ClosedXML to do the work for you then use:

    cell.Value = dataTable;
    or
    cell.SetValue(dataTable);
    or
    cell.InsertData(dataTable);
    or
    cell.InsertTable(dataTable);
    

    See the "Inserting Data/Tables" section of the Documentation