Skip to content

Commit

Permalink
exclude sheet and column with prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
neil3d committed Jul 25, 2020
1 parent cdaa094 commit f1f808b
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 22 deletions.
15 changes: 12 additions & 3 deletions CSDefineGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public string code {
}
}

public CSDefineGenerator(string excelName, ExcelLoader excel)
public CSDefineGenerator(string excelName, ExcelLoader excel, string excludePrefix)
{
//-- 创建代码字符串
StringBuilder sb = new StringBuilder();
Expand All @@ -44,7 +44,7 @@ public CSDefineGenerator(string excelName, ExcelLoader excel)
for (int i = 0; i < excel.Sheets.Count; i++)
{
DataTable sheet = excel.Sheets[i];
sb.Append(_exportSheet(sheet));
sb.Append(_exportSheet(sheet, excludePrefix));
}

sb.AppendLine();
Expand All @@ -53,18 +53,27 @@ public CSDefineGenerator(string excelName, ExcelLoader excel)
mCode = sb.ToString();
}

private string _exportSheet(DataTable sheet)
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();
Expand Down
Binary file modified Docs/ExampleData.xlsx
Binary file not shown.
4 changes: 2 additions & 2 deletions GUI/DataManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ public void loadExcel(Program.Options options)
ExcelLoader excel = new ExcelLoader(excelPath, header);

//-- C# 结构体定义
mCSharp = new CSDefineGenerator(excelPath, excel);
mCSharp = new CSDefineGenerator(excelPath, excel, options.ExcludePrefix);

//-- 导出JSON
mJson = new JsonExporter(excel, options.Lowcase, options.ExportArray, options.DateFormat, options.ForceSheetName, header);
mJson = new JsonExporter(excel, options.Lowcase, options.ExportArray, options.DateFormat, options.ForceSheetName, header, options.ExcludePrefix);
}
}
}
26 changes: 24 additions & 2 deletions GUI/MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions GUI/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ private void loadExcelAsync(string path)
options.HeaderRows = int.Parse(this.comboBoxHeader.Text);
options.DateFormat = this.comboBoxDateFormat.Text;
options.ForceSheetName = this.comboBoxSheetName.SelectedIndex == 0;
options.ExcludePrefix = this.textBoxExculdePrefix.Text;

