Program.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using OfficeOpenXml;
  6. namespace ET
  7. {
  8. struct HeadInfo
  9. {
  10. public string ClientServer;
  11. public string FieldDesc;
  12. public string FieldName;
  13. public string FieldType;
  14. public HeadInfo(string cs, string desc, string name, string type)
  15. {
  16. this.ClientServer = cs;
  17. this.FieldDesc = desc;
  18. this.FieldName = name;
  19. this.FieldType = type;
  20. }
  21. }
  22. class Program
  23. {
  24. private static string template;
  25. static void Main(string[] args)
  26. {
  27. template = File.ReadAllText("Template.txt");
  28. ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
  29. ExportExcel("/Users/tanghai/Documents/ET/Excel/StartZoneConfig.xlsx", new StringBuilder());
  30. }
  31. static void ExportExcel(string path, StringBuilder stringBuilder)
  32. {
  33. using Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
  34. using ExcelPackage p = new ExcelPackage(stream);
  35. List<HeadInfo> classField = new List<HeadInfo>();
  36. HashSet<string> uniqeField = new HashSet<string>();
  37. foreach (ExcelWorksheet worksheet in p.Workbook.Worksheets)
  38. {
  39. ExportSheet(worksheet, classField, uniqeField);
  40. }
  41. ExportClass(Path.GetFileNameWithoutExtension(path), classField, "./");
  42. }
  43. static void ExportSheet(ExcelWorksheet worksheet, List<HeadInfo> classField, HashSet<string> uniqeField)
  44. {
  45. const int row = 2;
  46. for (int col = 3; col < worksheet.Dimension.Columns; ++col)
  47. {
  48. string fieldName = worksheet.Cells[row + 2, col].Text.Trim();
  49. if (fieldName == "")
  50. {
  51. continue;
  52. }
  53. if (!uniqeField.Add(fieldName))
  54. {
  55. continue;
  56. }
  57. string fieldCS = worksheet.Cells[row, col].Text.Trim();
  58. string fieldDesc = worksheet.Cells[row + 1, col].Text.Trim();
  59. string fieldType = worksheet.Cells[row + 3, col].Text.Trim();
  60. classField.Add(new HeadInfo(fieldCS, fieldDesc, fieldName, fieldType));
  61. }
  62. }
  63. static void ExportClass(string protoName, List<HeadInfo> classField, string exportDir)
  64. {
  65. string exportPath = Path.Combine(exportDir, $"{protoName}.cs");
  66. using FileStream txt = new FileStream(exportPath, FileMode.Create);
  67. using StreamWriter sw = new StreamWriter(txt);
  68. StringBuilder sb = new StringBuilder();
  69. for (int i = 0; i < classField.Count; i++)
  70. {
  71. HeadInfo headInfo = classField[i];
  72. if (headInfo.ClientServer.StartsWith("#"))
  73. {
  74. continue;
  75. }
  76. sb.Append($"\t\t[ProtoMember({i + 1}, IsRequired = true)]\n");
  77. sb.Append($"\t\tpublic {headInfo.FieldType} {headInfo.FieldName} {{ get; set; }}\n");
  78. }
  79. string content = template.Replace("(ConfigName)", protoName).Replace(("(Fields)"), sb.ToString());
  80. sw.Write(content);
  81. }
  82. }
  83. }