forked from neil3d/excel2json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQLExporter.cs
121 lines (106 loc) · 4.07 KB
/
SQLExporter.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
113
114
115
116
117
118
119
120
121
using System;
using System.IO;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Newtonsoft.Json;
namespace excel2json
{
class SQLExporter
{
DataTable m_sheet;
int m_headerRows;
/// <summary>
/// 初始化内部数据
/// </summary>
/// <param name="sheet">Excel读取的一个表单</param>
/// <param name="headerRows">表头有几行</param>
public SQLExporter(DataTable sheet, int headerRows)
{
m_sheet = sheet;
m_headerRows = headerRows;
}
/// <summary>
/// 转换成SQL字符串,并保存到指定的文件
/// </summary>
/// <param name="filePath">存盘文件</param>
/// <param name="encoding">编码格式</param>
public void SaveToFile(string filePath, Encoding encoding)
{
//-- 转换成SQL语句
string tableName = Path.GetFileNameWithoutExtension(filePath);
string tabelStruct = GetTabelStructSQL(m_sheet, tableName);
string tabelContent = GetTableContentSQL(m_sheet, tableName);
//-- 保存文件
using (FileStream file = new FileStream(filePath, FileMode.Create, FileAccess.Write))
{
using (TextWriter writer = new StreamWriter(file, encoding))
{
writer.Write(tabelStruct);
writer.WriteLine();
writer.Write(tabelContent);
}
}
}
/// <summary>
/// 将表单内容转换成INSERT语句
/// </summary>
private string GetTableContentSQL(DataTable sheet, string tabelName)
{
StringBuilder sbContent = new StringBuilder();
StringBuilder sbNames = new StringBuilder();
StringBuilder sbValues = new StringBuilder();
//-- 字段名称列表
foreach (DataColumn column in sheet.Columns)
{
sbNames.Append(column.ToString());
sbNames.Append(", ");
}
//-- 逐行转换数据
int firstDataRow = m_headerRows - 1;
for (int i = firstDataRow; i < sheet.Rows.Count; i++ )
{
DataRow row = sheet.Rows[i];
sbValues.Clear();
foreach (DataColumn column in sheet.Columns)
{
if (sbValues.Length > 0)
sbValues.Append(", ");
sbValues.AppendFormat("'{0}'", row[column].ToString());
}
#if false
sbContent.AppendFormat("INSERT INTO `{0}`({1}) VALUES({2});\n",
tabelName, sbNames.ToString(), sbValues.ToString());
#else
sbContent.AppendFormat("INSERT INTO `{0}` VALUES({1});\n",
tabelName, sbValues.ToString());
#endif
}
return sbContent.ToString();
}
/// <summary>
/// 根据表头构造CREATE TABLE语句
/// </summary>
private string GetTabelStructSQL(DataTable sheet, string tabelName)
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat("DROP TABLE IF EXISTS `{0}`;\n", tabelName);
sb.AppendFormat("CREATE TABLE `{0}` (\n", tabelName);
DataRow typeRow = sheet.Rows[0];
foreach (DataColumn column in sheet.Columns)
{
string filedName = column.ToString();
string filedType = typeRow[column].ToString();
if (filedType == "varchar")
sb.AppendFormat("`{0}` {1}(64),", filedName, filedType);
else if (filedType == "text")
sb.AppendFormat("`{0}` {1}(256),", filedName, filedType);
else
sb.AppendFormat("`{0}` {1},", filedName, filedType);
}
sb.AppendFormat("PRIMARY KEY (`{0}`) ", sheet.Columns[0].ToString());
sb.AppendLine("\n) DEFAULT CHARSET=utf8;");
return sb.ToString();
}
}
}