LuaDefineGenerator.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using System.IO;
  3. using System.Data;
  4. using System.Text;
  5. using System.Collections.Generic;
  6. namespace excel2json
  7. {
  8. /// <summary>
  9. /// 根据表头,生成C#类定义数据结构
  10. /// 表头使用三行定义:字段名称、字段类型、注释
  11. /// </summary>
  12. class LuaDefineGenerator
  13. {
  14. struct FieldDef
  15. {
  16. public string name;
  17. public string type;
  18. public string comment;
  19. }
  20. string mCode;
  21. public string code {
  22. get {
  23. return this.mCode;
  24. }
  25. }
  26. public LuaDefineGenerator(string excelName, ExcelLoader excel, string excludePrefix)
  27. {
  28. //-- 创建代码字符串
  29. StringBuilder sb = new StringBuilder();
  30. sb.AppendLine("--[[");
  31. sb.AppendLine("1. 每个 Sheet 形成一个 常量类 定义, Sheet 的名称作为 常量类 的名称");
  32. sb.AppendLine("2. 表格约定:第一行是变量名称,第二行是变量类型");
  33. sb.AppendLine();
  34. sb.AppendFormat("Generate From {0}", excelName);
  35. sb.AppendLine("\n]]--");
  36. sb.AppendLine();
  37. sb.AppendLine();
  38. for (int i = 0; i < excel.Sheets.Count; i++)
  39. {
  40. DataTable sheet = excel.Sheets[i];
  41. sb.Append(_exportSheet(sheet, excludePrefix));
  42. }
  43. sb.AppendLine();
  44. mCode = sb.ToString();
  45. }
  46. private string _exportSheet(DataTable sheet, string excludePrefix)
  47. {
  48. if (sheet.Columns.Count < 0 || sheet.Rows.Count < 2)
  49. return "";
  50. string sheetName = sheet.TableName;
  51. if (excludePrefix.Length > 0 && sheetName.StartsWith(excludePrefix))
  52. return "";
  53. // get field list
  54. List<FieldDef> fieldList = new List<FieldDef>();
  55. DataRow typeRow = sheet.Rows[0];
  56. DataRow commentRow = sheet.Rows[1];
  57. int firstDataRow = 2;
  58. for (int i = firstDataRow; i < sheet.Rows.Count; i++)
  59. {
  60. DataRow row = sheet.Rows[i];
  61. string key = row[sheet.Columns[0]].ToString();
  62. if (excludePrefix.Length > 0 && key.StartsWith(excludePrefix))
  63. continue;
  64. FieldDef field;
  65. field.name = key.ToString();
  66. field.type = "";
  67. field.comment = key.ToString();
  68. fieldList.Add(field);
  69. }
  70. // export as string
  71. StringBuilder sb = new StringBuilder();
  72. sb.AppendFormat("local {0}Key =\r\n{{", sheet.TableName);
  73. sb.AppendLine();
  74. foreach (FieldDef field in fieldList)
  75. {
  76. sb.AppendFormat("\t{0} = \"{1}\", ",field.name, field.comment);
  77. sb.AppendLine();
  78. }
  79. sb.Append('}');
  80. sb.AppendFormat("\n\nreturn {0}Key", sheet.TableName);
  81. sb.AppendLine();
  82. sb.AppendLine();
  83. return sb.ToString();
  84. }
  85. public void SaveToFile(string filePath, Encoding encoding)
  86. {
  87. //-- 保存文件
  88. using (FileStream file = new FileStream(filePath, FileMode.Create, FileAccess.Write))
  89. {
  90. using (TextWriter writer = new StreamWriter(file, encoding))
  91. writer.Write(mCode);
  92. }
  93. }
  94. }
  95. }