-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
191 lines (168 loc) · 7.2 KB
/
Program.cs
File metadata and controls
191 lines (168 loc) · 7.2 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.IO;
namespace LanguageXlsxToLua
{
internal class Program
{
static void Main(string[] args)
{
PrepareFolder();
List<XlsxFileInfo> xlsxFiles = GetXlsxFileInfo();
foreach (XlsxFileInfo xlsxFile in xlsxFiles)
{
Console.WriteLine("Start processing file: " + xlsxFile.Name);
DataTable dt = GetExcelToDataTableBySheet(xlsxFile.Path, "Language$");
string[] languages = GetLanguages(dt);
Dictionary<string, Field[]> languageData = GetLanguageData(languages, dt);
Dictionary<string, string> languageLuaContent = new Dictionary<string, string>();
foreach (KeyValuePair<string, Field[]> pair in languageData)
{
languageLuaContent[pair.Key] = LuaHelper.GetLuaStringFromFields(pair.Value);
}
CheckAndCreateLanguageFolder(languages);
WriteLuaFiles(xlsxFile, languageLuaContent);
}
Console.WriteLine("Finished!");
Console.ReadLine();
}
static void PrepareFolder()
{
Console.WriteLine("Checking for input folder at path: " + AppValues.XlsxPath);
try
{
if (!Directory.Exists(AppValues.XlsxPath))
{
throw new Exception("No input folder at path: " + AppValues.XlsxPath);
}
}
catch (Exception e)
{
Console.WriteLine("!!! Error: " + e.Message);
throw;
}
Console.WriteLine("Preparing output path at: " + AppValues.OutputPath);
if (Directory.Exists(AppValues.OutputPath))
{
Directory.Delete(AppValues.OutputPath, true);
}
Directory.CreateDirectory(AppValues.OutputPath);
}
static List<XlsxFileInfo> GetXlsxFileInfo()
{
List<XlsxFileInfo> xlsxFiles = new List<XlsxFileInfo>();
DirectoryInfo df = new DirectoryInfo(AppValues.XlsxPath);
FileInfo[] fileInfo = df.GetFiles();
foreach (FileInfo file in fileInfo)
{
if (!file.Name.Contains(AppValues.Xlsx)) continue;
string fileName = file.Name;
int suffixSeperator = fileName.LastIndexOf('.');
string nameWOSuffix = fileName.Substring(0, suffixSeperator);
int prefixSeperator = fileName.IndexOf('_');
string prefix = fileName.Substring(0, prefixSeperator);
string module = fileName.Substring(prefixSeperator + 1, suffixSeperator - prefixSeperator - 1);
XlsxFileInfo info = new XlsxFileInfo();
{
info.Name = file.Name;
info.Path = file.FullName;
info.NameWOSuffix = nameWOSuffix;
info.Prefix = prefix;
info.NameWOPrefix = module;
}
xlsxFiles.Add(info);
}
return xlsxFiles;
}
static DataTable GetExcelToDataTableBySheet(string FileFullPath, string SheetName)
{
//此连接可以操作.xls与.xlsx文件
string strConn = "Provider=Microsoft.Ace.OleDb.12.0;" + "data source=" + FileFullPath + ";Extended Properties='Excel 12.0; HDR=NO; IMEX=1'";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
DataSet ds = new DataSet();
OleDbDataAdapter odda = new OleDbDataAdapter(string.Format("SELECT * FROM [{0}]", SheetName), conn);
//OleDbDataAdapter odda = new OleDbDataAdapter(string.Format("select * from [Sheet1$]", conn),conn);
odda.Fill(ds, SheetName);
conn.Close();
return ds.Tables[0];
}
static string[] GetLanguages(DataTable dt)
{
int colCount = AppValues.ColumnCount;
string[] languages = new string[colCount];
for (int col = 1; col <= colCount; col++) //[0][0] is meaningless, so count col at 1
{
string lang_short = dt.Rows[0][col].ToString();
languages[col - 1] = AppValues.Languages[lang_short];
}
return languages;
}
/// <summary>
/// Classify fields by languages
/// </summary>
/// <returns> Dictionary[key: language;Field[]: text field array] </returns>
static Dictionary<string, Field[]> GetLanguageData(string[] languages, DataTable dt)
{
int rowCount = dt.Rows.Count;
Dictionary<string, Field[]> data = new Dictionary<string, Field[]>();
foreach (string language in languages)
{
data[language] = new Field[rowCount - 1];
}
DataRowCollection sheet = dt.Rows;
for (int row = 1; row < rowCount; row++)
{
string fieldName = sheet[row][0].ToString();
for (int col = 1; col <= AppValues.ColumnCount; col++)
{
string language = languages[col - 1];
string value = sheet[row][col].ToString();
value = value.Replace("\n", "\\n");
if (!String.Equals(language, "Chinese")) //对非中文语言中的全角双引号替换成半角双引号
{
value = value.Replace('“', '"');
value = value.Replace('”', '"');
}
value = "'" + value + "'";
data[language][row - 1] = new Field()
{
Name = fieldName,
Value = value,
};
}
}
return data;
}
static void CheckAndCreateLanguageFolder(string[] languages)
{
foreach (string language in languages)
{
string langPath = AppValues.OutputPath + language;
if (!Directory.Exists(langPath))
{
Directory.CreateDirectory(langPath);
}
}
}
static void WriteLuaFiles(XlsxFileInfo info, Dictionary<string, string> languageLuaContent)
{
Console.WriteLine("Start writing lua files...");
foreach (KeyValuePair<string, string> pairs in languageLuaContent)
{
string language = pairs.Key;
Console.WriteLine("Writing " + language + " lua file...");
string langPath =
AppValues.OutputPath + language + "/" + info.Prefix + language + "_" + info.NameWOPrefix + AppValues.Lua;
using (StreamWriter sw = new StreamWriter(langPath))
{
sw.Write(pairs.Value);
sw.Flush();
sw.Close();
}
}
}
}
}