FileStream file;
ISheet sheet;
IWorkbook workbook = null;
string filepath = @"路径/文件名.xlsx";
file = new FileStream(filepath, FileMode.Open, FileAccess.Read);
// 2007版本及以上版本
if (filepath.IndexOf(".xlsx") > 0)
workbook = new XSSFWorkbook(file);
// 2003版本
else if (filepath.IndexOf(".xls") > 0)
workbook = new HSSFWorkbook(file);
//读取excel中第一个sheet
sheet = workbook.GetSheetAt(0);
try
{
if (workbook != null)
{
//EF context
StaffEntities staffEntities = new StaffEntities();
//赋值去除列头
for (int i = 1; i < sheet行数量; i++)
{
Staff staff = new Staff();
//赋值
sheet.GetRow(i).GetCell();
staffEntities.Staff.Add(staff);
}
//保存到数据库
staffEntities.SaveChanges();
}
file.Close();
}
catch (Exception ex)
{
throw ex;
}