private List<ExcelSalaryEmployeeFailDto> LoadExcel(string path)
{
var appFolders = IocManager.Instance.Resolve<IAppFolders>();
path = appFolders.GetFullPath(path);
var list = new List<ExcelSalaryEmployeeFailDto>();
using (var fs = new FileStream(path, FileMode.Open))
{
IWorkbook workbook = WorkbookFactory.Create(fs);
var sheet = workbook.GetSheetAt(0);
//获取指定列在工作表中的列索引
var nameIndex = new Dictionary<string, int>();
int rowNum = 1;
for (int n= 0 ;n< sheet.LastRowNum; n++)
{
var row = sheet.GetRow(n);//获取工作表表头
if (row == null)
{
continue;
}
for (int i = 0; i < row.LastCellNum; i++)
{
if (row.GetCell(i) != null)
{
switch (row.GetCell(i).ToString())
{
case "姓名":
if (!nameIndex.ContainsKey("姓名"))
{
nameIndex.Add("姓名", i);
rowNum = n;
}
else
{
nameIndex["姓名"] = i;
rowNum = n;
}
break;
case "状态":
if (!nameIndex.ContainsKey("状态"))
{
nameIndex.Add("状态", i);
}
else
{
nameIndex["状态"] = i;
}
break;
}
}
}
}
if (nameIndex.Count < 3)
{
throw new Exception($"Excel模板需要姓名、状态数据!");
}
for (int i = rowNum+1; i <= sheet.LastRowNum; i++)
{
var row = sheet.GetRow(i);
if (row == null)
{
break;
}
else if (row.GetCell(nameIndex["状态"]) != null)
{
if ( row.GetCell(nameIndex["状态"]).StringCellValue.ToString().Contains("成功"))
{
var dto = new ExcelSalaryEmployeeFailDto
{
Name = string.Empty,
IdentityCard = string.Empty,
Remark = ""
};
if (row.GetCell(nameIndex["姓名"]) != null)
{
dto.Name = row.GetCell(nameIndex["姓名"]).StringCellValue.ToString();
}
dto.IsSuccess = true;
list.Add(dto);
}
else
{
var dto = new ExcelSalaryEmployeeFailDto
{
Name = string.Empty,
IdentityCard = string.Empty,
Remark = "原因未注明"
};
if (row.GetCell(nameIndex["姓名"]) != null)
{
dto.Name = row.GetCell(nameIndex["姓名"]).StringCellValue.ToString();
}
dto.IsSuccess = false;
list.Add(dto);
}
}
}
}
return list;
}