ExcelTool.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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, x, 4].Style.Border.Top.Style = OfficeOpenXml.Style.ExcelBorderStyle.Dotted;
  20. st.Cells[x, y].Value = list[i].Text;
  21. foreach (var ms in list[i].Meanings)
  22. {
  23. st.Cells[x, ++y].Value = ms.PhoneticEN;
  24. foreach (var cc in ms.CixingChinese)
  25. {
  26. st.Cells[x, ++y].Value = cc.Cixing;
  27. st.Cells[x++, ++y].Value = cc.Chinese;
  28. y = 2;
  29. }
  30. y = 1;
  31. }
  32. if ((i + 1) % count ==0)
  33. {
  34. st.Select("A:D");
  35. st.SelectedRange.AutoFitColumns();
  36. st.Name = (i - count + 2)+ "-" + (i + 1);
  37. st = ep.Workbook.Worksheets.Add("TTT");
  38. x = 1;
  39. y = 1;
  40. }
  41. }
  42. st.Cells.AutoFitColumns();
  43. st.Cells.Style.VerticalAlignment = OfficeOpenXml.Style.ExcelVerticalAlignment.Center;
  44. st.Cells.Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Left;
  45. st.Column(4).Style.WrapText = true;
  46. st.Column(4).Width = 55;
  47. st.Name = (list.Count / count) * count + 1 + "-" + ((list.Count / count) * count + (list.Count % count));
  48. st.InsertRow(1, 1);
  49. st.Cells[1, 1, 1, 4].Merge = true;
  50. st.Cells[1, 1].Value = "单词表";
  51. st.Cells[1, 1].Style.Font.Bold = true;
  52. st.Cells[1, 1].Style.Font.Size = 24;
  53. st.Column(1).Style.Font.Name = "Arial";
  54. st.Column(2).Style.Font.Name = "Arial";
  55. st.Column(3).Style.Font.Name = "Arial";
  56. st.Column(4).Style.Font.Name = "微软雅黑";
  57. st.Cells[1, 1].Style.Font.Name = "微软雅黑";
  58. ep.SaveAs(new System.IO.FileInfo(fileName));
  59. }
  60. }
  61. }
  62. }