Program.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. static void Main(string[] args)
  25. {
  26. ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
  27. ExportExcel("/Users/tanghai/Documents/ET/Excel/StartMachineConfig.xlsx", new StringBuilder());
  28. }
  29. static void ExportExcel(string path, StringBuilder stringBuilder)
  30. {
  31. FileInfo fileInfo = new FileInfo(path);
  32. using ExcelPackage p = new ExcelPackage(fileInfo);
  33. List<HeadInfo> classField = new List<HeadInfo>();
  34. foreach (ExcelWorksheet worksheet in p.Workbook.Worksheets)
  35. {
  36. ExportSheet(worksheet, classField);
  37. }
  38. ExportClass(Path.GetFileNameWithoutExtension(path), classField, "");
  39. foreach (HeadInfo headInfo in classField)
  40. {
  41. Console.WriteLine($"{headInfo.FieldName} {headInfo.FieldType} {headInfo.FieldDesc}");
  42. }
  43. }
  44. static void ExportSheet(ExcelWorksheet worksheet, List<HeadInfo> classField)
  45. {
  46. const int row = 2;
  47. for (int col = 3; col < worksheet.Dimension.Columns; ++col)
  48. {
  49. string fieldCS = worksheet.Cells[row, col].Text.Trim();
  50. string fieldDesc = worksheet.Cells[row + 1, col].Text.Trim();
  51. string fieldName = worksheet.Cells[row + 2, col].Text.Trim();
  52. string fieldType = worksheet.Cells[row + 3, col].Text.Trim();
  53. if (fieldName == "")
  54. {
  55. continue;
  56. }
  57. classField.Add(new HeadInfo(fieldCS, fieldDesc, fieldName, fieldType));
  58. }
  59. }
  60. static void ExportClass(string protoName, List<HeadInfo> classField, string exportDir)
  61. {
  62. string exportPath = Path.Combine(exportDir, $"{protoName}.cs");
  63. using (FileStream txt = new FileStream(exportPath, FileMode.Create))
  64. using (StreamWriter sw = new StreamWriter(txt))
  65. {
  66. StringBuilder sb = new StringBuilder();
  67. sb.Append("namespace ET\n{");
  68. sb.Append($"\t[Config]\n");
  69. sb.Append($"\tpublic partial class {protoName}Category : ACategory<{protoName}>\n");
  70. sb.Append("\t{\n");
  71. sb.Append($"\t\tpublic static {protoName}Category Instance;\n");
  72. sb.Append($"\t\tpublic {protoName}Category()\n");
  73. sb.Append("\t\t{\n");
  74. sb.Append($"\t\t\tInstance = this;\n");
  75. sb.Append("\t\t}\n");
  76. sb.Append("\t}\n\n");
  77. sb.Append($"\tpublic partial class {protoName}: IConfig\n");
  78. sb.Append("\t{\n");
  79. sb.Append("\t\t[BsonId]\n");
  80. sb.Append("\t\tpublic long Id { get; set; }\n");
  81. for (int i = 0; i < classField.Count; i++)
  82. {
  83. HeadInfo headInfo = classField[i];
  84. if (headInfo.ClientServer.StartsWith("#"))
  85. {
  86. continue;
  87. }
  88. sb.Append($"\t\tpublic {headInfo.FieldType} {headInfo.FieldName} {{ get; set;}};\n");
  89. }
  90. sb.Append("\t}\n");
  91. sb.Append("}\n");
  92. sw.Write(sb.ToString());
  93. }
  94. }
  95. }
  96. }