CSDefineGenerator.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 CSDefineGenerator
  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 CSDefineGenerator(string excelName, ExcelLoader excel, string excludePrefix)
  27. {
  28. //-- 创建代码字符串
  29. StringBuilder sb = new StringBuilder();
  30. sb.AppendLine("//");
  31. sb.AppendLine("// 1. 每个 Sheet 形成一个 Struct 定义, Sheet 的名称作为 Struct 的名称");
  32. sb.AppendLine("// 2. 表格约定:第一行是变量名称,第二行是变量类型");
  33. sb.AppendLine();
  34. sb.AppendFormat("// Generate From {0}.xlsx", excelName);
  35. sb.AppendLine();
  36. sb.AppendLine();
  37. for (int i = 0; i < excel.Sheets.Count; i++)
  38. {
  39. DataTable sheet = excel.Sheets[i];
  40. sb.Append(_exportSheet(sheet, excludePrefix));
  41. }
  42. sb.AppendLine();
  43. sb.AppendLine("// End of Auto Generated Code");
  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. foreach (DataColumn column in sheet.Columns)
  58. {
  59. // 过滤掉包含指定前缀的列
  60. string columnName = column.ToString();
  61. if (excludePrefix.Length > 0 && columnName.StartsWith(excludePrefix))
  62. continue;
  63. FieldDef field;
  64. field.name = column.ToString();
  65. field.type = typeRow[column].ToString();
  66. field.comment = commentRow[column].ToString();
  67. fieldList.Add(field);
  68. }
  69. // export as string
  70. StringBuilder sb = new StringBuilder();
  71. sb.AppendFormat("public class {0}\r\n{{", sheet.TableName);
  72. sb.AppendLine();
  73. foreach (FieldDef field in fieldList)
  74. {
  75. sb.AppendFormat("\tpublic {0} {1}; // {2}", field.type, field.name, field.comment);
  76. sb.AppendLine();
  77. }
  78. sb.Append('}');
  79. sb.AppendLine();
  80. sb.AppendLine();
  81. return sb.ToString();
  82. }
  83. public void SaveToFile(string filePath, Encoding encoding)
  84. {
  85. //-- 保存文件
  86. using (FileStream file = new FileStream(filePath, FileMode.Create, FileAccess.Write))
  87. {
  88. using (TextWriter writer = new StreamWriter(file, encoding))
  89. writer.Write(mCode);
  90. }
  91. }
  92. }
  93. }