C# 按部门拆分excel文件
生活随笔
收集整理的这篇文章主要介绍了
C# 按部门拆分excel文件
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
按照所属部门不同将excel文件拆分成多个文件
string excel_path = @"G:\zhyue\backup\2018-08-01 读取腾讯邮箱接口-获取一个月内未接收到外部邮件且已离职的邮箱\address_biz (4).csv"; string save_path = @"G:\zhyue\backup\2018-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里面都只有自己部门的数据
转载于:https://www.cnblogs.com/zhyue93/p/Csharp_excel.html
总结
以上是生活随笔为你收集整理的C# 按部门拆分excel文件的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: python - IO模型
- 下一篇: C#引用类型转换的几种方式