C#_.NetFramework_Web项目_EXCEL数据导入
程序员文章站
2022-07-05 11:11:55
【推荐阅读我的最新的Core版文章,是最全的介绍:C#_.NetCore_Web项目_EXCEL数据导出】 需要引用NPOI的Nuget包:NPOI-v2.4.1 B-1:EXCEL数据导入--C#获取数据: ......
【推荐阅读我的最新的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对象 public static httprequest baserequest = httpcontext.current.request; //http请求response对象 public static httpresponse baseresponse = httpcontext.current.response; /// <summary> /// 将数据导出excel /// </summary> /// <param name="columnnameandshownamedic">列名+显示名</param> /// <param name="tcoll">数据集(tcoll里的类属性名必须和字典中的列名一致)</param> public static void exportexceldata(dictionary<string, string> columnnameandshownamedic, tcollection tcoll) { iworkbook workbook = new 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); for (int i = 0; i < tcoll.count; i++) { row1 = worksheet.getrow(i + 1); for (int j = 0; j < indexpropertydic.count; j++) { cell = row1.createcell(i); cell.setcellvalue(indexpropertydic[j].getvalue(tcoll[i]).tostring()); } } memorystream ms = new 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(); } /// <summary> /// 根据属性名顺序获取对应的属性对象 /// </summary> /// <param name="fieldnamelist"></param> /// <returns></returns> private static dictionary<int, propertyinfo> getindexpropertydic(list<string> fieldnamelist) { dictionary<int, propertyinfo> indexpropertydic = new dictionary<int, propertyinfo>(fieldnamelist.count); list<propertyinfo> tpropertyinfolist = typeof(t).getproperties().tolist(); propertyinfo propertyinfo = null; for (int i = 0; i < fieldnamelist.count; i++) { propertyinfo = tpropertyinfolist.find(m => m.name.equals(fieldnamelist[i], stringcomparison.ordinalignorecase)); indexpropertydic.add(i, propertyinfo); } return indexpropertydic; } }
上一篇: 详解PHP素材图片上传、下载功能
推荐阅读
-
C#_.NetCore_WebAPI项目_EXCEL数据导出(ExcelHelper_第二版_优化逻辑)
-
jsp和excel的数据交换-----我的项目实践
-
ASP将Excel数据导入到SQLServer的实现代码
-
webapi 导入excel处理数据
-
利用Excel里面的分隔符去导入文本文件以此来完成数据转换
-
把Excel上的数据表格导入到word中以方便计算
-
Excel表格数据导入数据库users表中,利用excel公式自动生成sql语句方法
-
Excel导入数据库时出现的文本截断问题解决方案
-
Java实现Excel导入数据库,数据库中的数据导入到Excel
-
cad点坐标数据怎么导入excel? cad坐标输出excel的方法