public static TableInfo AnalyzeTable(DataTable dt, string tableName, out string errorString) { TableInfo tableInfo = new TableInfo(); tableInfo.TableName = tableName; // 当前解析到的列号 int curColumnIndex = 0; // 解析第一列(主键列,要求数据类型为string或int且数据非空、唯一) DataType primaryKeyColumnType = _AnalyzeDataType(dt.Rows[AppValues.FIELD_DATA_TYPE_INDEX][0].ToString().Trim()); if (!(primaryKeyColumnType == DataType.Int || primaryKeyColumnType == DataType.String)) { errorString = _GetTableAnalyzeErrorString(tableName, 0) + "主键列的类型只能为int或string"; return(null); } else { // 解析出第一列,然后检查主键唯一性,如果是string型主键还要检查是否非空、是否符合变量名的规范(只能由英文字母、数字、下划线组成) FieldInfo primaryKeyField = _AnalyzeOneField(dt, tableName, 0, null, out curColumnIndex, out errorString); if (errorString != null) { errorString = _GetTableAnalyzeErrorString(tableName, 0) + "主键列解析错误\n" + errorString; return(null); } else { // 唯一性检查 FieldCheckRule uniqueCheckRule = new FieldCheckRule(); uniqueCheckRule.CheckType = TABLE_CHECK_TYPE.UNIQUE; uniqueCheckRule.CheckRuleString = "unique"; TableCheckHelper.CheckUnique(primaryKeyField, uniqueCheckRule, out errorString); if (errorString != null) { errorString = _GetTableAnalyzeErrorString(tableName, 0) + "主键列存在重复错误\n" + errorString; return(null); } // string型主键检查是否非空、是否符合变量名的规范 if (primaryKeyColumnType == DataType.String) { StringBuilder errorStringBuilder = new StringBuilder(); for (int row = 0; row < primaryKeyField.Data.Count; ++row) { string tempError = null; TableCheckHelper.CheckFieldName(primaryKeyField.Data[row].ToString(), out tempError); if (tempError != null) { errorStringBuilder.AppendFormat("第{0}行所填主键{1}\n", row + AppValues.FIELD_DATA_START_INDEX + 1, tempError); } } if (!string.IsNullOrEmpty(errorStringBuilder.ToString())) { errorString = _GetTableAnalyzeErrorString(tableName, 0) + "string型主键列存在非法数据\n" + errorStringBuilder.ToString(); } } tableInfo.AddField(primaryKeyField); } } // 存储定义过的字段名,不允许有同名字段(key:字段名, value:列号) Dictionary <string, int> fieldNames = new Dictionary <string, int>(); // 解析剩余的列 while (curColumnIndex < dt.Columns.Count) { int nextColumnIndex = curColumnIndex; FieldInfo oneField = _AnalyzeOneField(dt, tableName, nextColumnIndex, null, out curColumnIndex, out errorString); if (errorString != null) { errorString = _GetTableAnalyzeErrorString(tableName, nextColumnIndex) + errorString; return(null); } else { // 跳过未声明变量名的无效列 if (oneField != null) { // 检查字段名是否重复 if (fieldNames.ContainsKey(oneField.FieldName)) { errorString = _GetTableAnalyzeErrorString(tableName, nextColumnIndex) + string.Format("表格中存在字段名同为{0}的字段,分别位于第{1}列和第{2}列", oneField.FieldName, Utils.GetExcelColumnName(fieldNames[oneField.FieldName] + 1), Utils.GetExcelColumnName(oneField.ColumnSeq + 1)); return(null); } else { tableInfo.AddField(oneField); fieldNames.Add(oneField.FieldName, oneField.ColumnSeq); } } } } errorString = null; return(tableInfo); }
public static TableInfo AnalyzeTable(DataTable dt, string filePath, out string errorString) { if (dt.Rows.Count < ExcelTableSetting.DataFieldDataStartRowIndex) { errorString = "表格格式不符合要求,必须在表格前五行中依次声明字段描述、字段名、数据类型、检查规则、导出到MySQL数据库中的配置"; return(null); } if (dt.Columns.Count < 2) { errorString = "表格中至少需要配置2个字段"; return(null); } TableInfo tableInfo = new TableInfo(); tableInfo.ExcelFilePath = filePath; tableInfo.ExcelName = Path.GetFileNameWithoutExtension(filePath); tableInfo.TableName = ExcelMethods.GetTableName(tableInfo.ExcelName, "-", ExcelFolder.TheLanguage); string tableName = tableInfo.ExcelName; string temps = Path.GetDirectoryName(filePath); int tempi = temps.LastIndexOf(@"\"); string temps2 = temps.Substring(tempi, temps.Length - tempi); tableInfo.ExcelDirectory = temps2.Substring(1); // 当前解析到的列号 int curColumnIndex = 0; // 解析第一列(主键列,要求数据类型为int、long或string且数据非空、唯一) DataType primaryKeyColumnType = _AnalyzeDataType(dt.Rows[ExcelTableSetting.DataFieldDataTypeRowIndex][0].ToString().Trim()); if (!(primaryKeyColumnType == DataType.Int || primaryKeyColumnType == DataType.Long || primaryKeyColumnType == DataType.String)) { errorString = _GetTableAnalyzeErrorString(tableName, dt.TableName, 0) + "主键列的类型只能为int、long或string"; return(null); } else { // 解析出主键列,然后检查是否非空、唯一,如果是string型主键还要检查是否符合变量名的规范(只能由英文字母、数字、下划线组成) FieldInfo primaryKeyField = _AnalyzeOneField(dt, tableInfo, 0, null, out curColumnIndex, out errorString); if (errorString != null) { errorString = _GetTableAnalyzeErrorString(tableName, dt.TableName, 0) + "主键列解析错误\n" + errorString; return(null); } // 主键列字段名不允许为空 else if (primaryKeyField == null)//else if (primaryKeyField.IsIgnoreClientExport == true) { errorString = _GetTableAnalyzeErrorString(tableName, dt.TableName, 0) + "主键列必须指定字段名\n" + errorString; return(null); } else { // 唯一性检查 FieldCheckRule uniqueCheckRule = new FieldCheckRule(); uniqueCheckRule.CheckType = TableCheckType.Unique; uniqueCheckRule.CheckRuleString = "unique"; TableCheckHelper.CheckUnique(primaryKeyField, uniqueCheckRule, out errorString); if (errorString != null) { errorString = _GetTableAnalyzeErrorString(tableName, dt.TableName, 0) + "主键列存在重复错误\n" + errorString; return(null); } // string型主键检查是否符合变量名的规范 if (primaryKeyColumnType == DataType.String) { StringBuilder errorStringBuilder = new StringBuilder(); for (int row = 0; row < primaryKeyField.Data.Count; ++row) { string tempError = null; TableCheckHelper.CheckFieldName(primaryKeyField.Data[row].ToString(), out tempError); if (tempError != null) { errorStringBuilder.AppendFormat("第{0}行所填主键{1}\n", row + ExcelTableSetting.DataFieldDataStartRowIndex + 1, tempError); } } if (!string.IsNullOrEmpty(errorStringBuilder.ToString())) { errorString = _GetTableAnalyzeErrorString(tableName, dt.TableName, 0) + "string型主键列存在非法数据\n" + errorStringBuilder.ToString(); return(null); } } // 非空检查(因为string型检查是否符合变量名规范时以及未声明数值型字段中允许空时,均已对主键列进行过非空检查,这里只需对数据值字段且声明数值型字段中允许空的情况下进行非空检查) if (ExcelFolder.IsAllowedNullNumber == true && (primaryKeyColumnType == DataType.Int || primaryKeyColumnType == DataType.Long)) { FieldCheckRule notEmptyCheckRule = new FieldCheckRule(); uniqueCheckRule.CheckType = TableCheckType.NotEmpty; uniqueCheckRule.CheckRuleString = "notEmpty"; TableCheckHelper.CheckNotEmpty(primaryKeyField, notEmptyCheckRule, out errorString); if (errorString != null) { errorString = _GetTableAnalyzeErrorString(tableName, dt.TableName, 0) + "主键列存在非空错误\n" + errorString; return(null); } } tableInfo.AddField(primaryKeyField); } } // 存储定义过的字段名,不允许有同名字段assddds(key:字段名, value:列号) Dictionary <string, int> clientFieldNames = new Dictionary <string, int>(); //Dictionary<string, int> serverFieldNames = new Dictionary<string, int>(); // 先加入主键列 clientFieldNames.Add(tableInfo.GetKeyColumnFieldInfo().FieldName, 0); // if(tableInfo.GetKeyColumnFieldInfo().DatabaseFieldName!=null) // serverFieldNames.Add(tableInfo.GetKeyColumnFieldInfo().DatabaseFieldName, 0); // 解析剩余的列 while (curColumnIndex < dt.Columns.Count) { int nextColumnIndex = curColumnIndex; FieldInfo oneField = _AnalyzeOneField(dt, tableInfo, nextColumnIndex, null, out curColumnIndex, out errorString); if (errorString != null) { errorString = _GetTableAnalyzeErrorString(tableName, dt.TableName, nextColumnIndex) + errorString; return(null); } else { // 跳过未声明变量名以及数据库导出信息的无效列 if (oneField != null) { // 检查字段名是否重复 if (clientFieldNames.ContainsKey(oneField.FieldName)) { errorString = _GetTableAnalyzeErrorString(tableName, dt.TableName, nextColumnIndex) + string.Format("表格中存在字段名同为{0}的字段,分别位于第{1}列和第{2}列", oneField.FieldName, ExcelMethods.GetExcelColumnName(clientFieldNames[oneField.FieldName] + 1), ExcelMethods.GetExcelColumnName(oneField.ColumnSeq + 1)); return(null); } else { tableInfo.AddField(oneField); clientFieldNames.Add(oneField.FieldName, oneField.ColumnSeq); } // 检查字段名是否重复 //if (serverFieldNames.ContainsKey(oneField.DatabaseFieldName)) //{ // errorString = _GetTableAnalyzeErrorString(tableName, dt.TableName, nextColumnIndex) + string.Format("表格中存在字段名同为{0}的字段,分别位于第{1}列和第{2}列", oneField.DatabaseFieldName, ExcelMethods.GetExcelColumnName(serverFieldNames[oneField.DatabaseFieldName] + 1), ExcelMethods.GetExcelColumnName(oneField.ColumnSeq + 1)); // return null; //} //else // { // tableInfo.AddField(oneField); // serverFieldNames.Add(oneField.DatabaseFieldName, oneField.ColumnSeq); // } } } } errorString = null; return(tableInfo); }
public void LoadLibConfig(string file) { this.statusmessage = ""; tables.Clear(); infotables.Clear(); XmlReader xmlr = XmlReader.Create(file); TableInfo currentTable = null; string currentTag = ""; while (xmlr.Read()) { if (xmlr.NodeType == XmlNodeType.Element) { if (xmlr.IsStartElement()) { currentTag = xmlr.Name; switch (currentTag.ToLower()) { case "ini": tablecount = xmlr["TableCount"]; tableinfocount = xmlr["TableInfoCount"]; break; case "table": // Add table to table list currentTable = new TableInfo(); currentTable.Name = xmlr["name"]; currentTable.RowCount = xmlr["RowCount"]; currentTable.TableInfoID = xmlr["TableInfoID"]; currentTable.FieldCnt = xmlr["FieldCnt"]; infotables.Add(xmlr["TableInfoID"]); break; case "fieldinfo": currentTable.AddField(xmlr["Name"], xmlr["IsKey"], xmlr["DataType"]); break; case "row": currentTable.BeginRow(); break; default: break; } } } if (xmlr.NodeType == XmlNodeType.EndElement) { switch (xmlr.Name.ToLower()) { case "ini": break; case "table": if (!currentTable.UpdateMetadata()) { this.statusmessage = "Warning: Table \"" + currentTable.Name + "\" data might be malformed"; } tables.Add(currentTable); break; case "fieldinfo": break; case "row": currentTable.EndRow(); break; default: // Can only get value after tag concluded //currentTable.SetValue(lasttag, xmlr.Value); break; } } if (xmlr.NodeType == XmlNodeType.Text || xmlr.NodeType == XmlNodeType.CDATA) { currentTable.SetValue(currentTag, xmlr.Value); } } // Fix table count info if it does not match int matchTableCount = 0; int.TryParse(tablecount, out matchTableCount); if (matchTableCount != tables.Count) { this.statusmessage = "Warning: Number of tables do not match libconfig info"; tablecount = tables.Count.ToString(); } }
// catch(Exception e) // { // AppLog.LogErrorAndExit(string.Format("合并表格失败,因为{0}",e)); // } // return newTableInfo; //} /// <summary> /// 合并表格 /// </summary> /// <param name="newTableName">新表的名字,如:item</param> /// <param name="tableInfoList">待合并的表,TableInfo </param> /// <param name="errorString">错误输出</param> /// <returns></returns> public static TableInfo MergeTable(string newTableName, List <TableInfo> tableInfoList, out string errorString) { AppLog.Log(string.Format("开始合并表格:{0}", newTableName)); errorString = null; TableInfo newTableInfo = new TableInfo(); TableInfo oldTableInfo = tableInfoList[0]; //新表的基本信息 newTableInfo.ExcelFilePath = oldTableInfo.ExcelFilePath; newTableInfo.ExcelName = oldTableInfo.ExcelName; newTableInfo.TableName = newTableName; newTableInfo.ExcelDirectory = oldTableInfo.ExcelDirectory; newTableInfo.TableConfigData = oldTableInfo.TableConfigData; newTableInfo.TableConfigData2 = oldTableInfo.TableConfigData2; newTableInfo.TableConfig = oldTableInfo.TableConfig; FieldInfo newPrimaryKeyField = new FieldInfo(); FieldInfo oldPrimaryKeyField = oldTableInfo.GetKeyColumnFieldInfo(); newPrimaryKeyField.TableName = oldPrimaryKeyField.TableName; newPrimaryKeyField.SheetName = oldPrimaryKeyField.SheetName; //Excel表第1行信息 newPrimaryKeyField.Desc = oldPrimaryKeyField.Desc; //Excel表第2行信息 newPrimaryKeyField.FieldName = oldPrimaryKeyField.FieldName; //Excel表第3行信息 newPrimaryKeyField.DataType = oldPrimaryKeyField.DataType; newPrimaryKeyField.DataTypeString = oldPrimaryKeyField.DataTypeString; newPrimaryKeyField.ExtraParam = oldPrimaryKeyField.ExtraParam;//类似:date(input=#1970sec|toLua=yyyy年MM月dd日 HH时mm分ss秒) //Excel表第4行信息 newPrimaryKeyField.CheckRule = oldPrimaryKeyField.CheckRule; //Excel表第5行信息 newPrimaryKeyField.DatabaseFieldName = oldPrimaryKeyField.DatabaseFieldName; newPrimaryKeyField.DatabaseFieldType = oldPrimaryKeyField.DatabaseFieldType; newPrimaryKeyField.DatabaseInfoString = oldPrimaryKeyField.DatabaseInfoString; //合并主键及数据 StringBuilder tempMergeTable1 = new StringBuilder(); foreach (TableInfo tableInfoTemp in tableInfoList) { FieldInfo fieldInfoTemp = tableInfoTemp.GetKeyColumnFieldInfo(); if (newPrimaryKeyField.FieldName.Equals(fieldInfoTemp.FieldName) == false) { AppLog.LogErrorAndExit(string.Format("合并遇到错误:合并表格的主键名必须一致,表{0}的主键名为{1}与其他表格主键为{2}不同,其他表格包括:{3}", tableInfoTemp.ExcelName, fieldInfoTemp.FieldName, newPrimaryKeyField.FieldName, tempMergeTable1.ToString())); } if (newPrimaryKeyField.DataType.Equals(fieldInfoTemp.DataType) == false) { AppLog.LogErrorAndExit(string.Format("合并遇到错误:合并表格的主键名及类型必须一致,表{0}的主键名为{1}(类型为:{2})与其他表格主键类型为{3},其他表格包括:{4}", tableInfoTemp.ExcelName, fieldInfoTemp.FieldName, fieldInfoTemp.DataType, newPrimaryKeyField.DataType, tempMergeTable1.ToString())); } //if (newPrimaryKeyField.DatabaseInfoString.Equals(fieldInfoTemp.DatabaseInfoString) == false) //{ // AppLog.LogErrorAndExit(string.Format("合并遇到错误:合并表格的主键导入数据库(Excel第{0}行)必须一致:表{1}的导入数据库{2}与其他表格主键不同,其他表格包括:{3}", ExcelTableSetting.DataFieldExportDataBaseFieldInFoRowIndex, tableInfoTemp.ExcelName, fieldInfoTemp.DatabaseInfoString, tempMergeTable1.ToString())); //} newPrimaryKeyField = FieldInfo.AddDataByOtherField(newPrimaryKeyField, fieldInfoTemp, fieldInfoTemp.Data.Count, 0, out errorString); if (errorString != null) { AppLog.LogErrorAndExit("合并遇到错误:" + errorString); } } // 唯一性检查 FieldCheckRule uniqueCheckRule = new FieldCheckRule(); uniqueCheckRule.CheckType = TableCheckType.Unique; uniqueCheckRule.CheckRuleString = "unique"; TableCheckHelper.CheckUnique(newPrimaryKeyField, uniqueCheckRule, out errorString); if (errorString != null) { AppLog.LogErrorAndExit(string.Format("合并后名为{0}的表格主键存在重复值:{1}", newTableName, errorString)); } newTableInfo.AddField(newPrimaryKeyField); // 合并其他字段及字段类型,但不合并数据 List <string> newTableFieldNames = new List <string>(); newTableFieldNames.Add(newPrimaryKeyField.FieldName); StringBuilder tempMergeTable2 = new StringBuilder(); foreach (TableInfo tableInfoTemp in tableInfoList) { List <FieldInfo> listFieldInfoTemp = tableInfoTemp.GetAllFieldInfo(); foreach (FieldInfo tempFieldInfo in listFieldInfoTemp) { //如果是主键就跳过 if (tempFieldInfo.FieldName.Equals(newPrimaryKeyField.FieldName)) { continue; } //如果已合并过该字段,则进行检查字段类型、数据库类型是否相同 if (newTableFieldNames.Contains(tempFieldInfo.FieldName)) { FieldInfo tempNewTableInfoFieldInfo = newTableInfo.GetFieldInfoByFieldName(tempFieldInfo.FieldName); if (tempFieldInfo.DataType.Equals(tempNewTableInfoFieldInfo.DataType) == false) { AppLog.LogErrorAndExit(string.Format("合并遇到错误:合并表格的字段名与类型必须一致,表{0}中为{1}的字段(类型为:{2})与其他表格同名字段类型为{3},其他表格包括:{4}", tableInfoTemp.ExcelName, tempFieldInfo.FieldName, tempFieldInfo.DataType, tempNewTableInfoFieldInfo.DataType, tempMergeTable2.ToString())); } //if (tempFieldInfo.DatabaseInfoString.Equals(tempNewTableInfoFieldInfo.DatabaseInfoString) == false) //{ // AppLog.LogErrorAndExit(string.Format("合并遇到错误:合并表格的字段名与类型必须一致,表{0}中为{1}的字段(导出入数据类型为:{2})与其他表格同名字段导入数据库类型为{3},其他表格包括:{4}", tableInfoTemp.ExcelName, tempFieldInfo.FieldName, tempFieldInfo.DatabaseInfoString, tempNewTableInfoFieldInfo.DatabaseInfoString, tempMergeTable2.ToString())); //} if (tempNewTableInfoFieldInfo.ChildField == null) { if (tempFieldInfo.ChildField != null) { tempNewTableInfoFieldInfo.ChildField = tempFieldInfo.ChildField; } } else { if (tempNewTableInfoFieldInfo.ChildField.Count == tempFieldInfo.ChildField.Count) { //继续判断其他条件 } else { AppLog.LogErrorAndExit(string.Format("合并遇到错误:表{0}中为{1}的字段的ChildField数量与其他表格不同,其他表格包括:{2}", tableInfoTemp.ExcelName, tempFieldInfo.FieldName, tempMergeTable2.ToString())); } } } else//如果未合并,则直接合并 { FieldInfo newField = new FieldInfo(); //Excel表第1行信息 newField.Desc = tempFieldInfo.Desc; //Excel表第2行信息 newField.FieldName = tempFieldInfo.FieldName; //Excel表第3行信息 newField.DataType = tempFieldInfo.DataType; newField.DataTypeString = tempFieldInfo.DataTypeString; newField.ExtraParam = tempFieldInfo.ExtraParam; //类似:date(input=#1970sec|toLua=yyyy年MM月dd日 HH时mm分ss秒) //Excel表第4行信息 newField.CheckRule = tempFieldInfo.CheckRule; //Excel表第5行信息 newField.DatabaseFieldName = tempFieldInfo.DatabaseFieldName; newField.DatabaseFieldType = tempFieldInfo.DatabaseFieldType; newField.DatabaseInfoString = tempFieldInfo.DatabaseInfoString; newField.ChildField = tempFieldInfo.ChildField; newField.ChildFieldString = tempFieldInfo.ChildFieldString; newTableFieldNames.Add(newField.FieldName); newTableInfo.AddField(newField); } } tempMergeTable2.Append(tableInfoTemp.ExcelName); } List <FieldInfo> listFieldInfoTemp2 = newTableInfo.GetAllFieldInfo(); //移除各字段中ChildField字段的数据 foreach (FieldInfo tempFieldInfo in listFieldInfoTemp2) { if (tempFieldInfo.ChildField == null) { continue; } foreach (FieldInfo ChildFieldInfo1 in tempFieldInfo.ChildField) { if (ChildFieldInfo1.ChildField == null) { ChildFieldInfo1.Data = null; continue; } else { foreach (FieldInfo ChildFieldInfo2 in tempFieldInfo.ChildField) { if (ChildFieldInfo2.ChildField == null) { ChildFieldInfo2.Data = null; continue; } else { foreach (FieldInfo ChildFieldInfo3 in tempFieldInfo.ChildField) { if (ChildFieldInfo3.ChildField == null) { ChildFieldInfo3.Data = null; continue; } else { foreach (FieldInfo ChildFieldInfo4 in tempFieldInfo.ChildField) { if (ChildFieldInfo4.ChildField == null) { ChildFieldInfo4.Data = null; continue; } else { foreach (FieldInfo ChildFieldInfo5 in tempFieldInfo.ChildField) { if (ChildFieldInfo5.ChildField == null) { ChildFieldInfo5.Data = null; continue; } else { //暂时只支持5层ChildField,后续再考虑递归 } } } } } } } } } } } //合并其他字段数据,遍历新表所有字段 foreach (FieldInfo tempFieldInfo in listFieldInfoTemp2) { FieldInfo fieldA = tempFieldInfo; //如果是主键就跳过 if (fieldA.FieldName.Equals(newPrimaryKeyField.FieldName)) { continue; } FieldInfo fieldB; FieldInfo FieldReturn = new FieldInfo(); foreach (TableInfo tableInfoTemp in tableInfoList) //遍历所有待合并表 { if (tableInfoTemp.IsContainField(fieldA.FieldName)) //如果存在字段就获取到该字段 { fieldB = tableInfoTemp.GetFieldInfoByFieldName(fieldA.FieldName); } else { fieldB = new FieldInfo(); fieldB.Data = null; } fieldA = FieldInfo.AddDataByOtherField(fieldA, fieldB, tableInfoTemp.GetKeyColumnFieldInfo().Data.Count, 0, out errorString); } //newTableInfo.AddField(fieldA); } return(newTableInfo); }
/// <summary> /// 指定TableInfo与当前TableInfo合并 /// </summary> /// <param name="tableInfoList"></param> public static TableInfo Merge(string newTableName, List <TableInfo> tableInfoList, out string errorString) { errorString = null; StringBuilder stringBuilder = new StringBuilder(); //foreach(TableInfo t in tableInfoList) //{ // stringBuilder.AppendLine(t.ExcelName); //} AppLog.Log(string.Format("\n开始合并表格{0}", newTableName)); // AppLog.Log(string.Format("{0}",stringBuilder.ToString()), ConsoleColor.Green); TableInfo tableInfoFirst = new TableInfo(); tableInfoFirst.ExcelFilePath = tableInfoList[0].ExcelFilePath; tableInfoFirst.ExcelName = tableInfoList[0].ExcelName; tableInfoFirst.TableName = newTableName; tableInfoFirst.ExcelDirectory = tableInfoList[0].ExcelDirectory; tableInfoFirst.TableConfigData = tableInfoList[0].TableConfigData; tableInfoFirst.TableConfigData2 = tableInfoList[0].TableConfigData2; tableInfoFirst.TableConfig = tableInfoList[0].TableConfig; FieldInfo fieldInfoTemp; List <FieldInfo> allFieldInfo = null; List <FieldInfo> allFieldInfoA = null; List <string> fieldName = new List <string>(); //逐个表合并 foreach (TableInfo tableInfo in tableInfoList) { FieldInfo tableInfoKeyField = tableInfo.GetKeyColumnFieldInfo(); FieldInfo tableInfoFirstKeyField = tableInfoFirst.GetKeyColumnFieldInfo(); //检查主键是否相同 if (tableInfoFirstKeyField != null) { if (!(string.Equals(tableInfoFirstKeyField.FieldName, tableInfoKeyField.FieldName) && string.Equals(tableInfoFirstKeyField.DataType, tableInfoKeyField.DataType) && string.Equals(tableInfoFirstKeyField.DatabaseFieldName, tableInfoKeyField.DatabaseFieldName) && string.Equals(tableInfoFirstKeyField.DatabaseFieldType, tableInfoKeyField.DatabaseFieldType))) { AppLog.LogErrorAndExit(string.Format("合并遇到错误:合并表格的主键名必须一致,表{0}的主键与其他表格主键不同", tableInfo.ExcelName)); } } //合并字段信息 allFieldInfo = new List <FieldInfo>(); allFieldInfo = tableInfo.GetAllFieldInfo(); foreach (FieldInfo fieldInfo in allFieldInfo) { if (!fieldName.Contains(fieldInfo.FieldName)) { fieldInfoTemp = new FieldInfo(); fieldInfoTemp.TableName = fieldInfo.TableName; fieldInfoTemp.SheetName = fieldInfo.SheetName; //Excel表第1行信息 fieldInfoTemp.Desc = fieldInfo.Desc; //Excel表第2行信息 fieldInfoTemp.FieldName = fieldInfo.FieldName; //Excel表第3行信息 fieldInfoTemp.DataType = fieldInfo.DataType; fieldInfoTemp.DataTypeString = fieldInfo.DataTypeString; fieldInfoTemp.ExtraParam = fieldInfo.ExtraParam; //类似:date(input=#1970sec|toLua=yyyy年MM月dd日 HH时mm分ss秒) //Excel表第4行信息 fieldInfoTemp.CheckRule = fieldInfo.CheckRule; //Excel表第5行信息 fieldInfoTemp.DatabaseFieldName = fieldInfo.DatabaseFieldName; fieldInfoTemp.DatabaseFieldType = fieldInfo.DatabaseFieldType; //其他信息 fieldInfoTemp.ArrayChildDataType = fieldInfo.ArrayChildDataType; fieldInfoTemp.ArrayChildDataTypeString = fieldInfo.ArrayChildDataTypeString; fieldInfoTemp.ColumnSeq = fieldInfo.ColumnSeq; fieldInfoTemp.IsIgnoreClientExport = fieldInfo.IsIgnoreClientExport; fieldInfoTemp.TableStringFormatDefine = fieldInfo.TableStringFormatDefine; fieldInfoTemp.MapStringFormatDefine = fieldInfo.MapStringFormatDefine; fieldInfoTemp.ChildField = fieldInfo.ChildField; fieldInfoTemp.ParentField = fieldInfo.ParentField; tableInfoFirst.AddField(fieldInfoTemp); fieldName.Add(fieldInfo.FieldName); } else { FieldInfo tableInfoFirstField = tableInfoFirst.GetFieldInfoByFieldName(fieldInfo.FieldName); if (!(String.Equals(tableInfoFirstField.FieldName, fieldInfo.FieldName) && String.Equals(tableInfoFirstField.DataType, fieldInfo.DataType) && String.Equals(tableInfoFirstField.DatabaseFieldName, fieldInfo.DatabaseFieldName) && String.Equals(tableInfoFirstField.DatabaseFieldType, fieldInfo.DatabaseFieldType))) { AppLog.LogErrorAndExit(string.Format("合并遇到错误:合并表格的字段必须一致,表{0}的字段{1}与其他表格不同", tableInfo.ExcelName, fieldInfo.FieldName)); } } } } List <FieldInfo> allFieldInfo2 = new List <FieldInfo>(); allFieldInfo2 = tableInfoFirst.GetAllFieldInfo(); List <string> fieldNameB = new List <string>(); //所有字段先赋值 foreach (FieldInfo fieldInfo in allFieldInfo2) { if (tableInfoFirst.GetFieldInfoByFieldName(fieldInfo.FieldName).Data == null) { tableInfoFirst.GetFieldInfoByFieldName(fieldInfo.FieldName).Data = new List <object>(); } if (tableInfoFirst.GetFieldInfoByFieldName(fieldInfo.FieldName).LangKeys == null) { tableInfoFirst.GetFieldInfoByFieldName(fieldInfo.FieldName).LangKeys = new List <object>(); } if (tableInfoFirst.GetFieldInfoByFieldName(fieldInfo.FieldName).JsonString == null) { tableInfoFirst.GetFieldInfoByFieldName(fieldInfo.FieldName).JsonString = new List <string>(); } } //合并字段的值 //Dictionary<主键, 源自哪个Excel表> Dictionary <string, string> KeyColumn = new Dictionary <string, string>(); string KeyColumnName = tableInfoList[0].GetKeyColumnFieldInfo().FieldName; StringBuilder sbkey = new StringBuilder(); foreach (TableInfo tableInfo in tableInfoList) { AppLog.Log(string.Format("将表[{0}]合并到[{1}]", tableInfo.ExcelName, newTableName), ConsoleColor.Green); int count = tableInfo.GetAllFieldInfo()[0].Data.Count; if (count > 0) { allFieldInfoA = new List <FieldInfo>(); allFieldInfoA = tableInfo.GetAllFieldInfo(); for (int i = 0; i < count; ++i) { foreach (FieldInfo fieldInfo in allFieldInfo2) { if (tableInfo.GetFieldInfoByFieldName(fieldInfo.FieldName) != null) { object data = tableInfo.GetFieldInfoByFieldName(fieldInfo.FieldName).Data[i]; if (string.Equals(fieldInfo.FieldName, KeyColumnName)) { if (!KeyColumn.ContainsKey(data.ToString())) { KeyColumn.Add(data.ToString(), tableInfo.ExcelName); } else { sbkey.AppendLine(string.Format("错误:表{0}使用了已存在主键值{1}", tableInfo.ExcelName, data.ToString())); } // AppLog.LogErrorAndExit(string.Format("错误:表{0}使用了已存在主键值{1}", tableInfo.ExcelName, data.ToString())); } if (data != null) { tableInfoFirst.GetFieldInfoByFieldName(fieldInfo.FieldName).Data.Add(data); } else { tableInfoFirst.GetFieldInfoByFieldName(fieldInfo.FieldName).Data.Add(null); } //object langkeys = tableInfo.GetFieldInfoByFieldName(fieldInfo.FieldName).LangKeys[i]; if (tableInfo.GetFieldInfoByFieldName(fieldInfo.FieldName).LangKeys != null) { tableInfoFirst.GetFieldInfoByFieldName(fieldInfo.FieldName).LangKeys.Add(tableInfo.GetFieldInfoByFieldName(fieldInfo.FieldName).LangKeys[i]); } else { tableInfoFirst.GetFieldInfoByFieldName(fieldInfo.FieldName).LangKeys.Add(null); } // string jsonstring = tableInfo.GetFieldInfoByFieldName(fieldInfo.FieldName).JsonString[i]; if (tableInfo.GetFieldInfoByFieldName(fieldInfo.FieldName).JsonString != null) { tableInfoFirst.GetFieldInfoByFieldName(fieldInfo.FieldName).JsonString.Add(tableInfo.GetFieldInfoByFieldName(fieldInfo.FieldName).JsonString[i]); } else { tableInfoFirst.GetFieldInfoByFieldName(fieldInfo.FieldName).JsonString.Add(null); } } else { tableInfoFirst.GetFieldInfoByFieldName(fieldInfo.FieldName).Data.Add(null); tableInfoFirst.GetFieldInfoByFieldName(fieldInfo.FieldName).LangKeys.Add(null); tableInfoFirst.GetFieldInfoByFieldName(fieldInfo.FieldName).JsonString.Add(null); } } } } AppLog.Log("合并成完"); } if (sbkey.Length > 0) { AppLog.LogErrorAndExit(sbkey.ToString()); } // 唯一性检查 //FieldCheckRule uniqueCheckRule = new FieldCheckRule(); //uniqueCheckRule.CheckType = TableCheckType.Unique; //uniqueCheckRule.CheckRuleString = "unique"; //TableCheckHelper.CheckUnique(tableInfoFirst.GetKeyColumnFieldInfo(), uniqueCheckRule, out errorString); //if (errorString != null) //{ // //string error = string.Format("表格{0}-{1}中列号为{2}的字段存在以下严重错误,导致无法继续,请修正错误后重试\n", newTableName, "", ExcelMethods.GetExcelColumnName(0 + 1)); // // errorString = "主键列存在重复错误\n" + errorString; // AppLog.LogErrorAndExit(string.Format("错误:合并{0}失败,因为主键{1},{2}", newTableName, tableInfoFirst.GetKeyColumnFieldInfo().FieldName, errorString)); // //AppLog.LogErrorAndExit(string.Format("错误:存在多个以{0}为名的表格,且合并时发生错误失败\n{1}", tableName, errorString)); //} return(tableInfoFirst); }