From f1f808b23cf9b11f58f83dce05714f1dffba26d2 Mon Sep 17 00:00:00 2001 From: neil3d Date: Sat, 25 Jul 2020 16:44:15 +0800 Subject: [PATCH] exclude sheet and column with prefix --- CSDefineGenerator.cs | 15 ++++++++++++--- Docs/ExampleData.xlsx | Bin 10258 -> 11873 bytes GUI/DataManager.cs | 4 ++-- GUI/MainForm.Designer.cs | 26 ++++++++++++++++++++++++-- GUI/MainForm.cs | 1 + GUI/MainForm.resx | 3 +++ JsonExporter.cs | 32 +++++++++++++++++++++----------- Program.Options.cs | 6 ++++++ Program.cs | 4 ++-- Properties/AssemblyInfo.cs | 4 ++-- 10 files changed, 73 insertions(+), 22 deletions(-) diff --git a/CSDefineGenerator.cs b/CSDefineGenerator.cs index 3dfce14..cf290a9 100644 --- a/CSDefineGenerator.cs +++ b/CSDefineGenerator.cs @@ -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(); @@ -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(); @@ -53,11 +53,15 @@ 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 fieldList = new List(); DataRow typeRow = sheet.Rows[0]; @@ -65,6 +69,11 @@ private string _exportSheet(DataTable sheet) 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(); diff --git a/Docs/ExampleData.xlsx b/Docs/ExampleData.xlsx index 256066e75afbe92f204d6214a6c6ac7be1743a54..54333794c281677e7df6ea8d693050ca81401ed2 100644 GIT binary patch delta 5993 zcmZXYbzD@-+rXEUC6w5ul#~UeL%O@}f^>IxO2d*%=ju`-Atj9<-5`x1f;31sNQcCW z_jiBS_w}8B=FDf#d1lTtPt7;^#gYN(3nesk5C9W^1pokO0LAAzc|Irr00^^+lm-#xE*_xnco5z zQQLxvu692vD!DcH7lm0`lnf!q-RDGED|O%A$tS*=D0U;}szL>Y-tIx2n~#Ek+WJG% z3aeRp-OXWorrW|g3(pLSz=zhUiWNt&wDgeO+Sx~3UdVQ}7;@Y%2QA_`>tINQl93T} z8Yy$~#%TkbiD=;Gz-qKkk%7)lOx{{_=W4d0Xa$*|43)So3rbPB8Wk=#oB+Ry6XeEj z9fUpdbiQ7(feX89%)8lq=K*PYW>-1yODf@bljn3&;7!AcT|+kJ%e792Y zRUgX7&X()0^YML&B36~K^*xddb0bNerTx-yN>)~V{sklR z@(imczeI1A@q)iaR5+CSTkTk8b&-j9h?Fa=4%VgYel1;aN|zfA!GKR6WZ#aK6rnM$%n^X7D^nmJf?k{K@&m|sgjMQcHfLb zeDuKZ2sU^iHg4Q$^36*6*%K8Y`@Z?JUay&mH}htPIQQy7ND~sk+Ki;tKisQ?6Ab`( zbZ@-g4(wjeZuVx*&h~8Hjt;TO(1clPeE7lZD=K`y1(Rkckc_L=cCDtn?poshTOegG zqrXnj%Icz5meiVKNqDI*7n@Nw3mdO~_fgdo)YOMkJEyIpB*8 z(t0|XEQf^xXn#irFR_O#MIbU!9?L~OV5LP;kx#&*tX8dEWG~z&MHQ{}q@TRjI^-39 z#_84~q`UD+)qGa_){%DQ3~h#S%h2Ve0d;hoOyFjUbu8?-YHhDTi+rKSnjzQGI9!1( zosd=XTK7Gcra}s7BjtWn2;t(#MWOt|S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adBSJ>m4NohtIX9nyUnpc6CukiqK7-i8l zla0r3dE7%~3o8$Z!Wm6AmAvmWEbQ3=jm5u{|70*@s_9`vlFz~-EWB2}&U?@wS8Vk7 z)tt3->HU*2!mp^}ONHh%ULHKOfp$NVHg=8hT6yF_LEs!Vwv`}9R*I^QuRC_r8zJtd zQNDWB<<>bz5tos-60jelCp2P8WTGx_XFazA<3_1MT&IV%4*ZWy8xHYRCjtb+R$Ygu zAutPQo5U03T`@WvPscasq*k;VODKE)meco0rNYISk3tj_ECXJS&{OmW_mBbK!@&>* z=j@3@{7LT9!axO2H4bU^Du&Fh<#|~JQvBO|Q|~jmbcah8Vodhkl^gFCKL4&x^FA<1 z#z`BMP;s&Te0s6fYvox)N`^f9t*6@ySG9lrz1jdQ<-kOfkgZ6x4i6tu)0qWqOW5O< zNjLjZJ7(^NM}9=a!+bzVJLQ$;@5OR*IUdG=G6R8$m@rNFmq{0i@8vyYVzjev-+Bh_ zC+?EZv{Z%&SU0`<^*9tn-YzV!h=GvMVXk>ZFx5QwNFr^JvcDO?(_GETc4MM?-I;%b zKeoHS{6SqRvl7Z8_G1gzlsIU)9kFC?X^bFod$|~qYvWJ62@Zd|+%kpR*%NwKhfywT zdvX?UTjH!%_|^UCZ7R%RgB=c2=(OGxUxRP02OgUC6iAZ?8wnAlvFaDYTa zC4dao=!Br2$qdegyj`(qiz~oTk|8`4cir`Y)q` zLGsgLvJp)91r(SjgdTn&B?eD`9|IzzBLA~Pof^Z6;D-H?NdN%DpVgZYH6sSg$b_jD zpo0DHV*Kx@udtia%<~`aW7vhDkk_mLz 17, 17 + + False + 241, 17 diff --git a/JsonExporter.cs b/JsonExporter.cs index 35cdd15..9fb4ec3 100644 --- a/JsonExporter.cs +++ b/JsonExporter.cs @@ -25,13 +25,18 @@ public string context { /// 构造函数:完成内部数据创建 /// /// ExcelLoader Object - 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 validSheets = new List(); 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); @@ -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); @@ -58,7 +63,7 @@ public JsonExporter(ExcelLoader excel, bool lowcase, bool exportArray, string da Dictionary data = new Dictionary(); foreach (var sheet in validSheets) { - object sheetValue = convertSheet(sheet, exportArray, lowcase); + object sheetValue = convertSheet(sheet, exportArray, lowcase, excludePrefix); data.Add(sheet.TableName, sheetValue); } @@ -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 values = new List(); @@ -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) ); } @@ -95,7 +100,7 @@ private object convertSheetToArray(DataTable sheet, bool lowcase) /// /// 以第一列为ID,转换成ID->Object的字典对象 /// - private object convertSheetToDict(DataTable sheet, bool lowcase) + private object convertSheetToDict(DataTable sheet, bool lowcase, string excludePrefix) { Dictionary importData = new Dictionary(); @@ -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; @@ -120,12 +125,17 @@ private object convertSheetToDict(DataTable sheet, bool lowcase) /// /// 把一行数据转换成一个对象,每一列是一个属性 /// - private Dictionary convertRowToDict(DataTable sheet, DataRow row, bool lowcase, int firstDataRow) + private Dictionary convertRowToDict(DataTable sheet, DataRow row, bool lowcase, int firstDataRow, string excludePrefix) { var rowData = new Dictionary(); 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)) diff --git a/Program.Options.cs b/Program.Options.cs index 3cc4c44..2615a2a 100644 --- a/Program.Options.cs +++ b/Program.Options.cs @@ -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; + } } } } diff --git a/Program.cs b/Program.cs index 29b7803..86159a9 100644 --- a/Program.cs +++ b/Program.cs @@ -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); } } diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs index 04a2596..51dfab5 100644 --- a/Properties/AssemblyInfo.cs +++ b/Properties/AssemblyInfo.cs @@ -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")]