C# 按部门拆分excel文件
生活随笔
收集整理的這篇文章主要介紹了
C# 按部门拆分excel文件
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
按照所屬部門(mén)不同將excel文件拆分成多個(gè)文件
string excel_path = @"G:\zhyue\backup\2018-08-01 讀取騰訊郵箱接口-獲取一個(gè)月內(nèi)未接收到外部郵件且已離職的郵箱\address_biz (4).csv"; string save_path = @"G:\zhyue\backup\2018-08-01 讀取騰訊郵箱接口-獲取一個(gè)月內(nèi)未接收到外部郵件且已離職的郵箱\拆分excel\";//解決中文亂碼 TxtLoadOptions lo = new TxtLoadOptions(); lo.Encoding = Encoding.Default;//打開(kāi)excel文件 Workbook curr_wb = File.Exists(excel_path) ? new Workbook(excel_path, lo) : new Workbook(); //打開(kāi)第一個(gè)sheet Worksheet sheet_first = curr_wb.Worksheets[0]; Cells Cells = sheet_first.Cells; //一共多少行數(shù)據(jù) int rows = sheet_first.Cells.MaxDataRow + 1;//不同的部門(mén)名稱集合 List<string> lst_departments = new List<string>(); //excel的數(shù)據(jù)集合 List<UserInfo> lst_excel = new List<UserInfo>();for (int i = 1; i < rows; i++) {//從第二行開(kā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}); }//遍歷不同的部門(mén) 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("所屬部門(mén)");Cells1[0, 4].PutValue("聯(lián)系電話");Cells1[0, 5].PutValue("手機(jī)");Cells1[0, 6].PutValue("性別");Cells1[0, 7].PutValue("職務(wù)");Cells1[0, 8].PutValue("編號(hào)");Cells1[0, 9].PutValue("綁定微信");Cells1[0, 10].PutValue("備注");int i = 1;//當(dāng)前行數(shù) 從第2行開(kāi)始//查找excel中的這些部門(mén)并遍歷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); });結(jié)果:
每個(gè)excel里面都只有自己部門(mén)的數(shù)據(jù)
轉(zhuǎn)載于:https://www.cnblogs.com/zhyue93/p/Csharp_excel.html
總結(jié)
以上是生活随笔為你收集整理的C# 按部门拆分excel文件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: python - IO模型
- 下一篇: c# char unsigned_dll