public IList<T> ExportToList<T>(ISheet sheet, string[] fields) where T : class,new()
{
IList<T> list = new List<T>(); //遍历每一行数据
for (int i = sheet.FirstRowNum + , len = sheet.LastRowNum + ; i < len; i++)
{
T t = new T();
IRow row = sheet.GetRow(i); for (int j = , len2 = fields.Length; j < len2; j++)
{
Type propertyType = typeof(T).GetProperty(fields[j]).PropertyType; //获取当前属性的类型
ICell cell = row.GetCell(j);
object cellValue = null; if (cell == null)
{
continue;
} if(propertyType == typeof(string) | cell.CellType == CellType.Blank)
{
cell.SetCellType(CellType.String);
cellValue = cell.StringCellValue;
}
if (propertyType == typeof(int) && cell.CellType != CellType.Blank)
{
cell.SetCellType(CellType.Numeric);
cellValue = Convert.ToInt32(cell.NumericCellValue); //Double转int
}
if (propertyType == typeof(bool))
{
cell.SetCellType(CellType.Boolean);
cellValue = cell.BooleanCellValue;
} typeof(T).GetProperty(fields[j]).SetValue(t, cellValue, null);
}
list.Add(t);
}
return list;
}

调用如下:

static void Main(string[] args)
{ string _fromfile = @"D:\code\csharp\person.xlsx";
string _tofile = @"D:\test.xlsx"; IWorkbook book = null;
try
{
book = new XSSFWorkbook(_fromfile);
}
catch (IOException ex)
{
Console.WriteLine(ex.Message);
}
catch (Exception ex)
{
book = new HSSFWorkbook(File.OpenRead(_fromfile));
} ISheet sheet = book.GetSheet("person"); ExcelHelper helper = new ExcelHelper(_fromfile); string[] properties = new string[] { "name", "age", "sex", "id", "height", "weight", "country", "hometown", "phone" };
foreach (var p in helper.ExportToList<Person>(sheet, properties))
{
Console.WriteLine(" " + p.name.GetType() + " " + p.phone + " " + p.sex + " " + p.weight.GetType());
}
Console.Read();
}

说明:以上代码可以实现从Excel中读取数据,并将每个sheet里的对象集合放在一个list中。对于一个Excel,多次调用以上方法即可。

这些代码只是初步取出了数据,并没有进行数据为空等的校验,需要根据业务需要进行修改。

C# 将sheet中数据转为list的更多相关文章

  1. Java读取excel指定sheet中的各行数据,存入二维数组,包括首行,并打印

    1. 读取 //读取excel指定sheet中的各行数据,存入二维数组,包括首行 public static String[][] getSheetData(XSSFSheet sheet) thro ...

  2. python操作txt文件中数据教程[3]-python读取文件夹中所有txt文件并将数据转为csv文件

    python操作txt文件中数据教程[3]-python读取文件夹中所有txt文件并将数据转为csv文件 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 python操作txt文件中 ...

  3. python2/3中 将base64数据写成图片,并将图片数据转为16进制数据的方法、bytes/string的区别

    1.python2将base64数据写成图片,并将数据转为16进制字符串的方法 import binascii img = u'R0lGODlhagAeAIcAAAAAAAAARAAAiAAAzABE ...

  4. 在论坛中出现的比较难的sql问题:37(动态行转列 某一行数据转为列名)

    原文:在论坛中出现的比较难的sql问题:37(动态行转列 某一行数据转为列名) 所以,觉得有必要记录下来,这样以后再次碰到这类问题,也能从中获取解答的思路.

  5. JavaScript中数据类型转换总结

    JavaScript中数据类型转换总结 在js中,数据类型转换分为显式数据类型转换和隐式数据类型转换. 1, 显式数据类型转换 a:转数字: 1)Number转换: 代码: var a = " ...

  6. 【matlab】将matlab中数据输出保存为txt或dat格式

    将matlab中数据输出保存为txt或dat格式 总结网上各大论坛,主要有三种方法. 第一种方法:save(最简单基本的) 具体的命令是:用save *.txt -ascii x x为变量 *.txt ...

  7. sheet目标数据插入函数主键模块

    #coding:gbk #导入处理excel的模块 import xlrd #定义哪些字段需要判断,只支持时间字段 toSureColArray = ['CREATE_TIME','MODIFY_TI ...

  8. 将Excel中数据导入数据库(一)

    在工作中经常要将Excel中数据导入数据库,这里介绍一种方法. 假如Excel中的数据如下: 数据库建表如下: 其中Id为自增字段: Excel中数据导入数据库帮助类如下: using System; ...

  9. python——快速找出两个电子表中数据的差异

    最近刚接触python,找点小任务来练练手,希望自己在实践中不断的锻炼自己解决问题的能力. 公司里会有这样的场景:有一张电子表格的内容由两三个部门或者更多的部门用到,这些员工会在维护这些表格中不定期的 ...

随机推荐

  1. 用EXCEL内嵌的Visual Basic for Application 编程,通过 UGSimple USB-GPIB 控制器来驱动仪器34401A,并从34401A读取数据

    现在市场上有很多中USB-GPIB 控制器,或叫 USB 转GPIB链接线. 每种GPIB控制器都有它的 函数库(dll库).各种GPIB 控制器的价钱插别很大.这里以一种价钱较便宜的USB-GPIB ...

  2. java类型占用字节数&类型转换

    1.整型类型              存储需求     bit数    取值范围      备注int                 4字节           4*8 short         ...

  3. WCF启用日志追踪

    调用使用http post调用WCF Restful服务时,WCF会自动反序列化body里的实体,如果实体反序列化不成功时,会返回一个请求错误,让去看服务器日志.需要启用日志追踪功能,才能看到具体的情 ...

  4. PHP防SQL注入不要再用addslashes和mysql_real_escape_string

    PHP防SQL注入不要再用addslashes和mysql_real_escape_string了,有需要的朋友可以参考下. 博主热衷各种互联网技术,常啰嗦,时常伴有强迫症,常更新,觉得文章对你有帮助 ...

  5. PHP开发网站之微信登录、绑定

    )))刷新access_token()); ); ); curl_setopt($curlobj, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curlo ...

  6. golang中string以及slice之间的一些问题

    好记性不如烂笔头o_O slice切片不会开辟新的空间 a := []int{0,1,2,3} b := make([]int, 8) b = a[:] b[2] = 9 fmt.Println(a) ...

  7. linux学习 联网

    /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0ONBOOT=yesBOOTPROTO=staticIPADDR=192.168.199.** ...

  8. SQL Lazy Spool Eager Spool

    https://www.simple-talk.com/sql/learn-sql-server/showplan-operator-of-the-week-lazy-spool/ The Lazy ...

  9. Python 无限循环

    import threading import time class CountDownTimer(threading.Thread): def __init__(self, seconds, act ...

  10. MFC 获取图像的大小

    // 获致图像的大小 int CCImageDialog::GetImageSize(CSize& size, CString filename) { CImage image; image. ...