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模型
- 下一篇: 哈皮大师燃气热水器质量可靠吗怎么样?