NPOI是由国人开发的一个进行excel操作的第三方库。其介绍如下:NPOI
本文主要介绍如何使用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中即使某些单元格内容为空,但是其依旧占据了一个位置,所以在操作的时候需要格外注意。