Export DataTable to Excel with EPPlus

Davood Hanifi picture Davood Hanifi · Dec 2, 2012 · Viewed 96.9k times · Source

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?

Answer

bastianwegge picture bastianwegge · Dec 4, 2012
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.