//-- start import
this.backgroundWorker.RunWorkerAsync(options);
Expand Down
3 changes: 3 additions & 0 deletions GUI/MainForm.resx
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@
<metadata name="toolStrip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<metadata name="label7.GenerateMember" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>False</value>
</metadata>
<metadata name="backgroundWorker.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>241, 17</value>
</metadata>
Expand Down
32 changes: 21 additions & 11 deletions JsonExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,18 @@ public string context {
/// 构造函数:完成内部数据创建
/// </summary>
/// <param name="excel">ExcelLoader Object</param>
public JsonExporter(ExcelLoader excel, bool lowcase, bool exportArray, string dateFormat, bool forceSheetName, int headerRows)
public JsonExporter(ExcelLoader excel, bool lowcase, bool exportArray, string dateFormat, bool forceSheetName, int headerRows, string excludePrefix)
{
mHeaderRows = headerRows-1;
List<DataTable> validSheets = new List<DataTable>();
for (int i = 0; i < excel.Sheets.Count; i++)
{
DataTable sheet = excel.Sheets[i];

// 过滤掉包含特定前缀的表单
string sheetName = sheet.TableName;
if (excludePrefix.Length>0 && sheetName.StartsWith(excludePrefix))
continue;

if (sheet.Columns.Count > 0 && sheet.Rows.Count > 0)
validSheets.Add(sheet);
Expand All @@ -47,7 +52,7 @@ public JsonExporter(ExcelLoader excel, bool lowcase, bool exportArray, string da
{ // single sheet

//-- convert to object
object sheetValue = convertSheet(validSheets[0], exportArray, lowcase);
object sheetValue = convertSheet(validSheets[0], exportArray, lowcase, excludePrefix);

//-- convert to json string
mContext = JsonConvert.SerializeObject(sheetValue, jsonSettings);
Expand All @@ -58,7 +63,7 @@ public JsonExporter(ExcelLoader excel, bool lowcase, bool exportArray, string da
Dictionary<string, object> data = new Dictionary<string, object>();
foreach (var sheet in validSheets)
{
object sheetValue = convertSheet(sheet, exportArray, lowcase);
object sheetValue = convertSheet(sheet, exportArray, lowcase, excludePrefix);
data.Add(sheet.TableName, sheetValue);
}

Expand All @@ -67,15 +72,15 @@ public JsonExporter(ExcelLoader excel, bool lowcase, bool exportArray, string da
}
}

private object convertSheet(DataTable sheet, bool exportArray, bool lowcase)
private object convertSheet(DataTable sheet, bool exportArray, bool lowcase, string excludePrefix)
{
if (exportArray)
return convertSheetToArray(sheet, lowcase);
return convertSheetToArray(sheet, lowcase, excludePrefix);
else
return convertSheetToDict(sheet, lowcase);
return convertSheetToDict(sheet, lowcase, excludePrefix);
}

private object convertSheetToArray(DataTable sheet, bool lowcase)
private object convertSheetToArray(DataTable sheet, bool lowcase, string excludePrefix)
{
List<object> values = new List<object>();

Expand All @@ -85,7 +90,7 @@ private object convertSheetToArray(DataTable sheet, bool lowcase)
DataRow row = sheet.Rows[i];

values.Add(
convertRowToDict(sheet, row, lowcase, firstDataRow)
convertRowToDict(sheet, row, lowcase, firstDataRow, excludePrefix)
);
}

Expand All @@ -95,7 +100,7 @@ private object convertSheetToArray(DataTable sheet, bool lowcase)
/// <summary>
/// 以第一列为ID,转换成ID->Object的字典对象
/// </summary>
private object convertSheetToDict(DataTable sheet, bool lowcase)
private object convertSheetToDict(DataTable sheet, bool lowcase, string excludePrefix)
{
Dictionary<string, object> importData =
new Dictionary<string, object>();
Expand All @@ -108,7 +113,7 @@ private object convertSheetToDict(DataTable sheet, bool lowcase)
if (ID.Length <= 0)
ID = string.Format("row_{0}", i);

var rowObject = convertRowToDict(sheet, row, lowcase, firstDataRow);
var rowObject = convertRowToDict(sheet, row, lowcase, firstDataRow, excludePrefix);
// 多余的字段
// rowObject[ID] = ID;
importData[ID] = rowObject;
Expand All @@ -120,12 +125,17 @@ private object convertSheetToDict(DataTable sheet, bool lowcase)
/// <summary>
/// 把一行数据转换成一个对象,每一列是一个属性
/// </summary>
private Dictionary<string, object> convertRowToDict(DataTable sheet, DataRow row, bool lowcase, int firstDataRow)
private Dictionary<string, object> convertRowToDict(DataTable sheet, DataRow row, bool lowcase, int firstDataRow, string excludePrefix)
{
var rowData = new Dictionary<string, object>();
int col = 0;
foreach (DataColumn column in sheet.Columns)
{
// 过滤掉包含指定前缀的列
string columnName = column.ToString();
if (excludePrefix.Length > 0 && columnName.StartsWith(excludePrefix))
continue;

object value = row[column];

if (value.GetType() == typeof(System.DBNull))
Expand Down
6 changes: 6 additions & 0 deletions Program.Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ public bool ForceSheetName {
get;
set;
}

[Option('x', "exclude_prefix", Required = false, DefaultValue = "", HelpText = "exclude sheet or column start with specified prefix.")]
public string ExcludePrefix {
get;
set;
}
}
}
}
4 changes: 2 additions & 2 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,13 @@ private static void Run(Options options)
ExcelLoader excel = new ExcelLoader(excelPath, header);

//-- export
JsonExporter exporter = new JsonExporter(excel, options.Lowcase, options.ExportArray, dateFormat, options.ForceSheetName, header);
JsonExporter exporter = new JsonExporter(excel, options.Lowcase, options.ExportArray, dateFormat, options.ForceSheetName, header, options.ExcludePrefix);
exporter.SaveToFile(exportPath, cd);

//-- 生成C#定义文件
if (options.CSharpPath != null && options.CSharpPath.Length > 0)
{
CSDefineGenerator generator = new CSDefineGenerator(excelName, excel);
CSDefineGenerator generator = new CSDefineGenerator(excelName, excel, options.ExcludePrefix);
generator.SaveToFile(options.CSharpPath, cd);
}
}
Expand Down
4 changes: 2 additions & 2 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// 可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值,
// 方法是按如下所示使用“*”:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.3.0.0")]
[assembly: AssemblyFileVersion("1.3.0.0")]
[assembly: AssemblyVersion("1.4.0.0")]
[assembly: AssemblyFileVersion("1.4.0.0")]

0 comments on commit f1f808b

Please sign in to comment.