• 读取excel


    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;
    }

  • 相关阅读:
    说说渐进式增强
    Websocket模板
    Echart图表显示单位
    JS配置文件设置,共享变量抽取
    PHP处理字符串的10个简单方法
    C#实现只许一个实例运行(使用mutex类)
    实现PHP基本安全11条
    PHP开发不能违背的安全规则
    Querying a motor position
    log info
  • 原文地址:https://www.cnblogs.com/luoxiaoxiao102/p/12938496.html
Copyright © 2020-2023  润新知