Search code examples
c#exceldatatableexportepplus

Export DataTable to Excel with EPPlus


I want to export a data table to an Excel file with EPPlus. That data table has a property with int type, so I want the same format in the Excel file.

Does anyone know way to export a DataTable like this to Excel?


Solution

  • using (ExcelPackage pck = new ExcelPackage(newFile))
    {
      ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Accounts");
      ws.Cells["A1"].LoadFromDataTable(dataTable, true);
      pck.Save();
    }
    

    That should do the trick for you. If your fields are defined as int EPPlus will properly cast the columns into a number or float.