zoukankan      html  css  js  c++  java
  • C# 按部门拆分excel文件

    按照所属部门不同将excel文件拆分成多个文件

    string excel_path = @"G:zhyueackup2018-08-01 读取腾讯邮箱接口-获取一个月内未接收到外部邮件且已离职的邮箱address_biz (4).csv";
    string save_path = @"G:zhyueackup2018-08-01 读取腾讯邮箱接口-获取一个月内未接收到外部邮件且已离职的邮箱拆分excel";
    
    //解决中文乱码
    TxtLoadOptions lo = new TxtLoadOptions();
    lo.Encoding = Encoding.Default;
    
    //打开excel文件
    Workbook curr_wb = File.Exists(excel_path) ? new Workbook(excel_path, lo) : new Workbook();
    //打开第一个sheet
    Worksheet sheet_first = curr_wb.Worksheets[0];
    Cells Cells = sheet_first.Cells;
    //一共多少行数据
    int rows = sheet_first.Cells.MaxDataRow + 1;
    
    //不同的部门名称集合
    List<string> lst_departments = new List<string>();
    //excel的数据集合
    List<UserInfo> lst_excel = new List<UserInfo>();
    
    for (int i = 1; i < rows; i++)
    {//从第二行开始
        lst_excel.Add(new UserInfo()
        {
            username = Cells[i, 0].StringValue,
            email = Cells[i, 1].StringValue,
            othername = Cells[i, 2].StringValue,
            department = Cells[i, 3].StringValue,
            contact = Cells[i, 4].StringValue,
            phone = Cells[i, 5].StringValue,
            sex = Cells[i, 6].StringValue,
            position = Cells[i, 7].StringValue,
            number = Cells[i, 8].StringValue,
            bindwechat = Cells[i, 9].StringValue,
            remark = Cells[i, 10].StringValue
        });
    }
    
    //遍历不同的部门
    lst_excel.Select(x => x.department).Distinct().ToList().ForEach(x =>
    {
        Workbook wb1 = new Workbook();
        Worksheet sheet_first1 = wb1.Worksheets[0];
        Cells Cells1 = sheet_first1.Cells;
    
        //新生成的excel文件名
        string[] str_arr1 = x.Split('/');
        string department_new_name = str_arr1.Length > 1 ? x.Substring(x.IndexOf('/') + 1).Replace('/', '-') : x;
    
        Cells1[0, 0].PutValue("姓名");
        Cells1[0, 1].PutValue("电子邮件");
        Cells1[0, 2].PutValue("别名");
        Cells1[0, 3].PutValue("所属部门");
        Cells1[0, 4].PutValue("联系电话");
        Cells1[0, 5].PutValue("手机");
        Cells1[0, 6].PutValue("性别");
        Cells1[0, 7].PutValue("职务");
        Cells1[0, 8].PutValue("编号");
        Cells1[0, 9].PutValue("绑定微信");
        Cells1[0, 10].PutValue("备注");
        int i = 1;//当前行数 从第2行开始
        //查找excel中的这些部门并遍历
        lst_excel.Where(y => y.department == x).ToList().ForEach(y =>
        {
            Cells1[i, 0].PutValue(y.username);
            Cells1[i, 1].PutValue(y.email);
            Cells1[i, 2].PutValue(y.othername);
            Cells1[i, 3].PutValue(y.department);
            Cells1[i, 4].PutValue(y.contact);
            Cells1[i, 5].PutValue(y.phone);
            Cells1[i, 6].PutValue(y.sex);
            Cells1[i, 7].PutValue(y.position);
            Cells1[i, 8].PutValue(y.number);
            Cells1[i, 9].PutValue(y.bindwechat);
            Cells1[i, 10].PutValue(y.remark);
    
            i++;
        });
    
        wb1.Save(save_path + department_new_name + ".xlsx", SaveFormat.Xlsx);
    });

    结果:

    每个excel里面都只有自己部门的数据

  • 相关阅读:
    HDFS文件系统简单的Java读写操作
    HDFS文件读写流程简单图解
    大数据入门---------------------Java部分开始
    Hive 基础入门
    安卓 2.3 安装及环境搭建
    字节流与字符流的区别详解(转)
    Java File 类的使用方法详解(转)
    ThinkPHP Session 使用
    PHP TP框架
    对象和数组的相互转化
  • 原文地址:https://www.cnblogs.com/zhyue93/p/Csharp_excel.html
Copyright © 2011-2022 走看看