コード例 #1
0
        private static void CollectHeadInfo(DataSet mResultSet, out DataTable mSheet, out int colCount,
                                            out int rowCount, out List <TableField> heads, out List <int> keyIdxs)
        {
            if (mResultSet.Tables.Count < 1)
            {
                throw new Exception("Tables.Count < 1");
            }

            mSheet = mResultSet.Tables[0];
            if (mSheet.Rows.Count < 5)
            {
                throw new Exception("Rows.Count < 5");
            }

            rowCount = mSheet.Rows.Count;
            colCount = mSheet.Columns.Count;
            int maxColIdx = 0;

            for (int col = 1; col < colCount; col++)
            {
                var _str = mSheet.Rows[4][col];
                if (_str == null || string.IsNullOrEmpty(_str.ToString()))
                {
                    break;
                }

                maxColIdx = col;
            }


            StringBuilder stringBuilder = new StringBuilder();

            //get the header
            heads = new List <TableField>();
            int rowIdx = 0;

            rowIdx = 1;
            for (int col = 1; col <= maxColIdx; col++)
            {
                var filed = new TableField();
                var str   = "";
                int idx   = 1;
                //parse key info
                str = mSheet.Rows[idx++][col].ToString().ToLower();
                filed.SetDefine(str);
                //comment
                str           = mSheet.Rows[idx++][col].ToString();
                filed.comment = str;
                //type
                str = mSheet.Rows[idx++][col].ToString().ToLower();
                if (str.EndsWith("[]"))
                {
                    filed.isList = true;
                    str          = str.Replace("[]", "");
                }

                filed.SetFieldType(str);
                //filedName
                str             = mSheet.Rows[idx++][col].ToString();
                filed.filedName = str;
                heads.Add(filed);
            }

            keyIdxs = new List <int>();
            for (int i = 0; i < heads.Count; i++)
            {
                var item = heads[i];
                if (item.define == TableDefine.Key)
                {
                    keyIdxs.Add(i);
                }
            }

            if (keyIdxs.Count == 0)
            {
                keyIdxs.Add(0);
            }

            if (keyIdxs.Count > 4)
            {
                throw new Exception("KeyCount 超过4");
            }

            if (keyIdxs[keyIdxs.Count - 1] != (keyIdxs.Count - 1))
            {
                //throw new Exception("Key 不连续 不是前面列");
            }

            foreach (var item in keyIdxs)
            {
                var head = heads[item];
                if (head.fieldType == ETableBaseType.String ||
                    head.fieldType == ETableBaseType.Bool ||
                    head.fieldType == ETableBaseType.Float
                    )
                {
                    throw new Exception("不支持非整形的Key");
                }
            }

            if ((colCount - 1) != heads.Count)
            {
                //throw new Exception("columnsCount != heads.Count");
                Console.WriteLine("colCount = {0} , heads.Count = {1}", colCount, heads.Count);
            }

            for (int i = 5; i < rowCount; i++)
            {
                foreach (var keyidx in keyIdxs)
                {
                    var cellStr = mSheet.Rows[i][keyidx + 1].ToString();
                    if (string.IsNullOrEmpty(cellStr))
                    {
                        rowCount = i;
                        return;
                    }
                }
            }
        }