Beispiel #1
0
        private ExcelMapData get_or_create_index_map(ExcelMapData v_cur, Key v_key)
        {
            ExcelMapData rtn = v_cur.getData(v_key);

            if (rtn != null)
            {
                return(rtn);
            }
            switch (v_key.keytype)
            {
            case KeyType.Integer:
                rtn = new ExcelMapData();
                rtn.initAsTableData();
                v_cur.addData(v_key.ikey, rtn);
                break;

            case KeyType.String:
                rtn = new ExcelMapData();
                rtn.initAsTableData();
                v_cur.addData(v_key.skey, rtn);
                break;

            default:
                return(null);
            }
            Debug.Assert(rtn != null, string.Format("{0} 这个键暂时不支持", v_key.keytype));
            return(rtn);
        }
Beispiel #2
0
 public ExcelToMapData(ExcelMapData v_luaMap, bool v_isDataPersistence, string v_className)
 {
     _data = v_luaMap;
     _isDataPersistence = v_isDataPersistence;
     className          = v_className;
     sheet_bins         = new List <ExportSheetBin>();
 }
Beispiel #3
0
        private static int GetCellTxt(ExcelMapData data, StringBuilder v_sb, int v_layer)
        {
            if (v_layer > Separator.Length)
            {
                Debug.Error("层数超过了上限");
            }
            if (data.IsLeafe)
            {
                v_sb.Append(data.LeafVal.GetTxtValue());
                return(0);
            }
            List <KeyValue <ExcelMapData> > childDatas = data.GetKeyValues();

            if (childDatas == null)
            {
                return(0);
            }
            int deep = 0;

            for (int i = 0; i < childDatas.Count; i++)
            {
                if (i > 0)
                {
                    v_sb.Append(Separator[deep - 1]);
                }
                deep = Math.Max(deep, GetCellTxt(childDatas[i].val, v_sb, v_layer + 1) + 1);
            }
            return(deep);
        }
Beispiel #4
0
        public static LuaMap GetLuaTable(ExcelMapData v_root, bool v_bSingleKey = false)
        {
            LuaMap luaRoot = new LuaMap();

            luaRoot.init(true, ExportSheetBin.ROW_MAX_ELEMENT);
            _translate(v_root, luaRoot, v_bSingleKey);
            return(luaRoot);
        }
Beispiel #5
0
 public void addData(Key v_key, ExcelMapData v_data)
 {
     if (v_key.keytype == KeyType.String)
     {
         addData(v_key.skey, v_data);
     }
     else if (v_key.keytype == KeyType.Integer)
     {
         addData(v_key.ikey, v_data);
     }
 }
Beispiel #6
0
        private static void _translate(ExcelMapData v_src, LuaTable v_dst, bool v_bSingleKey = false)
        {
            List <KeyValue <ExcelMapData> > childDatas = v_src.GetKeyValues();

            for (int i = 0; i < childDatas.Count; i++)
            {
                KeyValue <ExcelMapData> child = childDatas[i];
                Key          key  = child.key;
                ExcelMapData data = child.val;
                switch (data.Type)
                {
                case EExcelMapDataType.indexMap:
                    LuaMap indexMap = new LuaMap();
                    indexMap.init(true, ExportSheetBin.ROW_MAX_ELEMENT);
                    v_dst.addData(key, indexMap);
                    _translate(data, indexMap, v_bSingleKey);
                    indexMap.Note = data.Note;
                    break;

                case EExcelMapDataType.rowData:
                    LuaMap rowData = new LuaMap();
                    rowData.init(false, ExportSheetBin.ROW_MAX_ELEMENT);
                    rowData.Single_value_hide_key = v_bSingleKey;
                    v_dst.addData(key, rowData);
                    _translate(data, rowData, v_bSingleKey);
                    rowData.Note = data.Note;
                    break;

                case EExcelMapDataType.cellTable:
                    LuaTable cellTable;
                    if (data.IsArray)
                    {
                        cellTable = new LuaArray();
                        ((LuaArray)cellTable).init(false, true, ExportSheetBin.ROW_MAX_ELEMENT);
                    }
                    else
                    {
                        cellTable = new LuaMap();
                        ((LuaMap)cellTable).init(false, ExportSheetBin.ROW_MAX_ELEMENT);
                    }
                    v_dst.addData(key, cellTable);
                    _translate(data, cellTable);
                    cellTable.Note = data.Note;
                    break;

                case EExcelMapDataType.cellData:
                    LuaValue leafVal = data.LeafVal.GetLuaValue();
                    v_dst.addData(key, leafVal);
                    leafVal.Note = data.Note;
                    break;
                }
            }
        }
