Program.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using OfficeOpenXml;
  6. namespace ET
  7. {
  8. public enum ConfigType
  9. {
  10. Client,
  11. Server,
  12. }
  13. struct HeadInfo
  14. {
  15. public string FieldAttribute;
  16. public string FieldDesc;
  17. public string FieldName;
  18. public string FieldType;
  19. public HeadInfo(string cs, string desc, string name, string type)
  20. {
  21. this.FieldAttribute = cs;
  22. this.FieldDesc = desc;
  23. this.FieldName = name;
  24. this.FieldType = type;
  25. }
  26. }
  27. class Program
  28. {
  29. private static string template;
  30. private const string excelDir = "../../Excel";
  31. private const string classDir = "../../Generate/{0}/Code/Config";
  32. private const string jsonDir = "../../Generate/{0}/Json";
  33. static void Main(string[] args)
  34. {
  35. template = File.ReadAllText("Template.txt");
  36. ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
  37. foreach (string path in Directory.GetFiles(excelDir, "*.xlsx"))
  38. {
  39. using Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
  40. using ExcelPackage p = new ExcelPackage(stream);
  41. string name = Path.GetFileNameWithoutExtension(path);
  42. ExportExcelClass(p, name, ConfigType.Client);
  43. ExportExcelClass(p, name, ConfigType.Server);
  44. ExportExcelJson(p, name, ConfigType.Client);
  45. ExportExcelJson(p, name, ConfigType.Server);
  46. }
  47. }
  48. #region 导出class
  49. static void ExportExcelClass(ExcelPackage p, string name, ConfigType configType)
  50. {
  51. List<HeadInfo> classField = new List<HeadInfo>();
  52. HashSet<string> uniqeField = new HashSet<string>();
  53. foreach (ExcelWorksheet worksheet in p.Workbook.Worksheets)
  54. {
  55. ExportSheetClass(worksheet, classField, uniqeField, configType);
  56. }
  57. ExportClass(name, classField, configType);
  58. }
  59. static void ExportSheetClass(ExcelWorksheet worksheet, List<HeadInfo> classField, HashSet<string> uniqeField, ConfigType configType)
  60. {
  61. const int row = 2;
  62. for (int col = 3; col <= worksheet.Dimension.End.Column; ++col)
  63. {
  64. string fieldName = worksheet.Cells[row + 2, col].Text.Trim();
  65. if (fieldName == "")
  66. {
  67. continue;
  68. }
  69. if (!uniqeField.Add(fieldName))
  70. {
  71. continue;
  72. }
  73. string fieldCS = worksheet.Cells[row, col].Text.Trim();
  74. string fieldDesc = worksheet.Cells[row + 1, col].Text.Trim();
  75. string fieldType = worksheet.Cells[row + 3, col].Text.Trim();
  76. classField.Add(new HeadInfo(fieldCS, fieldDesc, fieldName, fieldType));
  77. }
  78. }
  79. static void ExportClass(string protoName, List<HeadInfo> classField, ConfigType configType)
  80. {
  81. string exportPath = Path.Combine(string.Format(classDir, configType.ToString()), $"{protoName}.cs");
  82. using FileStream txt = new FileStream(exportPath, FileMode.Create);
  83. using StreamWriter sw = new StreamWriter(txt);
  84. StringBuilder sb = new StringBuilder();
  85. for (int i = 0; i < classField.Count; i++)
  86. {
  87. HeadInfo headInfo = classField[i];
  88. if (headInfo.FieldAttribute.StartsWith("#"))
  89. {
  90. continue;
  91. }
  92. sb.Append($"\t\t[ProtoMember({i + 1}, IsRequired = true)]\n");
  93. sb.Append($"\t\tpublic {headInfo.FieldType} {headInfo.FieldName} {{ get; set; }}\n");
  94. }
  95. string content = template.Replace("(ConfigName)", protoName).Replace(("(Fields)"), sb.ToString());
  96. sw.Write(content);
  97. }
  98. #endregion
  99. static void ExportExcelJson(ExcelPackage p, string name, ConfigType configType)
  100. {
  101. StringBuilder sb = new StringBuilder();
  102. sb.AppendLine("{\"list\":[");
  103. foreach (ExcelWorksheet worksheet in p.Workbook.Worksheets)
  104. {
  105. ExportSheetJson(worksheet, configType, sb);
  106. }
  107. sb.AppendLine("]}");
  108. string jsonPath = Path.Combine(string.Format(jsonDir, configType.ToString()), $"{name}.txt");
  109. using FileStream txt = new FileStream(jsonPath, FileMode.Create);
  110. using StreamWriter sw = new StreamWriter(txt);
  111. sw.Write(sb.ToString());
  112. }
  113. static void ExportSheetJson(ExcelWorksheet worksheet, ConfigType configType, StringBuilder sb)
  114. {
  115. int infoRow = 2;
  116. List<HeadInfo> headInfos = new List<HeadInfo>();
  117. headInfos.Add(new HeadInfo());
  118. headInfos.Add(new HeadInfo());
  119. headInfos.Add(new HeadInfo());
  120. for (int col = 3; col <= worksheet.Dimension.End.Column; ++col)
  121. {
  122. string fieldName = worksheet.Cells[infoRow + 2, col].Text.Trim();
  123. if (fieldName == "")
  124. {
  125. continue;
  126. }
  127. string fieldCS = worksheet.Cells[infoRow, col].Text.Trim();
  128. string fieldDesc = worksheet.Cells[infoRow + 1, col].Text.Trim();
  129. string fieldType = worksheet.Cells[infoRow + 3, col].Text.Trim();
  130. headInfos.Add(new HeadInfo(fieldCS, fieldDesc, fieldName, fieldType));
  131. }
  132. for (int row = 6; row <= worksheet.Dimension.End.Row; ++row)
  133. {
  134. sb.Append("{");
  135. for (int col = 3; col < worksheet.Dimension.End.Column; ++col)
  136. {
  137. HeadInfo headInfo = headInfos[col];
  138. if (headInfo.FieldAttribute.Contains("#"))
  139. {
  140. continue;
  141. }
  142. if (headInfo.FieldName == "Id")
  143. {
  144. headInfo.FieldName = "_id";
  145. }
  146. else
  147. {
  148. sb.Append(",");
  149. }
  150. sb.Append($"\"{headInfo.FieldName}\":{Convert(headInfo.FieldType, worksheet.Cells[row, col].Text.Trim())}");
  151. }
  152. sb.Append("},\n");
  153. }
  154. }
  155. private static string Convert(string type, string value)
  156. {
  157. switch (type)
  158. {
  159. case "int[]":
  160. case "int32[]":
  161. case "long[]":
  162. return $"[{value}]";
  163. case "string[]":
  164. return $"[{value}]";
  165. case "int":
  166. case "int32":
  167. case "int64":
  168. case "long":
  169. case "float":
  170. case "double":
  171. return value;
  172. case "string":
  173. return $"\"{value}\"";
  174. default:
  175. throw new Exception($"不支持此类型: {type}");
  176. }
  177. }
  178. }
  179. }