Exemple #1
0
        /// <summary>
        /// 获取当前Sheet切页的表头信息
        /// </summary>
        /// <param name="sheet"></param>
        /// <returns></returns>
        public static List <ColoumnDesc> GetColoumnDesc(Sheet sheet)
        {
            int coloumnCount = GetSheetColoumns(sheet);
            List <ColoumnDesc> coloumnDescList = new List <ColoumnDesc>();

            for (int i = 0; i < coloumnCount; i++)
            {
                string comment = sheet.getCell(i, 0).getContents().Trim();
                comment = string.IsNullOrWhiteSpace(comment) ? comment : comment.Split('\n')[0];
                string typeStr = sheet.getCell(i, 1).getContents().Trim();
                string nameStr = sheet.getCell(i, 2).getContents().Trim();

                bool isArray = typeStr.Contains("[]");
                typeStr = typeStr.Replace("[]", "");
                FieldType fieldType;
                if (typeStr.ToLower().StartsWith("struct-"))
                {
                    typeStr   = typeStr.Remove(0, 7);
                    fieldType = FieldType.c_struct;
                }
                else if (typeStr.ToLower().StartsWith("enum-"))
                {
                    typeStr.Remove(0, 5);
                    fieldType = FieldType.c_enum;
                }
                else
                {
                    fieldType = StringToFieldType(typeStr);
                }
                ColoumnDesc coloumnDesc = new ColoumnDesc();
                coloumnDesc.index   = i;
                coloumnDesc.comment = comment;
                coloumnDesc.typeStr = typeStr;
                coloumnDesc.name    = nameStr;
                coloumnDesc.type    = fieldType;
                coloumnDesc.isArray = isArray;
                coloumnDescList.Add(coloumnDesc);
            }
            return(coloumnDescList);
        }
Exemple #2
0
        /// <summary>
        /// 生成最后的lua文件
        /// </summary>
        /// <param name="coloumnDesc"></param>
        /// <param name="sheet"></param>
        /// <returns></returns>
        public static string GenLuaFile(Sheet sheet)
        {
            List <ColoumnDesc> coloumnDesc = GetColoumnDesc(sheet);

            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.Append("--[[Notice:This lua config file is auto generate by Xls2Lua Tools,don't modify it manually! --]]\n");
            if (null == coloumnDesc || coloumnDesc.Count <= 0)
            {
                return(stringBuilder.ToString());
            }
            //创建索引
            Dictionary <string, int> fieldIndexMap = new Dictionary <string, int>();

            for (int i = 0; i < coloumnDesc.Count; i++)
            {
                fieldIndexMap[coloumnDesc[i].name] = i + 1;
            }
            //创建数据块的索引表
            stringBuilder.Append("local fieldIdx = {}\n");
            foreach (var cur in fieldIndexMap)
            {
                stringBuilder.Append(string.Format("fieldIdx.{0} = {1}\n", cur.Key, cur.Value));
            }

            //创建数据块
            stringBuilder.Append("local data = {");
            int rows        = GetSheetRows(sheet);
            int validRowIdx = 4;

            //逐行读取并处理
            for (int i = validRowIdx; i < rows; i++)
            {
                StringBuilder oneRowBuilder = new StringBuilder();
                oneRowBuilder.Append("{");
                //对应处理每一列
                for (int j = 0; j < coloumnDesc.Count; j++)
                {
                    ColoumnDesc curColoumn = coloumnDesc[j];
                    var         curCell    = sheet.getCell(curColoumn.index, i);
                    string      content    = curCell.getContents();

                    if (FieldType.c_struct != curColoumn.type)
                    {
                        FieldType fieldType = curColoumn.type;
                        //如果不是数组类型的话
                        if (!curColoumn.isArray)
                        {
                            content = GetLuaValue(fieldType, content);
                            oneRowBuilder.Append(content);
                        }
                        else
                        {
                            StringBuilder tmpBuilder    = new StringBuilder("{");
                            var           tmpStringList = content.Split(splitSymbol, StringSplitOptions.RemoveEmptyEntries);
                            for (int k = 0; k < tmpStringList.Length; k++)
                            {
                                tmpStringList[k] = GetLuaValue(fieldType, tmpStringList[k]);
                                tmpBuilder.Append(tmpStringList[k]);
                                if (k != tmpStringList.Length - 1)
                                {
                                    tmpBuilder.Append(",");
                                }
                            }

                            tmpBuilder.Append("}");

                            oneRowBuilder.Append(tmpBuilder);
                            //oneRowBuilder.Append("}");
                        }
                    }
                    else
                    {
                        //todo:可以处理结构体类型的字段
                        throw new Exception("暂不支持结构体类型的字段!");
                    }

                    if (j != coloumnDesc.Count - 1)
                    {
                        oneRowBuilder.Append(",");
                    }
                }

                oneRowBuilder.Append("},");
                stringBuilder.Append(string.Format("\n{0}", oneRowBuilder));
            }
            //当所有的行都处理完成之后
            stringBuilder.Append("}\n");
            //设置元表
            string str =
                "local mt = {}\n" +
                "mt.__index = function(a,b)\n" +
                "\tif fieldIdx[b] then\n" +
                "\t\treturn a[fieldIdx[b]]\n" +
                "\tend\n" +
                "\treturn nil\n" +
                "end\n" +
                "mt.__newindex = function(t,k,v)\n" +
                "\terror('do not edit config')\n" +
                "end\n" +
                "mt.__metatable = false\n" +
                "for _,v in ipairs(data) do\n\t" +
                "setmetatable(v,mt)\n" +
                "end\n" +
                "return data";

            stringBuilder.Append(str);
            return(stringBuilder.ToString());
        }