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

posted @ 2015-08-06 09:42  ttkuuk  阅读(290)  评论(0编辑  收藏  举报