当前位置: 移动技术网 > IT编程>开发语言>c# > C#使用NPOI导入Excel的方法详解

C#使用NPOI导入Excel的方法详解

2019年07月18日  | 移动技术网IT编程  | 我要评论

本文实例讲述了c#使用npoi导入excel的方法。分享给大家供大家参考,具体如下:

npoi是由国人开发的一个进行excel操作的第三方库。百度百科介绍如下:

本文主要介绍如何使用npoi将excel数据读取。

首先引入程序集:

using system.io;
using system.reflection;
using npoi.hssf.usermodel;
using npoi.ss.usermodel;
using system.web;

然后定位到文件位置:

string path = "~/上传文件/custompersonsalary/" + id + "/"+id+".xls";
string filepath = server.mappath(path);
filestream fs = new filestream(filepath, filemode.open, fileaccess.readwrite, fileshare.readwrite) //打开.xls文件

接下来,将xls文件中的数据写入workbook中:

hssfworkbook wk = new hssfworkbook(fs); //把xls文件中的数据写入wk中

wk.numberofsheets是xls文件中总共的表的个数。

wk.getsheetat(i)是获取第i个表的数据。

通过循环:

for (int i = 0; i < wk.numberofsheets; i++) //numberofsheets是xls文件中总共的表数

将每个表的数据单独存放在isheet对象中:

isheet sheet = wk.getsheetat(i); //读取当前表数据

这样某张表的数据就暂存在sheet对象中了。
接下来,我们可以通过sheet.lastrownum来获取行数,sheet.getrow(j)来获取第j行数据:

