forked from neil3d/excel2json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCSDefineGenerator.cs
76 lines (64 loc) · 2.34 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
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, DataTable sheet) {
//-- First Row as Column Name
if (sheet.Rows.Count < 2)
return;
List<FieldDef> m_fieldList = new List<FieldDef>();
DataRow typeRow = sheet.Rows[0];
DataRow commentRow = sheet.Rows[1];
foreach (DataColumn column in sheet.Columns) {
FieldDef field;
field.name = column.ToString();
field.type = typeRow[column].ToString();
field.comment = commentRow[column].ToString();
m_fieldList.Add(field);
}
//-- 创建代码字符串
StringBuilder sb = new StringBuilder();
sb.AppendLine("//");
sb.AppendLine("// Auto Generated Code By excel2json");
sb.AppendLine("//");
sb.AppendLine();
sb.AppendFormat("// Generate From {0}.xlsx", excelName);
sb.AppendLine();
sb.AppendFormat("public class {0}\r\n{{", excelName);
sb.AppendLine();
foreach (FieldDef field in m_fieldList) {
sb.AppendFormat("\tpublic {0} {1}; // {2}", field.type, field.name, field.comment);
sb.AppendLine();
}
sb.Append('}');
sb.AppendLine();
sb.AppendLine("// End of Auto Generated Code");
mCode = 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);
}
}
}
}