Exemple #1
0
        /// <summary>
        /// 读取excel
        /// </summary>
        /// <param name="path"></param>
        private void ReadExcel(int index)
        {
            if (PathNames == null || PathNames.Length <= index)
            {
                return;
            }
            SelectKey = PathNames[index];
            Workbook  = ExcelMgr.ReadExcel(SelectKey);
            if (Workbook == null)
            {
                if (EditorUtility.DisplayDialog("错  误", string.Format("Excel路径<{0}>没有找到指定配置表!", SelectKey), "确定"))
                {
                    return;
                }
                return;
            }
            SheetNum = Workbook.NumberOfSheets;
            //sheetsName = new string[mSheetNum];
            ToggleSheetList = new bool[SheetNum];
            SheetsName      = ExcelMgr.GetSheetsName(Workbook);
//             for (int i = 0; i < mSheetNum; i++)
//             {
//                 sheetsName[i] = mWorkbook.GetSheetName(i);
//             }

            //
            //             ISheet sheet = workbook.GetSheetAt(0);  //获取第一个工作表
            //             IRow row;// = sheet.GetRow(0);            //新建当前工作表行数据
            //             for (int i = 0; i < sheet.LastRowNum; i++)  //对工作表每一行
            //             {
            //                 row = sheet.GetRow(i);   //row读入第i行数据
            //                 if (row != null)
            //                 {
            //                     for (int j = 0; j < row.LastCellNum; j++)  //对工作表每一列
            //                     {
            //                         string cellValue = row.GetCell(j).ToString(); //获取i行j列数据
            //                         //Console.WriteLine(cellValue);
            //                     }
            //                 }
            //             }
            //Console.ReadLine();
            ExcelMgr.CloseExcel(Workbook);
        }
Exemple #2
0
        public static void GeneratingResourceFiles(Dictionary <string, Table> dic)
        {
            if (dic == null)
            {
                return;
            }
            EditorUtility.DisplayCancelableProgressBar("生成配置文件", "开始生成", 0);
            int len   = dic.Values.Count;
            int index = 0;

            foreach (Table table in dic.Values)
            {
                if (!table.IsOutput)
                {
                    continue;
                }
                EditorUtility.DisplayCancelableProgressBar("生成配置文件", string.Format("正在读取Excel配置表:{0}", table.Path), index / len);
                IWorkbook workbook = ExcelMgr.ReadExcel(table.Path);
                if (workbook == null)
                {
                    continue;
                }
                Dictionary <string, TableInfo> .ValueCollection values = table.Dic.Values;
                foreach (TableInfo ti in values)
                {
                    if (!ti.IsOutput)
                    {
                        continue;
                    }
                    EditorUtility.DisplayCancelableProgressBar("生成配置文件", string.Format("正在解析Excel配置表:{0}分页:{0}", Path.GetDirectoryName(table.Path), ti.Sheet), index / len);
                    Generating(table.Path, workbook, ti);
                }
                EditorUtility.DisplayCancelableProgressBar("生成配置文件", string.Format("Excel配置表{0}cfg文件生成完成, 关闭{0}Excel表", Path.GetDirectoryName(table.Path), table.Path), index / len);
                ExcelMgr.CloseExcel(workbook);
                index++;
            }
            EditorUtility.DisplayCancelableProgressBar("生成配置文件", "Excel配置表数据生成完毕!!", 1);
            EditorUtility.ClearProgressBar();
        }
Exemple #3
0
 /// <summary>
 /// 更新配置数据
 /// </summary>
 private void UpdateTableInfo(int index)
 {
     if (SheetsName == null || SheetsName.Count == 0)
     {
         return;
     }
     if (Workbook == null)
     {
         return;
     }
     SelectSheetName = SheetsName[index];
     ParamNames      = ExcelMgr.GetSheetTitle(Workbook, SelectSheetName);
     if (ConfigDic.ContainsKey(SelectKey))
     {
         if (!ConfigDic[SelectKey].Dic.ContainsKey(SelectSheetName))
         {
             TableInfo table = new TableInfo();
             table.Sheet = SelectSheetName;
             table.UpdateParams(ParamNames);
             ConfigDic[SelectKey].Dic.Add(SelectSheetName, table);
         }
     }
 }