for (j = 1; j <= sheet.lastrownum; j++) //lastrownum 是当前表的总行数
{
  irow row = sheet.getrow(j); //读取当前行数据

每一行的数据又存在irow对象中。

我们可以通过row.lastcellnum来获取列数,row.cells[i]来获取第i列数据。

row.cells[2].tostring();

这里需要注意一点的就是,如果单元格中数据为公式计算而出的话,row.cells[i]会返回公式,需要改为:

row.cells[2].numericcellvalue

就可以返回计算结果了。

最后将我在项目中用到的一段导入excel数据赋予实体的示例如下:

/// <summary>
/// 导入操作
/// </summary>
/// @author: 刘放
/// @date: 2015/10/17
/// <param name="id">主表id</param>
/// <returns>如果成功,返回ok,如果失败,返回不满足格式的姓名</returns>
public string indb(string id)
{
 int j=0;
 stringbuilder sbr = new stringbuilder();
 string path = "~/上传文件/custompersonsalary/" + id + "/"+id+".xls";
 string filepath = server.mappath(path);
 using (filestream fs = new filestream(filepath, filemode.open, fileaccess.readwrite, fileshare.readwrite)) //打开123.xls文件
 {
  //定义一个工资详细集合
  list<hr_staffwage_details> staffwagelist = new list<hr_staffwage_details>();
  try
  {
   hssfworkbook wk = new hssfworkbook(fs); //把xls文件中的数据写入wk中
   for (int i = 0; i < wk.numberofsheets; i++) //numberofsheets是xls文件中总共的表数
   {
    isheet sheet = wk.getsheetat(i); //读取当前表数据
    for (j = 1; j <= sheet.lastrownum; j++) //lastrownum 是当前表的总行数
    {
     irow row = sheet.getrow(j); //读取当前行数据
     if (row != null)
     {
      //for (int k = 0; k <= row.lastcellnum; k++) //lastcellnum 是当前行的总列数
      //{
      //如果某一行的员工姓名,部门,岗位和员工信息表不对应,退出。
      sysentities db = new sysentities();
      if (commonhelp.isinhr_staffinfo(db, row.cells[2].tostring(), row.cells[0].tostring(), row.cells[1].tostring()) == false)//姓名,部门,岗位
      {
       //返回名字以便提示
       return row.cells[2].tostring();
      }
      //如果符合要求,这将值放入集合中。
      hr_staffwage_details hr_sw = new hr_staffwage_details();
      hr_sw.id = result.getnewidfornum("hr_staffwage_details");//生成编号
      hr_sw.sw_d_name = row.cells[2].tostring();//姓名
      hr_sw.sw_d_department = row.cells[0].tostring();//部门
      hr_sw.sw_d_position = row.cells[1].tostring();//职位
      hr_sw.sw_d_manhour = row.cells[3].tostring() != "" ? convert.todouble(row.cells[3].tostring()) : 0;//工数
      hr_sw.sw_d_postwage = row.cells[4].numericcellvalue.tostring() != "" ? convert.todouble(row.cells[4].numericcellvalue.tostring()) : 0;//基本工资
      hr_sw.sw_d_realpostwage = row.cells[5].numericcellvalue.tostring() != "" ? convert.todouble(row.cells[5].numericcellvalue.tostring()) : 0;//岗位工资
      hr_sw.sw_d_piecewage = row.cells[6].numericcellvalue.tostring() != "" ? convert.todouble(row.cells[6].numericcellvalue.tostring()) : 0;//计件工资
      hr_sw.sw_d_overtimepay = row.cells[7].numericcellvalue.tostring() != "" ? convert.todouble(row.cells[7].numericcellvalue.tostring()) : 0;//加班工资
      hr_sw.sw_d_yearwage = row.cells[8].numericcellvalue.tostring() != "" ? convert.todouble(row.cells[8].numericcellvalue.tostring()) : 0;//年假工资
      hr_sw.sw_d_middleshift = row.cells[9].numericcellvalue.tostring() != "" ? convert.todouble(row.cells[9].numericcellvalue.tostring()) : 0;//中班
      hr_sw.sw_d_nightshift = row.cells[10].numericcellvalue.tostring() != "" ? convert.todouble(row.cells[10].numericcellvalue.tostring()) : 0;//夜班
      hr_sw.sw_d_medicalaid = row.cells[11].numericcellvalue.tostring() != "" ? convert.todouble(row.cells[11].numericcellvalue.tostring()) : 0;//医补
      hr_sw.sw_d_dustfee = row.cells[12].numericcellvalue.tostring() != "" ? convert.todouble(row.cells[12].numericcellvalue.tostring()) : 0;//防尘费
      hr_sw.sw_d_other = row.cells[13].numericcellvalue.tostring() != "" ? convert.todouble(row.cells[13].numericcellvalue.tostring()) : 0;//其他
      hr_sw.sw_d_allowance = row.cells[14].numericcellvalue.tostring() != "" ? convert.todouble(row.cells[14].numericcellvalue.tostring()) : 0;//津贴
      hr_sw.sw_d_heat = row.cells[15].numericcellvalue.tostring() != "" ? convert.todouble(row.cells[15].numericcellvalue.tostring()) : 0;//防暑费
      hr_sw.sw_d_wash = row.cells[16].numericcellvalue.tostring() != "" ? convert.todouble(row.cells[16].numericcellvalue.tostring()) : 0;//澡费
      hr_sw.sw_d_subsidy = row.cells[17].numericcellvalue.tostring() != "" ? convert.todouble(row.cells[17].numericcellvalue.tostring()) : 0;//补助
      hr_sw.sw_d_bonus = row.cells[18].numericcellvalue.tostring() != "" ? convert.todouble(row.cells[18].numericcellvalue.tostring()) : 0;//奖金
      hr_sw.sw_d_fine = row.cells[19].numericcellvalue.tostring() != "" ? convert.todouble(row.cells[19].numericcellvalue.tostring()) : 0;//罚款
      hr_sw.sw_d_insurance = row.cells[20].numericcellvalue.tostring() != "" ? convert.todouble(row.cells[20].numericcellvalue.tostring()) : 0;//养老保险
      hr_sw.sw_d_medicalinsurance = row.cells[21].numericcellvalue.tostring() != "" ? convert.todouble(row.cells[21].numericcellvalue.tostring()) : 0;//医疗保险
      hr_sw.sw_d_lunch = row.cells[22].numericcellvalue.tostring() != "" ? convert.todouble(row.cells[22].numericcellvalue.tostring()) : 0;//餐费
      hr_sw.sw_d_delunch = row.cells[23].numericcellvalue.tostring() != "" ? convert.todouble(row.cells[23].numericcellvalue.tostring()) : 0;//扣餐费
      hr_sw.sw_d_de = row.cells[24].numericcellvalue.tostring() != "" ? convert.todouble(row.cells[24].numericcellvalue.tostring()) : 0;//扣项
      hr_sw.sw_d_week = row.cells[25].numericcellvalue.tostring() != "" ? convert.todouble(row.cells[25].numericcellvalue.tostring()) : 0;//星期
      hr_sw.sw_d_duplex = row.cells[26].numericcellvalue.tostring() != "" ? convert.todouble(row.cells[26].numericcellvalue.tostring()) : 0;//双工
      hr_sw.sw_d_shouldwage = row.cells[27].numericcellvalue.tostring() != "" ? convert.todouble(row.cells[27].numericcellvalue.tostring()) : 0;//应发金额
      hr_sw.sw_d_incometax = row.cells[28].numericcellvalue.tostring() != "" ? convert.todouble(row.cells[28].numericcellvalue.tostring()) : 0;//所得税
      hr_sw.sw_d_finalwage = row.cells[29].numericcellvalue.tostring() != "" ? convert.todouble(row.cells[29].numericcellvalue.tostring()) : 0;//实发金额
      hr_sw.sw_d_remark = row.cells[30].tostring();//备注
      hr_sw.sw_id = id;//外键
      hr_sw.sw_d_wagetype = null;//工资类型
      staffwagelist.add(hr_sw);
     }
    }
   }
  }
  catch (exception e) {
   //错误定位
   int k = j;
  }
  //再将list转入数据库
  double allfinalwage = 0;
  foreach (hr_staffwage_details item in staffwagelist)
  {
   sysentities db = new sysentities();
   db.addtohr_staffwage_details(item);
   db.savechanges();
   allfinalwage +=convert.todouble(item.sw_d_finalwage);
  }
  //将总计赋予主表
  sysentities dbt = new sysentities();
  hr_staffwage sw = commonhelp.gethr_staffwagebyid(dbt, id);
  sw.sw_wagesum = math.round(allfinalwage,2);
  dbt.savechanges();
 }
 sbr.tostring();
  return "ok";
}

最后需要注意一点的就是,excel中即使某些单元格内容为空,但是其依旧占据了一个位置,所以在操作的时候需要格外注意。

更多关于c#相关内容感兴趣的读者可查看本站专题:《c#操作excel技巧总结》、《c#程序设计之线程使用技巧总结》、《c#中xml文件操作技巧汇总》、《c#常见控件用法教程》、《winform控件用法总结》、《c#数据结构与算法教程》、《c#数组操作技巧总结》及《c#面向对象程序设计入门教程

希望本文所述对大家c#程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网