Esempio n. 1
0
        /// <summary>
        /// 相当于根据表的内容,重新组装下表头,方便写CS文件的时候处理数据
        /// </summary>
        /// <param name="aColIndex"></param>
        /// <param name="aSheet"></param>
        /// <param name="aColInfoAry"></param>
        /// <param name="tableName"></param>
        /// <returns></returns>
        private static ColumnInfo GetFieldInfo(int aColIndex, Dictionary <int, string[]> aSheet, List <ColumnInfo> aColInfoAry, string tableName)
        {
            // 挨着取第一行单元格的数据
            string cellData = aSheet[sheetHeadRow][aColIndex];
            // 获得第一行每个单元格的参数名,相当于每列的名字
            string fieldName = string.Empty;

            if (cellData != null && cellData.ToString() != null)
            {
                fieldName = cellData.ToString().Replace("\n", " ").Trim(' ');
            }
            ColumnInfo colInfo = new ColumnInfo();

            if (!aSheet.ContainsKey(sheetDataTypeRow))
            {
                return(null);
            }
            // 取数据类型所在行的数据
            var    row      = aSheet[sheetDataTypeRow];
            string dataType = null;

            if (row != null)
            {
                dataType = row[aColIndex];
            }
            // 查找哪列是1的那列
            string keyType = aSheet[sheetKeyRow][aColIndex];
            // 这行目前有两个作用
            //1. 以前根据"[[]]"标签针对列做单独处理
            //2. 客户端可以跳过不需要处理的一列
            string commentType = aSheet[sheetRow][aColIndex];

            if (commentType != null)
            {
                string tempType = commentType.ToString();
                // 客户端可以跳过不需要处理的内容
                if (tempType.Contains("client ignore"))
                {
                    ignore = true;
                    return(null);
                }
            }
            // 分解一列的数据类型
            if (dataType == null)
            {
                colInfo.m_dataType = ColumnDataType.INT_DATA;
            }
            else if (dataType.ToString().Contains("varchar") || dataType.ToString().Contains("text"))
            {
                colInfo.m_dataType = ColumnDataType.STRING;
            }
            else if (dataType.ToString().Contains("big"))
            {
                colInfo.m_dataType = ColumnDataType.INT64_DATA;
            }
            else if (dataType.ToString().Contains("define"))
            {
                colInfo.m_dataType = ColumnDataType.DEFINE;
            }
            else
            {
                colInfo.m_dataType = ColumnDataType.INT_DATA;
            }
            // 查找关键列是哪一列,第一行是1的那一列
            if (keyType != null && keyType.ToString().Equals("1") || keyType.ToString().Equals("1.0"))
            {
                colInfo.m_isIndex = true;
            }
            else
            {
                colInfo.m_isIndex = false;
            }
            // 目前这个参数在导表工程中没有应用了
            colInfo.m_param = fieldName;
            // 表的名字,cs文件的名字
            colInfo.m_name = fieldName;
            if (cellData != null && commentType != null)
            {
                colInfo.m_comment = commentType.ToString();
            }
            colInfo.m_type      = null;
            colInfo.m_isKey     = false;
            colInfo.m_isRef     = false;
            colInfo.m_isComplex = false;
            colInfo.m_isArray   = false;

            // 表头为空
            if (fieldName.Length == 0)
            {
                if (aColInfoAry.Count > 0)
                {
                    UnityEngine.Debug.LogError(string.Format("表头格式错误,表{0}[{1}]列表头为空!", tableName, (aColIndex + 1).ToString()));
                }
                return(null);
            }

            Regex regRef     = new Regex(@"^\w+\s*\{.*\}$");
            Regex regComplex = new Regex(@"^\w+\s*\{.*\}\s*\[.*\]$");

            if (regComplex.IsMatch(fieldName))     // 复合列
            {
                colInfo.m_isComplex = true;
                colInfo.m_name      = fieldName.Substring(0, fieldName.IndexOf('{')).Trim(' ');
            }
            else if (regRef.IsMatch(fieldName))    // 参照列
            {
                colInfo.m_isRef = true;
                colInfo.m_name  = fieldName.Substring(0, fieldName.IndexOf('{')).Trim(' ');
            }
            else                                   // 普通列
            {
                Regex re = new Regex(@"^[a-zA-Z]+\w*$");
                if (!re.IsMatch(fieldName))
                {
                    UnityEngine.Debug.LogError(string.Format("表头格式错误,表{0}[\"{1}\"]列的表头存在非法字符(用字母、数字、下划线组合的以字母开头的名称)!", tableName, fieldName));
                    return(null);
                }
            }

            // 列名是否重复
            if (aColInfoAry.Exists(t => t.m_name == colInfo.m_name))
            {
                UnityEngine.Debug.LogError(string.Format("表头格式错误,表{0}[\"{1}\"]列名重复!", tableName, colInfo.m_name));
                return(null);
            }
            return(colInfo);
        }
Esempio n. 2
0
        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);
        }