Beispiel #7
0
        //把数据装载进中间结构
        public bool getExportMap(ExcelMapData v_root, int v_optCode)
        {
            if (indexData.pmKey == null)
            {
                return(false);
            }
            ExcelMapData rtn = v_root;

            //rtn.init(true, ExportSheetBin.ROW_MAX_ELEMENT);
            //rtn.Single_value_hide_key = true;
            //主键索引
            int[] colIndex = getColIndex();
            if (colIndex == null)
            {
                return(false);
            }
            int head_len = header.opt_head_len(v_optCode);

            for (int i = 0; i < data.Count; i++)
            {
                ExcelMapData cur = rtn;
                for (int j = 0; j < colIndex.Length; j++)
                {
                    try
                    {
                        int col     = colIndex[j];
                        Key the_key = data[i][col].ToKey();
                        if (j == colIndex.Length - 1)
                        {
                            cur      = get_or_create_index_map(cur, the_key);
                            cur.Type = EExcelMapDataType.rowData;
                            //把一行的数据装载进来
                            header.get_row_data(cur, data[i], v_optCode);
                        }
                        else
                        {
                            cur      = get_or_create_index_map(cur, the_key);
                            cur.Type = EExcelMapDataType.indexMap;
                        }
                        if (j == colIndex.Length - 1)
                        {
                            cur.Note = notes[i];
                        }
                    }
                    catch (Exception ex)
                    {
                        Debug.Exception("在装载第{0}行{1}列时发生错误,错误信息是{2}", i + 4, Tools.getColName(colIndex[j]), ex.ToString());
                    }
                }
            }
            return(true);
        }
Beispiel #8
0
        public static JsonTable GetJsonTable(ExcelMapData v_root)
        {
            JsonTable luaRoot = null;

            if (v_root.IsArray)
            {
                luaRoot = new JsonArray();
                ((JsonArray)luaRoot).init(true, ExportSheetBin.ROW_MAX_ELEMENT);
            }
            else
            {
                luaRoot = new JsonMap();
                ((JsonMap)luaRoot).init(true, ExportSheetBin.ROW_MAX_ELEMENT);
            }
            _translate(v_root, luaRoot);
            return(luaRoot);
        }
Beispiel #9
0
        public ExcelMapData get_row_data(ExcelMapData src_map, CellValue[] v_row_data, int v_optCode)
        {
            ExcelMapData rtn;

            if (src_map == null)
            {
                rtn = new ExcelMapData();
            }
            else
            {
                rtn = src_map;
            }
            Serchdata serch_root = new Serchdata(this, rtn);

            _get_row_data(serch_root, v_row_data, v_optCode);
            return(rtn);
        }
Beispiel #10
0
        private void _get_row_data(Serchdata v_st, CellValue[] v_row_data, int v_optCode)
        {
            ExcelMapData           curmap  = v_st.curmap;
            ComplexExcelHeaderNode parrent = v_st.node;
            List <KeyValue <ComplexExcelHeaderNode> > keyValSet = v_st.node.getKeyValueSet();

            for (int i = 0; i < keyValSet.Count; i++)
            {
                Key the_key = keyValSet[i].key;
                ComplexExcelHeaderNode node = parrent.get_node(the_key);


                if (node.IsSkip)
                {
                    continue;
                }
                if (node.IsLeaf)
                {
                    ExcelHeaderDecorate ehd = m_header_dct[node.LeafDataIdx];
                    if (!ehd.is_need_opt(v_optCode))
                    {
                        continue;
                    }
                    CellValue celldata = v_row_data[node.LeafDataIdx];
                    if (!celldata._isMiss)
                    {
                        ExcelMapData leafData = new ExcelMapData();
                        leafData.initAsLeafeData(celldata);
                        leafData.Type = EExcelMapDataType.cellData;
                        curmap.addData(the_key, leafData);
                    }
                }
                else
                {
                    ExcelMapData map_next = new ExcelMapData();
                    map_next.initAsTableData();
                    map_next.Type = EExcelMapDataType.cellTable;
                    Serchdata sdnew = new Serchdata(node, map_next);
                    _get_row_data(sdnew, v_row_data, v_optCode);
                    if (!map_next.IsEmpty())
                    {
                        curmap.addData(the_key, map_next);
                    }
                }
            }
        }
