// 替换一列的参照信息 private static bool ReplaceColumn(DataTable aDataTable, int aColIndex, List <ColumnInfo> fieldTypeAryo) { ColumnInfo colInfo = fieldTypeAryo[aColIndex]; if (colInfo.m_isRef) { Regex reg = new Regex(@"^\w+\s*\{(\s*\w+\.\w+\s*)?\}$"); if (reg.IsMatch(colInfo.m_param)) { string tmp = string.Format("{0}[\"{1}\"]", aDataTable.TableName, colInfo.m_name); int startIndex = colInfo.m_param.IndexOf('{'); int endIndex = colInfo.m_param.IndexOf('}'); string param = colInfo.m_param.Substring(startIndex + 1, endIndex - startIndex - 1).Trim(' '); if (ReferenceHandle.FindRefenrence(aDataTable, fieldTypeAryo, ref colInfo.m_name, ref colInfo.m_type, param, tmp, true) != ErrRefenrence.ErrRefenrenceNone) { return(false); } } else { UnityEngine.Debug.LogError(string.Format("表头参照格式错误,表{0}[\"{1}\"]列的表头:{2} 格式不正确!", aDataTable.TableName, colInfo.m_name, colInfo.m_param)); return(false); } } else if (colInfo.m_isComplex) { Regex regDivide = new Regex(@"\w+\s+\w+"); bool isDivide = regDivide.IsMatch(colInfo.m_param.Substring(colInfo.m_param.IndexOf('['))); colInfo.m_param = colInfo.m_param.Replace(" ", ""); Regex reg = new Regex(@"^\w+\{((\w+=\w+\.\w+,)*\w+=\w+\.\w+)?\}\[((\w+,)*\w+)?\]$"); if (!isDivide && reg.IsMatch(colInfo.m_param)) { string param = colInfo.m_param.Substring(colInfo.m_param.IndexOf('{')); if (!CheckComplexColumn(param, colInfo, aDataTable, fieldTypeAryo)) { return(false); } colInfo.m_type = typeof(string); } else { UnityEngine.Debug.LogError(string.Format("表头拆分格式错误,表{0}[\"{1}\"]列的表头:{2} 格式不正确!", aDataTable.TableName, colInfo.m_name, colInfo.m_param)); return(false); } } return(true); }
private static bool CheckComplexColumn(string value, ColumnInfo fInfo, DataTable dt, List <ColumnInfo> fieldTypeAryo) { Dictionary <string, string> dic = new Dictionary <string, string>(); int index = value.IndexOf('['); string str = value.Substring(index + 1, value.IndexOf(']') - index - 1); string[] keys = str.Split(','); for (int i = 0; i < keys.Length; ++i) { if (keys[i].Length != 0) { dic.Add(keys[i], null); } } str = value.Substring(1, value.IndexOf('}') - 1); string[] v = str.Split(','); string k = ""; for (int i = 0; i < v.Length; ++i) { str = v[i]; if (str.Length == 0) { continue; } k = str.Substring(0, str.IndexOf('=')); if (dic.ContainsKey(k)) { dic[k] = str.Substring(str.IndexOf('=') + 1); string tmp = string.Format("{0}[\"{1}\"]", dt.TableName, fInfo.m_name); if (ReferenceHandle.FindRefenrence(dt, fieldTypeAryo, ref fInfo.m_name, ref fInfo.m_type, dic[k], tmp, true) != ErrRefenrence.ErrRefenrenceNone) { return(false); } } else { UnityEngine.Debug.LogError(string.Format("表头拆分格式错误,表{0}[\"{1}\"]列参照名称:{2}不在拆分名称中!", dt.TableName, fInfo.m_name, k)); return(false); } } fInfo.m_splitDic = dic; return(true); }
public static DataTable LoadOneExcelEx(FileInfo fInfo, string declareDir, string dataDir) { string name = ""; try { // 拼接具体的Excel路径 string str = fInfo.DirectoryName + "/" + fInfo.Name; str = str.Replace('\\', '/'); // 一张表的数据 Dictionary <int, string[]> sheet = new Dictionary <int, string[]>(); sheet = ExcelFile.ReadExcel(str); if (sheet == null) { return(null); } string[] headerRow; // 获取表头标题行的数据 sheet.TryGetValue(sheetHeadRow, out headerRow); if (headerRow == null) { return(null); } //截取生成Excel的名字,用来作为CS的文件名,exsample AAA.xlsx->AAA name = fInfo.Name.Substring(0, fInfo.Name.IndexOf('.')); // 行数 int rowCount = sheet.Count; // 列数 int colCount = headerRow.Length; // 一张Excel表的数据 DataTable table = new DataTable(name); // 把一张表表头的数据放进来 for (int i = 0; i < colCount; i++) { string colName = i.ToString(); string col = headerRow[i].ToString(); if (col != null) { colName = col.ToString(); } DataColumn column = new DataColumn(colName); table.Columns.Add(column); } // 这里把表里面具体内容的每行数据写进数据结构,从第五行开始读取和存放数据 for (int i = sheetRow + 1; i < rowCount; i++) { DataRow dataRow = table.NewRow(); if (!sheet.ContainsKey(i)) { continue; } // 获取每行的数据 var row = sheet[i]; // 根据列数读取一行的数据保存下来 for (int j = 0; j < colCount; j++) { string value = string.Empty; if (row != null && row[j] != null) { value = row[j]; } dataRow[j] = value; } // 每行的数据添加进数据结构 table.Rows.Add(dataRow); } // 初始化表头信息 bool hasIndex = false; List <ColumnInfo> colInfoAry = new List <ColumnInfo>(); for (int i = colCount - 1; i >= 0; i--) { ColumnInfo cInfo = GetFieldInfo(i, sheet, colInfoAry, name); if (cInfo != null) { colInfoAry.Insert(0, cInfo); if (cInfo.m_isIndex) { hasIndex = true; } } else { table.Columns.RemoveAt(i); } } if (!hasIndex) { throw new Exception("没有找到被设置为1的索引列!"); } if (colInfoAry.Count > 0) { //TableChecker.CheckOut(table,colInfoAry); ReferenceHandle.ReplaceReference(table, colInfoAry); // 根据数据生成CS文件 CSharpWriter.WriteDataDeclareCSEx(declareDir, dataDir, table, colInfoAry); } sheet.Clear(); sheet = null; return(table); } catch (Exception ex) { UnityEngine.Debug.LogError(string.Format("加载{0}表异常: {1}", fInfo.Name, ex.ToString())); } return(null); }