ExcelTool.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using OfficeOpenXml;
  6. namespace TxtDictionary
  7. {
  8. class ExcelTool
  9. {
  10. public void Save(string fileName, List<Word> list, int count)
  11. {
  12. using (ExcelPackage ep = new ExcelPackage())
  13. {
  14. var st = ep.Workbook.Worksheets.Add("TTT");
  15. int x = 1;
  16. int y = 1;
  17. for (int i = 0; i < list.Count; i++)
  18. {
  19. st.Cells[x, y].Value = list[i].Text;
  20. foreach (var ms in list[i].Meanings)
  21. {
  22. st.Cells[x, ++y].Value = ms.PhoneticEN;
  23. foreach (var cc in ms.CixingChinese)
  24. {
  25. st.Cells[x, ++y].Value = cc.Cixing;
  26. st.Cells[x++, ++y].Value = cc.Chinese;
  27. y = 2;
  28. }
  29. y = 1;
  30. }
  31. if ((i + 1) % count ==0)
  32. {
  33. st.Select("A:D");
  34. st.SelectedRange.AutoFitColumns();
  35. st.Name = (i - count + 2)+ "-" + (i + 1);
  36. st = ep.Workbook.Worksheets.Add("TTT");
  37. x = 1;
  38. y = 1;
  39. }
  40. }
  41. st.Select("A:D");
  42. st.SelectedRange.AutoFitColumns();
  43. st.Name = (list.Count / count) * count + 1 + "-" + ((list.Count / count) * count + (list.Count % count));
  44. ep.SaveAs(new System.IO.FileInfo(fileName));
  45. }
  46. }
  47. }
  48. }