使用 EPPlus 将数据表导出到 Excel [英] Export DataTable to Excel with EPPlus

查看:43
本文介绍了使用 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.

这篇关于使用 EPPlus 将数据表导出到 Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