-
Notifications
You must be signed in to change notification settings - Fork 490
/
CSDefineGenerator.cs
112 lines (95 loc) · 3.57 KB
/
CSDefineGenerator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using System;
using System.IO;
using System.Data;
using System.Text;
using System.Collections.Generic;
namespace excel2json
{
/// <summary>
/// 根据表头,生成C#类定义数据结构
/// 表头使用三行定义:字段名称、字段类型、注释
/// </summary>
class CSDefineGenerator
{
struct FieldDef
{
public string name;
public string type;
public string comment;
}
string mCode;
public string code {
get {
return this.mCode;
}
}
public CSDefineGenerator(string excelName, ExcelLoader excel, string excludePrefix)
{
//-- 创建代码字符串
StringBuilder sb = new StringBuilder();
sb.AppendLine("//");
sb.AppendLine("// Auto Generated Code By excel2json");
sb.AppendLine("// https://neil3d.gitee.io/coding/excel2json.html");
sb.AppendLine("// 1. 每个 Sheet 形成一个 Struct 定义, Sheet 的名称作为 Struct 的名称");
sb.AppendLine("// 2. 表格约定:第一行是变量名称,第二行是变量类型");
sb.AppendLine();
sb.AppendFormat("// Generate From {0}.xlsx", excelName);
sb.AppendLine();
sb.AppendLine();
for (int i = 0; i < excel.Sheets.Count; i++)
{
DataTable sheet = excel.Sheets[i];
sb.Append(_exportSheet(sheet, excludePrefix));
}
sb.AppendLine();
sb.AppendLine("// End of Auto Generated Code");
mCode = sb.ToString();
}
private string _exportSheet(DataTable sheet, string excludePrefix)
{
if (sheet.Columns.Count < 0 || sheet.Rows.Count < 2)
return "";
string sheetName = sheet.TableName;
if (excludePrefix.Length > 0 && sheetName.StartsWith(excludePrefix))
return "";
// get field list
List<FieldDef> fieldList = new List<FieldDef>();
DataRow typeRow = sheet.Rows[0];
DataRow commentRow = sheet.Rows[1];
foreach (DataColumn column in sheet.Columns)
{
// 过滤掉包含指定前缀的列
string columnName = column.ToString();
if (excludePrefix.Length > 0 && columnName.StartsWith(excludePrefix))
continue;
FieldDef field;
field.name = column.ToString();
field.type = typeRow[column].ToString();
field.comment = commentRow[column].ToString();
fieldList.Add(field);
}
// export as string
StringBuilder sb = new StringBuilder();
sb.AppendFormat("public class {0}\r\n{{", sheet.TableName);
sb.AppendLine();
foreach (FieldDef field in fieldList)
{
sb.AppendFormat("\tpublic {0} {1}; // {2}", field.type, field.name, field.comment);
sb.AppendLine();
}
sb.Append('}');
sb.AppendLine();
sb.AppendLine();
return sb.ToString();
}
public void SaveToFile(string filePath, Encoding encoding)
{
//-- 保存文件
using (FileStream file = new FileStream(filePath, FileMode.Create, FileAccess.Write))
{
using (TextWriter writer = new StreamWriter(file, encoding))
writer.Write(mCode);
}
}
}
}