.NET NOPI插件EXCEL读取的使用

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得到行中的某列数据

转载于:https://www.cnblogs.com/ttkuuk/p/4707116.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值