Beispiel #11
0
        private static void _translate(ExcelMapData v_src, StringBuilder v_dst, TxtExportHeader[] listFieldName)
        {
            StringBuilder lineBuilder = new StringBuilder();
            StringBuilder cellBuilder = new StringBuilder();
            List <KeyValue <ExcelMapData> > childDatas = v_src.GetKeyValues();

            for (int i = 0; i < childDatas.Count; i++)      //行
            {
                lineBuilder.Clear();
                ExcelMapData data = childDatas[i].val;
                if (data.Type != EExcelMapDataType.rowData)
                {
                    continue;
                }
                List <KeyValue <ExcelMapData> > childDatas2 = data.GetKeyValues();

                for (int j = 0; j < listFieldName.Length; j++)  //每一列
                {
                    if (lineBuilder.Length > 0)
                    {
                        lineBuilder.Append("\t");
                    }

                    int childIdx = _findChild(listFieldName[j].Name, childDatas2);

                    if (childIdx >= 0)
                    {
                        StringBuilder sb = new StringBuilder();
                        GetCellTxt(childDatas2[childIdx].val, sb, 0);
                        lineBuilder.Append(sb);
                    }
                    else
                    {
                        lineBuilder.Append(listFieldName[j].GetDefaultVal());
                    }
                }
                v_dst.Append(lineBuilder);
                v_dst.Append("\r\n");
            }
        }
Beispiel #12
0
        public void apposeReadExcel(string v_filePath)
        {
            Excel.Workbook  book        = new Excel.Workbook(v_filePath);
            Excel.Worksheet index_sheet = book.Worksheets["INDEX"];
            if (index_sheet == null)
            {
                __old_readExcel(v_filePath);
                return;
            }
            //读取索引表
            List <IndexSheetData> indexes      = new List <IndexSheetData>();
            SheetHeader           index_header = new SheetHeader();

            index_header.readHeader(index_sheet);
            Excel.Cells datas = index_sheet.Cells;

            for (int i = 1; i < 100; i++)
            {
                try
                {
                    if (datas[i, 0].Value == null || string.IsNullOrEmpty(datas[i, 0].Value.ToString()))
                    {
                        break;
                    }
                    IndexSheetData the_index = new IndexSheetData();
                    the_index.init(datas, i, index_header);
                    indexes.Add(the_index);
                }
                catch (Exception ex)
                {
                    Debug.Error("{0}读取索引列,第{1}行时报错,报错信息如下\r\n{2}", Path.GetFileName(v_filePath), i + 2, ex.ToString());
                    return;
                }
            }


            Dictionary <string, ExcelToMapData>[] table_memo    = new Dictionary <string, ExcelToMapData> [2];
            Dictionary <string, ExportSheetBin>[] sheetBin_memo = new Dictionary <string, ExportSheetBin> [2];
            string[] root_pathes = { Config.cliPath, Config.servPath };
            int[]    optCode     = { 1, 2 };
            for (int i = 0; i < table_memo.Length; i++)
            {
                table_memo[i]    = new Dictionary <string, ExcelToMapData>();
                sheetBin_memo[i] = new Dictionary <string, ExportSheetBin>();
            }
            //根据索引表读取各sheet
            foreach (IndexSheetData curIndex in indexes)
            {
                if (!curIndex.isOpt)
                {
                    continue;
                }
                Excel.Worksheet curSheet = book.Worksheets[curIndex.sheetName];
                if (curSheet == null)
                {
                    Debug.Error("{0}没有找到sheet[{1}]", Path.GetFileName(v_filePath), curIndex.sheetName);
                    return;
                }

                ExportSheetBin sheetBin = new ExportSheetBin();
                try
                {
                    if (!sheetBin.init(curSheet, curIndex))
                    {
                        return;
                    }
                }
                catch (Exception ex)
                {
                    Debug.Error("{0}_[{1}]导出基础数据时出现错误,错误信息为:\r\n{2}", Path.GetFileNameWithoutExtension(v_filePath), curIndex.sheetName, ex.ToString());
                    return;
                }
                //根据服务端的文件名,创建获取luamap
                string[] file_names = { curIndex.optCliFileName, curIndex.optSrvFileName };
                for (int i = 0; i < table_memo.Length; i++)//客户端服务端各生成一遍
                {
                    if (string.IsNullOrEmpty(file_names[i]))
                    {
                        continue;
                    }
                    if (!table_memo[i].ContainsKey(file_names[i]))
                    {
                        ExcelMapData   new_map           = new ExcelMapData();
                        bool           isDataPersistence = curIndex.isDataPersistence && (!string.IsNullOrEmpty(curIndex.optCliFileName)) && curIndex.optCliFileName.EndsWith(".lua");
                        ExcelToMapData new_data          = new ExcelToMapData(new_map,
                                                                              isDataPersistence,
                                                                              Path.GetFileNameWithoutExtension(file_names[i]));
                        table_memo[i].Add(file_names[i], new_data);
                    }
                    if (!sheetBin_memo[i].ContainsKey(file_names[i]))
                    {
                        sheetBin_memo[i].Add(file_names[i], sheetBin);
                    }
                    ExcelToMapData root_table = table_memo[i][file_names[i]];
                    root_table.add_sheetbin(sheetBin);
                    //把表中的数据读取到lua map里
                    //应当把这里的逻辑改为,把表中数据读到一个map_data中,而后转到各语言的结构中
                    try
                    {
                        sheetBin.getExportMap(root_table._data, optCode[i]);
                    }
                    catch (Exception ex)
                    {
                        Debug.Error("在装载【{0}】数据到中间结构时发生错误,错误信息是{1}", curIndex.sheetName, ex.ToString());
                    }
                }
            }

            //这里应当写为,根据后缀名导出不同的语言
            for (int i = 0; i < table_memo.Length; i++)
            {
                foreach (var cur_pair in table_memo[i])
                {
                    string         opt_path     = root_pathes[i] + cur_pair.Key;
                    OptData        optData      = null;
                    ExportSheetBin cur_sheetBin = sheetBin_memo[i][cur_pair.Key];
                    ELanguage      optLanguage  = cur_sheetBin.indexData.getOptLanguage(i);
                    bool           skip         = false;

                    switch (optLanguage)
                    {
                    case ELanguage.lua:
                        optData = LuaExporter.getExportContent(cur_pair.Value, optCode[i]);
                        break;

                    case ELanguage.json:
                        optData = JsonExporter.getExportContent(cur_pair.Value, optCode[i]);
                        break;

                    case ELanguage.xml:
                        Debug.Error("xml导出未实现");
                        break;

                    case ELanguage.none:
                        skip = true;
                        break;

                    default:
                        Debug.Error("未知导出语言");
                        break;
                    }
                    if (!skip)
                    {
                        if (optData.errList.Count > 0)
                        {
                            Debug.Error(string.Format("在导出{0}时发生错误:\r\n", cur_pair.Key) + optData.getErrInfo());
                            return;
                        }
                        File.WriteAllText(opt_path, optData.content);

                        //todo 如果是Realease模式,把文件输出到一个临时文件夹,调用LUAC,把lua文件生成到配置的cli文件夹中。
                    }
                }
            }
            Debug.Info("{0}:导表完成~~~", Path.GetFileName(v_filePath));
        }
