C# Excel导入、导出
本篇主要介绍C#的Excel导入、导出。
目录
1. 介绍:描述第三方类库NPOI以及Excel结构
2. Excel导入:介绍C#如何调用NPOI进行Excel导入,包含:流程图、NOPI以及C#代码
3. Excel导出:介绍C#如何调用NPOI进行Excel导出,包含:流程图、NOPI以、C#代码以及代码分析
4. 源码下载:展示运行图及源码下载
1. 介绍
1.1 第三方类库:NPOI
说明:NPOI是POI项目的.NET 版本,可用于Excel、Word的读写操作。
优点:不用装Office环境。
下载地址:http://npoi.codeplex.com/releases
1.2 Excel结构介绍
工作簿(Workbook):每个Excel文件可理解为一个工作簿。
工作表(Sheet):一个工作簿(Workbook)可以包含多个工作表。
行(row):一个工作表(Sheet)可以包含多个行。

2. Excel导入
2.1 操作流程

2.2 NPOI操作代码
说明:把Excel文件转换为List<T>
步骤:
①读取Excel文件并以此初始化一个工作簿(Workbook);
②从工作簿上获取一个工作表(Sheet);默认为工作薄的第一个工作表;
③遍历工作表所有的行(row);默认从第二行开始遍历,第一行(序号0)为单元格头部;
④遍历行的每一个单元格(cell),根据一定的规律赋值给对象的属性。
代码:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
/// <summary>/// 从Excel2003取数据并记录到List集合里/// </summary>/// <param name="cellHeard">单元头的Key和Value:{ { "UserName", "姓名" }, { "Age", "年龄" } };</param>/// <param name="filePath">保存文件绝对路径</param>/// <param name="errorMsg">错误信息</param>/// <returns>转换好的List对象集合</returns>private static List<T> Excel2003ToEntityList<T>(Dictionary<string, string> cellHeard, string filePath, out StringBuilder errorMsg) where T : new(){ errorMsg = new StringBuilder(); // 错误信息,Excel转换到实体对象时,会有格式的错误信息 List<T> enlist = new List<T>(); // 转换后的集合 List<string> keys = cellHeard.Keys.ToList(); // 要赋值的实体对象属性名称 try { using (FileStream fs = File.OpenRead(filePath)) { HSSFWorkbook workbook = new HSSFWorkbook(fs); HSSFSheet sheet = (HSSFSheet)workbook.GetSheetAt(0); // 获取此文件第一个Sheet页 for (int i = 1; i <= sheet.LastRowNum; i++) // 从1开始,第0行为单元头 { // 1.判断当前行是否空行,若空行就不在进行读取下一行操作,结束Excel读取操作 if (sheet.GetRow(i) == null) { break; } T en = new T(); string errStr = ""; // 当前行转换时,是否有错误信息,格式为:第1行数据转换异常:XXX列; for (int j = 0; j < keys.Count; j++) { // 2.若属性头的名称包含'.',就表示是子类里的属性,那么就要遍历子类,eg:UserEn.TrueName if (keys[j].IndexOf(".") >= 0) { // 2.1解析子类属性 string[] properotyArray = keys[j].Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries); string subClassName = properotyArray[0]; // '.'前面的为子类的名称 string subClassProperotyName = properotyArray[1]; // '.'后面的为子类的属性名称 System.Reflection.PropertyInfo subClassInfo = en.GetType().GetProperty(subClassName); // 获取子类的类型 if (subClassInfo != null) { // 2.1.1 获取子类的实例 var subClassEn = en.GetType().GetProperty(subClassName).GetValue(en, null); // 2.1.2 根据属性名称获取子类里的属性信息 System.Reflection.PropertyInfo properotyInfo = subClassInfo.PropertyType.GetProperty(subClassProperotyName); if (properotyInfo != null) { try { // Excel单元格的值转换为对象属性的值,若类型不对,记录出错信息 properotyInfo.SetValue(subClassEn, GetExcelCellToProperty(properotyInfo.PropertyType, sheet.GetRow(i).GetCell(j)), null); } catch (Exception e) { if (errStr.Length == 0) { errStr = "第" + i + "行数据转换异常:"; } errStr += cellHeard[keys[j]] + "列;"; } } } } else { // 3.给指定的属性赋值 System.Reflection.PropertyInfo properotyInfo = en.GetType().GetProperty(keys[j]); if (properotyInfo != null) { try { // Excel单元格的值转换为对象属性的值,若类型不对,记录出错信息 properotyInfo.SetValue(en, GetExcelCellToProperty(properotyInfo.PropertyType, sheet.GetRow(i).GetCell(j)), null); } catch (Exception e) { if (errStr.Length == 0) { errStr = "第" + i + "行数据转换异常:"; } errStr += cellHeard[keys[j]] + "列;"; } } } } // 若有错误信息,就添加到错误信息里 if (errStr.Length > 0) { errorMsg.AppendLine(errStr); } enlist.Add(en); } } return enlist; } catch (Exception ex) { throw ex; }} |
2.3 C#逻辑操作代码
说明:对Excel转换后的List<T>进行后续操作;如:检测有效性、持久化存储等等
步骤:
①调用2.2代码,把Excel文件转换为List<T>。
②对List<T>进行有效性检测:必填项是否为空、是否有重复记录等等。
③对List<T>进行持久化存储操作。如:存储到数据库。
④返回操作结果。
代码:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
public void ImportExcel(HttpContext context){ StringBuilder errorMsg = new StringBuilder(); // 错误信息 try { #region 1.获取Excel文件并转换为一个List集合 // 1.1存放Excel文件到本地服务器 HttpPostedFile filePost = context.Request.Files["filed"]; // 获取上传的文件 string filePath = ExcelHelper.SaveExcelFile(filePost); // 保存文件并获取文件路径 // 单元格抬头 // key:实体对象属性名称,可通过反射获取值 // value:属性对应的中文注解 Dictionary<string, string> cellheader = new Dictionary<string, string> { { "Name", "姓名" }, { "Age", "年龄" }, { "GenderName", "性别" }, { "TranscriptsEn.ChineseScores", "语文成绩" }, { "TranscriptsEn.MathScores", "数学成绩" }, }; // 1.2解析文件,存放到一个List集合里 List<UserEntity> enlist = ExcelHelper.ExcelToEntityList<UserEntity>(cellheader, filePath, out errorMsg); #endregion #region 2.对List集合进行有效性校验 #region 2.1检测必填项是否必填 for (int i = 0; i < enlist.Count; i++) { UserEntity en = enlist[i]; string errorMsgStr = "第" + (i + 1) + "行数据检测异常:"; bool isHaveNoInputValue = false; // 是否含有未输入项 if (string.IsNullOrEmpty(en.Name)) { errorMsgStr += "姓名列不能为空;"; isHaveNoInputValue = true; } if (isHaveNoInputValue) // 若必填项有值未填 { en.IsExcelVaildateOK = false; errorMsg.AppendLine(errorMsgStr); } } #endregion #region 2.2检测Excel中是否有重复对象 for (int i = 0; i < enlist.Count; i++) { UserEntity enA = enlist[i]; if (enA.IsExcelVaildateOK == false) // 上面验证不通过,不进行此步验证 { continue; } for (int j = i + 1; j < enlist.Count; j++) { UserEntity enB = enlist[j]; // 判断必填列是否全部重复 if (enA.Name == enB.Name) { enA.IsExcelVaildateOK = false; enB.IsExcelVaildateOK = false; errorMsg.AppendLine("第" + (i + 1) + "行与第" + (j + 1) + "行的必填列重复了"); } } } #endregion // TODO:其他检测 #endregion // 3.TODO:对List集合持久化存储操作。如:存储到数据库 // 4.返回操作结果 bool isSuccess = false; if (errorMsg.Length == 0) { isSuccess = true; // 若错误信息成都为空,表示无错误信息 } var rs = new { success = isSuccess, msg = errorMsg.ToString(), data = enlist }; System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer(); context.Response.ContentType = "text/plain"; context.Response.Write(js.Serialize(rs)); // 返回Json格式的内容 } catch (Exception ex) { throw ex; }} |
3. Excel导出
3.1 导出流程

3.2 NPOI操作代码
说明:把List<T>转换为Excel
步骤:
①创建一个工作簿(Workbook);
②在工作簿上创建一个工作表(Sheet);
③在工作表上创建第一行(row),第一行为列头,依次写入cellHeard的值(做为列名)。
④循环遍历List<T>集合,每循环一遍创建一个行(row),然后根据cellHeard的键(属性名称)依次从List<T>中的实体对象取值存放到单元格内。
代码:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
/// <summary>/// 实体类集合导出到Excle2003/// </summary>/// <param name="cellHeard">单元头的Key和Value:{ { "UserName", "姓名" }, { "Age", "年龄" } };</param>/// <param name="enList">数据源</param>/// <param name="sheetName">工作表名称</param>/// <returns>文件的下载地址</returns>public static string EntityListToExcel2003(Dictionary<string, string> cellHeard, IList enList, string sheetName){ try { string fileName = sheetName + "-" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".xls"; // 文件名称 string urlPath = "UpFiles/ExcelFiles/" + fileName; // 文件下载的URL地址,供给前台下载 string filePath = HttpContext.Current.Server.MapPath("\\" + urlPath); // 文件路径 // 1.检测是否存在文件夹,若不存在就建立个文件夹 string directoryName = Path.GetDirectoryName(filePath); if (!Directory.Exists(directoryName)) { Directory.CreateDirectory(directoryName); } // 2.解析单元格头部,设置单元头的中文名称 HSSFWorkbook workbook = new HSSFWorkbook(); // 工作簿 ISheet sheet = workbook.CreateSheet(sheetName); // 工作表 IRow row = sheet.CreateRow(0); List<string> keys = cellHeard.Keys.ToList(); for (int i = 0; i < keys.Count; i++) { row.CreateCell(i).SetCellValue(cellHeard[keys[i]]); // 列名为Key的值 } // 3.List对象的值赋值到Excel的单元格里 int rowIndex = 1; // 从第二行开始赋值(第一行已设置为单元头) foreach (var en in enList) { IRow rowTmp = sheet.CreateRow(rowIndex); for (int i = 0; i < keys.Count; i++) // 根据指定的属性名称,获取对象指定属性的值 { string cellValue = ""; // 单元格的值 object properotyValue = null; // 属性的值 System.Reflection.PropertyInfo properotyInfo = null; // 属性的信息 // 3.1 若属性头的名称包含'.',就表示是子类里的属性,那么就要遍历子类,eg:UserEn.UserName if (keys[i].IndexOf(".") >= 0) { // 3.1.1 解析子类属性(这里只解析1层子类,多层子类未处理) string[] properotyArray = keys[i].Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries); string subClassName = properotyArray[0]; // '.'前面的为子类的名称 string subClassProperotyName = properotyArray[1]; // '.'后面的为子类的属性名称 System.Reflection.PropertyInfo subClassInfo = en.GetType().GetProperty(subClassName); // 获取子类的类型 if (subClassInfo != null) { // 3.1.2 获取子类的实例 var subClassEn = en.GetType().GetProperty(subClassName).GetValue(en, null); // 3.1.3 根据属性名称获取子类里的属性类型 properotyInfo = subClassInfo.PropertyType.GetProperty(subClassProperotyName); if (properotyInfo != null) { properotyValue = properotyInfo.GetValue(subClassEn, null); // 获取子类属性的值 } } } else { // 3.2 若不是子类的属性,直接根据属性名称获取对象对应的属性 properotyInfo = en.GetType().GetProperty(keys[i]); if (properotyInfo != null) { properotyValue = properotyInfo.GetValue(en, null); } } // 3.3 属性值经过转换赋值给单元格值 if (properotyValue != null) { cellValue = properotyValue.ToString(); // 3.3.1 对时间初始值赋值为空 if (cellValue.Trim() == "0001/1/1 0:00:00" || cellValue.Trim() == "0001/1/1 23:59:59") { cellValue = ""; } } // 3.4 填充到Excel的单元格里 rowTmp.CreateCell(i).SetCellValue(cellValue); } rowIndex++; } // 4.生成文件 FileStream file = new FileStream(filePath, FileMode.Create); workbook.Write(file); file.Close(); // 5.返回下载路径 return urlPath; } catch (Exception ex) { throw ex; }} |
3.3 C#逻辑操作代码
说明:对Excel转换后的List<T>进行后续操作;如:检测有效性、持久化存储等等
步骤:
①获取List<T>集合。
②调用3.2,将List<T>转换为Excel文件。
③服务器存储Excel文件并返回下载链接。
代码:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
public void ExportExcel(HttpContext context){ try { // 1.获取数据集合 List<UserEntity> enlist = new List<UserEntity>() { new UserEntity{Name="刘一",Age=22,Gender="Male",TranscriptsEn=new TranscriptsEntity{ChineseScores=80,MathScores=90}}, new UserEntity{Name="陈二",Age=23,Gender="Male",TranscriptsEn=new TranscriptsEntity{ChineseScores=81,MathScores=91} }, new UserEntity{Name="张三",Age=24,Gender="Male",TranscriptsEn=new TranscriptsEntity{ChineseScores=82,MathScores=92} }, new UserEntity{Name="李四",Age=25,Gender="Male",TranscriptsEn=new TranscriptsEntity{ChineseScores=83,MathScores=93} }, new UserEntity{Name="王五",Age=26,Gender="Male",TranscriptsEn=new TranscriptsEntity{ChineseScores=84,MathScores=94} }, }; // 2.设置单元格抬头 // key:实体对象属性名称,可通过反射获取值 // value:Excel列的名称 Dictionary<string, string> cellheader = new Dictionary<string, string> { { "Name", "姓名" }, { "Age", "年龄" }, { "GenderName", "性别" }, { "TranscriptsEn.ChineseScores", "语文成绩" }, { "TranscriptsEn.MathScores", "数学成绩" }, }; // 3.进行Excel转换操作,并返回转换的文件下载链接 string urlPath = ExcelHelper.EntityListToExcel2003(cellheader, enlist, "学生成绩"); System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer(); context.Response.ContentType = "text/plain"; context.Response.Write(js.Serialize(urlPath)); // 返回Json格式的内容 } catch (Exception ex) { throw ex; }} |
3.4 代码分析
核心代码主要是cellheader与List<T>之间的映射关系:

4. 源码下载
4.1 运行图

4.2 下载地址
C# Excel导入、导出的更多相关文章
- 利用反射实现通用的excel导入导出
如果一个项目中存在多种信息的导入导出,为了简化代码,就需要用反射实现通用的excel导入导出 实例代码如下: 1.创建一个 Book类,并编写set和get方法 package com.bean; p ...
- Excel导入导出的业务进化场景及组件化的设计方案(上)
1:前言 看过我文章的网友们都知道,通常前言都是我用来打酱油扯点闲情的. 自从写了上面一篇文章之后,领导就找我谈话了,怕我有什么想不开. 所以上一篇的(下)篇,目前先不出来了,哪天我异地二次回忆的时候 ...
- 关于Excel导入导出的用例设计
目前,为方便操作,很多系统都会增加批量导入导出的功能.文件导入导出一般格式都是excel.由于用户直接在excel在填写内容,无法控制填写的格 式,加上excel解析比较困难,所以一般涉及到excel ...
- ASP.NET 之 常用类、方法的超级总结,并包含动态的EXCEL导入导出功能,奉上类库源码
实用类:UtilityClass 包含如下方法 判断对象是否为空或NULL,如果是空或NULL返回true,否则返回false 验证手机号是否正确 13,15,18 验证邮箱 验证网址 MD5加密,返 ...
- Excel导入导出帮助类
/// <summary> /// Excel导入导出帮助类 /// 记得引入 NPOI /// 下载地址 http://npoi.codeplex.com/rele ...
- 一个基于POI的通用excel导入导出工具类的简单实现及使用方法
前言: 最近PM来了一个需求,简单来说就是在录入数据时一条一条插入到系统显得非常麻烦,让我实现一个直接通过excel导入的方法一次性录入所有数据.网上关于excel导入导出的例子很多,但大多相互借鉴. ...
- Octopus——excel导入导出工具
Octopus Octopus是一个简易的Excel导入导出工具.目前主要就两个功能: 导入:将excel中一行数据转换为指定的java对象,并通过指定的正则表达式检查合法性. 导出:按照给定的xml ...
- TP5.0 excel 导入导出
引第三方的phpexcel类库放到 ThinkPHP\Library\Vendor\demo下,自己建的文件夹demo 再将Excel.class放到ThinkPHP\Library\Org\clas ...
- Mego(04) - NET简单实现EXCEL导入导出
前言 相信做过信息系统的朋友都会遇到EXCEL导入导出的相关开发,做过不少EXCEL导入导出后总结起来大致有如下几种方式实现: ADO.NET的OldDb或ODBC连接EXCEL使用DataTable ...
- JeeSite中Excel导入导出
在各种管理系统中,数据的导入导出是经常用到的功能,通常导入导出以Excel.CSV格式居多.如果是学习的过程中,最好是自己实现数据导入与导出的功能,然而在项目中,还是调用现成的功能比较好.近期一直使用 ...
随机推荐
- 玩转Win32开发(2):完整的开发流程
上一篇中我给各位说了一般人认为C++中较为难的东西——指针.其实对于C++,难点当然不局限在指针这玩意儿上,还有一些有趣的概念,如模板类.虚基类.纯虚函数等,这些都是概念性的东西,几乎每一 ...
- mfc socket编程
socket编程用法---- 随着计算机网络化的深入,计算机网络编程在程序设计的过程中变得日益重要.由于C++语言对底层操作的优越性,许多文章都曾经介绍过用VC++进行Socket编程的方法.但由于都 ...
- VS2010调试小技巧
在VS下做开发的时候我们进行调试的时候路径是这个样子的:http://localhost:端口号/项目名称/index.aspx 但是发布到服务器上面的时候却是这个样子的:http://www.xxx ...
- 减少iOS应用程序安装包大小
安装包优化大小方法: <资源优化> 1.去除无用资源 通过几次项目的升级后,项目中会出现一些没有用到的图片.这些图片在我们导入到项目中后,之后项目升级过程后并没有再次用到. 那这些图片我们 ...
- Java基础知识强化57:经典排序之希尔排序(ShellSort)
1. 希尔排序的原理: 希尔排序(Shell Sort)是插入排序的一种.也称缩小增量排序,是直接插入排序算法的一种更高效的改进版本.希尔排序是非稳定排序算法.该方法因DL.Shell于1959年提出 ...
- ValidateBox( 验证框) 组件
本节课重点了解 EasyUI 中 ValidateBox(验证框)组件的使用方法,这个组件依赖于Tooltip(提示框)组件. 一. 加载方式//class 加载方式<input id=&quo ...
- 1.引入必要的文件 2.加载 UI 组件的方式 4.Parser 解析器
//引入 jQuery 核心库,这里采用的是 2.0 <scripttype="text/javascript"src="easyui/jquery.min.js& ...
- Linux操作系统分析__破解操作系统的奥秘
学号:SA12226343 姓名:sunhongbo 一.操作系统工作的基础 存储程序计算机和堆栈(函数调用堆栈)机制以及中断机制是操作系统工作的基础. 现代计算机仍采用存储程序计算机的结构体系和工 ...
- myeclipse笔记(3):导入的项目切换jdk版本
有时候,从外面导入的javaweb项目会访问不了,这个时候改变jdk版本就是其中解决的方法之一. 右键点击项目 --> bulid path --> configure 选择需要 ...
- C/C++中虚函数的调用
代码: #include <iostream> using namespace std; class A{ public: virtual void print(){ cout<&l ...