Exemple #4
0
        private static void Generating(string path, IWorkbook iwb, TableInfo ti)
        {
            ISheet sheet = iwb.GetSheet(ti.Sheet);

            if (sheet == null)
            {
                return;
            }
            List <string> names = ExcelMgr.GetSheetTitle(iwb, ti.Sheet);

            if (names == null || names.Count == 0)
            {
                return;
            }
            int len = sheet.LastRowNum;

            if (len < 1)
            {
                MessageBox.Error(string.Format("{0}没有数据!!", path));
                return;
            }
            List <object> outputList = new List <object>();

            for (int i = 1; i <= len; i++)
            {
                IRow row = sheet.GetRow(i);
                if (row == null)
                {
                    continue;
                }
                Dictionary <string, ParamInfo> param = ti.Params;
                if (param == null)
                {
                    continue;
                }
                object obj = InstantiationScript(ti.ScriptPath);
                if (obj == null)
                {
                    continue;
                }
                Type t = obj.GetType();
                foreach (KeyValuePair <string, ParamInfo> pd in param)
                {
                    if (!pd.Value.IsOutput)
                    {
                        continue;
                    }
                    if (pd.Value.Index == 0)
                    {
                        continue;
                    }
                    string proName = ti.PropertyNames[pd.Value.Index];
                    Type   proType = ti.PropertyDic[proName];
                    if (proType == null)
                    {
                        continue;
                    }
                    string       value = row.GetCell(pd.Value.CellIndex).ToString();
                    PropertyInfo pro   = t.GetProperty(proName);
                    if (pro != null)
                    {
                        pro.SetValue(obj, TypeConversion(value, proType), null);
                    }
                }
                outputList.Add(obj);
            }
            if (outputList.Count == 0)
            {
                Debug.LogError("GetByteByDic Fail!!, dic is null");
                return;
            }
            string outputPath = string.Format("{0}{1}", PathTool.DataPath, PathTool.Temp);

            if (string.IsNullOrEmpty(outputPath))
            {
                Debug.LogError("Get Assets Path Fail!!, InfoTool.PathConfig[EditorPrefsKey.AssetsPath] is null");
                return;
            }
            Config.OutputConfig <object>(outputPath, ti.OutputName, outputList, SuffixTool.TableInfo.ToLower());
        }
Exemple #5
0
        private IEnumerator Read(string path, Action <string, Color> setTipVal, Action finish)
        {
            if (mWorkbook == null)
            {
                mWorkbook = ExcelMgr.ReadExcel(mPath);
            }
            yield return(new WaitForEndOfFrame());

            if (mWorkbook == null)
            {
                setTipVal(string.Format("Excel路径<{0}>没有找到指定配置表!", mPath), Color.red);
            }
            else
            {
                mProgressBar.value = 0.01f;
                setTipVal("读取完成", Color.green);
                yield return(new WaitForEndOfFrame());

                List <object> outputList = new List <object>();
                string        cfgName    = null;
                string[]      keys       = null;
                int           count      = mWorkbook.NumberOfSheets;
                var           pgs        = 0.9f / count;
                for (var page = 0; page < count; page++)
                {
                    ISheet sheet = mWorkbook.GetSheetAt(page);
                    if (sheet != null)
                    {
                        if (keys == null)
                        {
                            IRow row = ExcelMgr.GetSheetRow(sheet, 1);
                            keys = new string[row.LastCellNum];
                            if (row != null && row.LastCellNum > 1)
                            {
                                for (int c = 0; c < row.LastCellNum; c++)
                                {
                                    var str = row.GetCell(c).ToString();
                                    if (string.IsNullOrEmpty(str))
                                    {
                                        yield return(new WaitForEndOfFrame());

                                        goto ROW_CELL_IS_NULL;
                                    }
                                    keys[c] = str;
                                }
                            }
                            else
                            {
                                yield return(new WaitForEndOfFrame());

                                goto DECODE_EXCEL_FAIL;
                            }
                        }
                        var rowLen = sheet.LastRowNum + 1;
                        mProgressBar.value = (pgs * page) - (rowLen * pgs) * 2;
                        if (rowLen <= 2)
                        {
                            continue;
                        }
                        for (var r = 2; r < rowLen; r++)
                        {
                            IRow row = ExcelMgr.GetSheetRow(sheet, r);
                            if (row != null && row.LastCellNum > 1)
                            {
                                object cfg = null;
                                for (int c = 0; c < row.LastCellNum; c++)
                                {
                                    var key = keys[c];
                                    if (c == 0)
                                    {
                                        if (!string.IsNullOrEmpty(key))
                                        {
                                            cfg = InfoTool.InstantiationScript(key);
                                            if (cfg == null)
                                            {
                                                goto GET_ASSEMBLY_FAIL;
                                            }
                                            cfgName = key;
                                            yield return(new WaitForEndOfFrame());
                                        }
                                        else
                                        {
                                            yield return(new WaitForEndOfFrame());

                                            goto GET_ASSEMBLY_FAIL;
                                        }
                                    }
                                    else
                                    {
                                        var cell = row.GetCell(c);
                                        if (cell == null)
                                        {
                                            continue;
                                        }
                                        WriteCfg(keys[c], row.GetCell(c).ToString(), ref cfg);
                                    }
                                }
                                if (cfg == null)
                                {
                                    continue;
                                }
                                outputList.Add(cfg);
                            }
                            mProgressBar.value = (pgs * page) - (rowLen * pgs) * r;
                            yield return(new WaitForEndOfFrame());
                        }
                    }
                }
                mProgressBar.value = 0.9f;
                setTipVal("解码完成", Color.green);
                yield return(new WaitForEndOfFrame());

                string outputPath = string.Format("{0}{1}", PathTool.DataPath, PathTool.Temp);
                Config.OutputConfig <object>(outputPath, cfgName, outputList, SuffixTool.TableInfo.ToLower());
                mProgressBar.value = 1.0f;
            }
            goto FINISH;
            ROW_CELL_IS_NULL  : MessageBox.Error("配置表字段key为null"); goto RETURN;
            GET_ASSEMBLY_FAIL : MessageBox.Error("程序集没有识别到Assembly对应的脚本。"); goto RETURN;
            DECODE_EXCEL_FAIL : MessageBox.Error("解析excel失败"); goto RETURN;
            RETURN            : Clear();
FINISH:
            setTipVal("导出完成", Color.green);
            Clear();
            finish();
        }