首页 > Web开发 > 详细

.NET NOPI插件EXCEL读取的使用

时间:2015-08-06 10:52:56      阅读:159      评论:0      收藏:0      [点我收藏+]

IWorkbook wk = null;
int isInt = 0;//用于判断是否为可转换为INT的值
if (Path.GetExtension(filePath) == ".xls")
{
wk = new HSSFWorkbook(fs);//把xls文件中的数据写入wk中 2003
}
else
{
wk = new XSSFWorkbook(fs);//把xls文件中的数据写入wk中 2007
}
for (int i = 0; i < wk.NumberOfSheets; i++) //NumberOfSheets是myxls.xls中总共的表数
{
ISheet sheet = wk.GetSheetAt(i); //读取当前表数据
for (int j = 1; j <= sheet.LastRowNum; j++) //LastRowNum 是当前表的总行数
{
IRow row = sheet.GetRow(j); //读取当前行数据
if (row != null)
{
ICell cell = null;
person = new BatchInsertPerson();
cell = row.GetCell(0); //姓名
person.Name = cell.ToString().Trim().Replace(" ", "");
cell = row.GetCell(1); //性别
person.Gender = cell.ToString().Trim().Replace(" ", "");
cell = row.GetCell(2); //证件类型
person.IDType = cell.ToString().Trim().Replace(" ", "");
cell = row.GetCell(3); //证件号码
person.IDNo = cell.ToString().Trim().Replace(" ", "");
cell = row.GetCell(4); //出生日期
person.Birthday = cell.ToString().Trim().Replace(" ", "");
cell = row.GetCell(5); //手机
person.Phone = cell.ToString().Trim().Replace(" ", "");
cell = row.GetCell(6); 
person.AirNo = cell.ToString().Trim().Replace(" ", "");
listperson.Add(person);
}
}
}

以上代码通过文件的后缀名判断文件是否是Excel2003还是2007的文件进行读取,通过GetSheetAt得到里面的工作表

通过GetRow得到工作表中的某行

通过GetCell得到行中的某列数据

.NET NOPI插件EXCEL读取的使用

原文:http://www.cnblogs.com/ttkuuk/p/4707116.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!