Beispiel #13
0
        private static void _translate(ExcelMapData v_src, JsonTable v_dst)
        {
            List <KeyValue <ExcelMapData> > childDatas = v_src.GetKeyValues();

            for (int i = 0; i < childDatas.Count; i++)
            {
                KeyValue <ExcelMapData> child = childDatas[i];
                Key          key  = child.key;
                ExcelMapData data = child.val;
                try
                {
                    switch (data.Type)
                    {
                    case EExcelMapDataType.indexMap:
                        JsonTable indexMap = null;
                        if (data.IsArray)
                        {
                            indexMap = new JsonArray();
                            ((JsonArray)indexMap).init(true, ExportSheetBin.ROW_MAX_ELEMENT);
                        }
                        else
                        {
                            indexMap = new JsonMap();
                            ((JsonMap)indexMap).init(true, ExportSheetBin.ROW_MAX_ELEMENT);
                        }
                        v_dst.addData(key, indexMap);
                        _translate(data, indexMap);
                        break;

                    case EExcelMapDataType.rowData:
                        JsonMap rowData = new JsonMap();
                        rowData.init(false, ExportSheetBin.ROW_MAX_ELEMENT);
                        v_dst.addData(key, rowData);
                        _translate(data, rowData);
                        break;

                    case EExcelMapDataType.cellTable:
                        JsonTable cellTable;
                        if (data.IsArray)
                        {
                            cellTable = new JsonArray();
                            ((JsonArray)cellTable).init(false, ExportSheetBin.ROW_MAX_ELEMENT);
                        }
                        else
                        {
                            cellTable = new JsonMap();
                            ((JsonMap)cellTable).init(false, ExportSheetBin.ROW_MAX_ELEMENT);
                        }
                        v_dst.addData(key, cellTable);
                        _translate(data, cellTable);
                        break;

                    case EExcelMapDataType.cellData:
                        JsonValue leafVal = data.LeafVal.GetJsonValue();
                        v_dst.addData(key, leafVal);
                        break;
                    }
                }
                catch (Exception ex)
                {
                    Debug.Exception("在添加{0}时发生错误,错误信息是:\r\n{1}", key, ex.ToString());
                }
            }
        }
Beispiel #14
0
 public Serchdata(ComplexExcelHeaderNode v_node, ExcelMapData v_map)
 {
     node   = v_node;
     curmap = v_map;
 }
Beispiel #15
0
 public void addData(string v_sKey, ExcelMapData v_data)
 {
     Debug.Assert(!m_sData.ContainsKey(v_sKey), string.Format("ExcelMapData addData 添加了重复键{0}", v_sKey));
     m_sData.Add(v_sKey, v_data);
 }