ASP.NET使用NPOI将数据导出Excel

您所在的位置:网站首页 asp控件导出excel实例 ASP.NET使用NPOI将数据导出Excel

ASP.NET使用NPOI将数据导出Excel

2023-10-20 15:37| 来源: 网络整理| 查看: 265

目录 1、说明2、获取开源项目,生成DLL2.1 获取开源项目2.2 生成DLL2.2.1 重新添加SS文件夹-->UserModel文件夹下的所有文件2.2.2 使用Nuget添加EnumsNET 3、使用样例3.1 环境说明3.2 特殊说明3.2 通过界面的形式下载3.3 使用WebAPI将数据导出excel 4、代码下载5、总结

1、说明

之前使用IE浏览器的ActiveX将数据导出为excel,但这个依赖于IE,且有安全隐患,因此使用第三方插件进行导出。网上找了找发现了NPOI。总的来说,挺好用。但里面也有很多坑。

2、获取开源项目,生成DLL 2.1 获取开源项目

NPOI的开源地址为:项目在GitHub的地址,从上面下载源码即可。

2.2 生成DLL

最坑的事情是源码下来后,在生成DLL过程中会出现如下的错误: 生成DLL时的错误信息

2.2.1 重新添加SS文件夹–>UserModel文件夹下的所有文件

具体操作如下: 添加现有项 UserModel文件

2.2.2 使用Nuget添加EnumsNET

添加Enums.NET基本上这样,就可以生成生成功了。成功后如下图所示 生成成功后的截图

3、使用样例 3.1 环境说明

使用了win10企业版,VS2019社区版,.NET.Framework4.7.2,ASP.NET MVC 5

3.2 特殊说明

从GitHub拉下来的最新版NPOI中,其使用了Microsoft.IO.RecyclableMemoryStream,其依赖项最低是.NET.Framework4.6.1。因此在使用NPOI时,要特别注意选择合适的版本。

3.2 通过界面的形式下载

即通过点击按钮,进行数据下载。 前端界面如下: 前端界面 下载效果如下: 在这里插入图片描述数据效果

前端代码如下:

@{ ViewBag.Title = "Index"; } Index 下载Excel

Controller的代码如下:

//使用界面进行下载 public ActionResult ExportExcel() { ExportClass export = new ExportClass(); HSSFWorkbook detaildata = export.getExportData("数据导出"); MemoryStream ms = new MemoryStream(); detaildata.Write(ms); ms.Seek(0, SeekOrigin.Begin); return File(ms, "application/vnd.ms-excel", "Excel的文件名.xls"); }

其中ExportClass类源码如下:

public class ExportClass { //返回展示的名称。用于excel的标题 public List getDisplayName(T entity) { List resultList = new List(); PropertyInfo[] pinfoArray = entity.GetType().GetProperties(); foreach (var item in pinfoArray) { var obj = item.GetCustomAttributes(typeof(DisplayNameAttribute), true); if(obj!=null) { var displayName = ((DisplayNameAttribute)obj[0]).DisplayName; resultList.Add(displayName); } } return resultList; } //返回真实数据 public List getTrueValue(T entity) { List resultList = new List(); Type type = entity.GetType(); PropertyInfo[] pinfoArray = type.GetProperties(); foreach (var propertyItem in pinfoArray) { string name = propertyItem.Name; PropertyInfo pinfo = type.GetProperty(name); var theTrueValue = pinfo.GetValue(entity).ToString(); resultList.Add(theTrueValue); } return resultList; } //返回一个Excel public HSSFWorkbook getExportData(string sheetname) { HSSFWorkbook book = new HSSFWorkbook(); ISheet sheet = book.CreateSheet(sheetname); //获取数据 List dataList = new List() { new ExcelDataModel { TheColoumID="第(1,1)数据",TheColoumName="第(1,2)数据"}, new ExcelDataModel { TheColoumID="第(2,1)数据",TheColoumName="第(2,2)数据"}, new ExcelDataModel { TheColoumID="第(3,1)数据",TheColoumName="第(3,2)数据"} }; //获取标题的数据 List titleList = getDisplayName(dataList.FirstOrDefault()); ICellStyle cellStyle = book.CreateCellStyle(); cellStyle.BorderBottom = BorderStyle.Thin; cellStyle.BorderLeft = BorderStyle.Thin; cellStyle.BorderTop = BorderStyle.Thin; cellStyle.BorderRight = BorderStyle.Thin; cellStyle.VerticalAlignment = VerticalAlignment.Center; //创建表头 IRow row = sheet.CreateRow(0); for (int i = 0; i IRow temprow = sheet.CreateRow(i + 1); var tempResult = getTrueValue(dataList[i]); for (int j = 0; j [DisplayName("第一列的名称")] public string TheColoumID { get; set; } [DisplayName("第二列")] public string TheColoumName { get; set; } } 3.3 使用WebAPI将数据导出excel

API的代码如下:

[HttpGet] public HttpResponseMessage ExportExcel() { //获取数据 ExportClass export = new ExportClass(); HSSFWorkbook detaildata = export.getExportData("数据导出"); //整理返回结果 using(MemoryStream ms = new MemoryStream()) { detaildata.Write(ms); var response = new HttpResponseMessage(HttpStatusCode.OK) { Content = new ByteArrayContent(ms.ToArray()) }; response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = "数据excel.xlsx" }; return response; } } 4、代码下载

源码下载:具体下载地址 提取码:NHZL

5、总结

总的来说,NPOI是一个不错的插件,不过建议开源项目作者把精力更多的放在永攀高峰的技术上,取的更大的成果。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3