C#_.NetFramework_Web项目_EXCEL数据导入 C#_.NetCor
发布时间:2020-12-16 08:57:47 所属栏目:asp.Net 来源:网络整理
导读:【推荐阅读我的最新的Core版文章,是最全的介绍: C#_.NetCore_Web项目_EXCEL数据导出 】 ? ? 需要引用NPOI的Nuget包:NPOI-v2.4.1 ? B-1:EXCEL数据导入--C#获取数据: ? /// summary /// EXCEL帮助类 /summary typeparam name="T" 泛型类 /typeparam typep
【推荐阅读我的最新的Core版文章,是最全的介绍:C#_.NetCore_Web项目_EXCEL数据导出】 ? ? 需要引用NPOI的Nuget包:NPOI-v2.4.1 ? B-1:EXCEL数据导入--C#获取数据: ? /// <summary> /// EXCEL帮助类 </summary> <typeparam name="T">泛型类</typeparam> <typeparam name="TCollection">泛型类集合</typeparam> public class ExcelHelp<T,TCollection> where T : new() where TCollection : List<T>,new() { //http请求Request对象 static HttpRequest baseRequest = HttpContext.Current.Request; http请求Response对象 static HttpResponse baseResponse = HttpContext.Current.Response; <summary> 将数据导出EXCEL </summary> <param name="columnNameAndShowNameDic">列名+显示名</param> <param name="tColl">数据集(tColl里的类属性名必须和字典中的列名一致)</param> static void ExportExcelData(Dictionary<string,1)">string> columnNameAndShowNameDic,TCollection tColl) { IWorkbook workbook = HSSFWorkbook(); ISheet worksheet = workbook.CreateSheet("sheet1"); List<string> columnNameList = columnNameAndShowNameDic.Keys.ToList(); List<string> showNameList = columnNameAndShowNameDic.Values.ToList(); 设置首列显示 IRow row1 = worksheet.GetRow(0); ICell cell = null; for (var i = 0; i < columnNameList.Count; i++) { cell = row1.CreateCell(i); cell.SetCellValue(columnNameList[i]); } Dictionary<int,PropertyInfo> indexPropertyDic = GetIndexPropertyDic(columnNameList); int i = 0; i < tColl.Count; i++) { row1 = worksheet.GetRow(i + 1); int j = 0; j < indexPropertyDic.Count; j++) { cell = row1.CreateCell(i); cell.SetCellValue(indexPropertyDic[j].GetValue(tColl[i]).ToString()); } } MemoryStream ms = MemoryStream(); workbook.Write(ms); byte[] buffer = ms.GetBuffer(); baseResponse.Clear(); baseResponse.Buffer = true; baseResponse.ContentEncoding = System.Text.Encoding.UTF8; baseResponse.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; baseResponse.ContentType = application/vnd.ms-excel设置导出文件名 baseResponse.AddHeader(content-disposition",attachment; filename=" + MaintainReport.xlsx); baseResponse.AddHeader(Content-Length,buffer.Length.ToString()); baseResponse.BinaryWrite(buffer); baseResponse.Flush(); baseResponse.End(); } 根据属性名顺序获取对应的属性对象 <param name="fieldNameList"></param> <returns></returns> private static Dictionary< fieldNameList) { Dictionary<new Dictionary<(fieldNameList.Count); List<PropertyInfo> tPropertyInfoList = typeof(T).GetProperties().ToList(); PropertyInfo propertyInfo = 0; i < fieldNameList.Count; i++) { propertyInfo = tPropertyInfoList.Find(m => m.Name.Equals(fieldNameList[i],StringComparison.OrdinalIgnoreCase)); indexPropertyDic.Add(i,propertyInfo); } return indexPropertyDic; } } ? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – 匿名类型列表
- asp.net – 如何在Nuget中排除/不显示.NET Framework包?
- asp.net – 是否可以更改web.config而不会终止所有用户会话
- asp.net – 如何找到哪个控制器/操作发生错误?
- 如何使用WebMatrix在ASP.NET网页中创建自定义错误页面?
- asp.net – 在.NET中以编程方式添加HttpHandler的任何方法?
- .net – 如何在实体框架中使用Database first方法使用Repos
- asp.net-mvc – 使用DataAnnotations与ASP.Net MVC的多个Me
- asp.net-mvc – 会员提供商中的GetAllUsers在哪里?
- 如何决定哪个是正确的,WebForms或MVC做ASP.NET时
推荐文章
站长推荐
- asp.net-mvc – AppHarbor的反向代理导致SSL和ap
- asp.net-mvc – ASP.NET MVC SiteMap提供程序 –
- asp.net – ‘InitializeCulture’不是其成员
- asp.net-mvc – EF代码首先不生成表
- asp.net – 搜索elmah错误日志文件(也许在1000年
- asp.net – 如何判断正在查看的网站位置?
- asp.net-mvc-2 – 允许匿名访问Content和Script文
- ASP.NET FormsAuthentication cookie值的内容是什
- asp.net – 如何使用TLS电子邮件与web.config
- C#使用Log4Net记录日志
热点阅读