使用 EPPlus 将数据表导出到 Excel

问题描述:

我想用 EPPlus 将数据表导出到 Excel 文件.该数据表具有 int 类型的属性,因此我希望 Excel 文件中具有相同的格式.

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.

有人知道如何将这样的数据表导出到 Excel 吗?

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

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

这应该对你有用.如果您的字段定义为 int EPPlus 将正确地将列转换为数字或浮点数.

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.