Example #1
0
        /// <summary>
        /// 递归读取类里面的数据
        /// </summary>
        /// <param name="data"></param>
        /// <param name="sheetClass"></param>
        /// <param name="allSheetClassDic"></param>
        /// <param name="sheetDataDic"></param>
        private static void ReadData(object data, SheetClass sheetClass, Dictionary <string, SheetClass> allSheetClassDic, Dictionary <string, SheetData> sheetDataDic, string mainKey)
        {
            List <VarClass> varList  = sheetClass.VarList;
            VarClass        varClass = sheetClass.ParentVar;
            object          dataList = GetMemberValue(data, varClass.Name);

            int listCount = System.Convert.ToInt32(dataList.GetType().InvokeMember("get_Count", BindingFlags.Default | BindingFlags.InvokeMethod, null, dataList, new object[] { }));

            SheetData sheetData = new SheetData();

            if (!string.IsNullOrEmpty(varClass.Foregin))
            {
                sheetData.AllName.Add(varClass.Foregin);
                sheetData.AllType.Add(varClass.Type);
            }

            for (int i = 0; i < varList.Count; i++)
            {
                if (!string.IsNullOrEmpty(varList[i].Col))
                {
                    sheetData.AllName.Add(varList[i].Col);
                    sheetData.AllType.Add(varList[i].Type);
                }
            }

            string tempKey = mainKey;

            for (int i = 0; i < listCount; i++)
            {
                object item = dataList.GetType().InvokeMember("get_Item", BindingFlags.Default | BindingFlags.InvokeMethod, null, dataList, new object[] { i });

                RowData rowData = new RowData();
                if (!string.IsNullOrEmpty(varClass.Foregin) && !string.IsNullOrEmpty(tempKey))
                {
                    rowData.RowDataDic.Add(varClass.Foregin, tempKey);
                }

                if (!string.IsNullOrEmpty(sheetClass.MainKey))
                {
                    mainKey = GetMemberValue(item, sheetClass.MainKey).ToString();
                }

                for (int j = 0; j < varList.Count; j++)
                {
                    if (varList[j].Type == "list" && string.IsNullOrEmpty(varList[j].SplitStr))
                    {
                        SheetClass tempSheetClass = allSheetClassDic[varList[j].ListSheetName];
                        ReadData(item, tempSheetClass, allSheetClassDic, sheetDataDic, mainKey);
                    }
                    else if (varList[j].Type == "list")
                    {
                        SheetClass tempSheetClass = allSheetClassDic[varList[j].ListSheetName];
                        string     value          = GetSplitStrList(item, varList[j], tempSheetClass);
                        rowData.RowDataDic.Add(varList[j].Col, value);
                    }
                    else if (varList[j].Type == "listStr" || varList[j].Type == "listFloat" || varList[j].Type == "listInt" || varList[j].Type == "listBool" || varList[j].Type == "listUshort")
                    {
                        string value = GetSpliteBaseList(item, varList[j]);
                        rowData.RowDataDic.Add(varList[j].Col, value);
                    }
                    else
                    {
                        object value = GetMemberValue(item, varList[j].Name);
                        if (varList != null)
                        {
                            rowData.RowDataDic.Add(varList[j].Col, value.ToString());
                        }
                        else
                        {
                            Debug.LogError(varList[j].Name + "反射出来为空!");
                        }
                    }
                }

                string key = varClass.ListSheetName;
                if (sheetDataDic.ContainsKey(key))
                {
                    sheetDataDic[key].AllData.Add(rowData);
                }
                else
                {
                    sheetData.AllData.Add(rowData);
                    sheetDataDic.Add(key, sheetData);
                }
            }
        }
Example #2
0
        private static void ExcelToXml(string name)
        {
            Dictionary <string, SheetData> sheetDataDic = new Dictionary <string, SheetData>();
            string className = "";
            string xmlName   = "";
            string excelName = "";
            //第一步,读取Reg文件,确定类的结构
            Dictionary <string, SheetClass> allSheetClassDic = ReadReg(name, ref excelName, ref xmlName, ref className);

            //第二步,读取excel里面的数据
            string excelPath = ExcelPath + excelName;

            try
            {
                using (FileStream stream = new FileStream(excelPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    using (ExcelPackage package = new ExcelPackage(stream))
                    {
                        ExcelWorksheets worksheetArray = package.Workbook.Worksheets;
                        for (int i = 0; i < worksheetArray.Count; i++)
                        {
                            SheetData      sheetData  = new SheetData();
                            ExcelWorksheet worksheet  = worksheetArray[i + 1];
                            SheetClass     sheetClass = allSheetClassDic[worksheet.Name];
                            int            colCount   = worksheet.Dimension.End.Column;
                            int            rowCount   = worksheet.Dimension.End.Row;

                            for (int n = 0; n < sheetClass.VarList.Count; n++)
                            {
                                sheetData.AllName.Add(sheetClass.VarList[n].Name);
                                sheetData.AllType.Add(sheetClass.VarList[n].Type);
                            }

                            for (int m = 1; m < rowCount; m++)
                            {
                                RowData rowData = new RowData();
                                int     n       = 0;
                                if (string.IsNullOrEmpty(sheetClass.SplitStr) && sheetClass.ParentVar != null &&
                                    !string.IsNullOrEmpty(sheetClass.ParentVar.Foregin))
                                {
                                    rowData.ParnetVlue = worksheet.Cells[m + 1, 1].Value.ToString().Trim();
                                    n = 1;
                                }
                                for (; n < colCount; n++)
                                {
                                    ExcelRange range = worksheet.Cells[m + 1, n + 1];
                                    string     value = "";
                                    if (range.Value != null)
                                    {
                                        value = range.Value.ToString().Trim();
                                    }
                                    string colValue = worksheet.Cells[1, n + 1].Value.ToString().Trim();
                                    if (string.IsNullOrEmpty(GetNameFormCol(sheetClass.VarList, colValue)))
                                    {
                                        continue;
                                    }
                                    rowData.RowDataDic.Add(GetNameFormCol(sheetClass.VarList, colValue), value);
                                }

                                sheetData.AllData.Add(rowData);
                            }
                            sheetDataDic.Add(worksheet.Name, sheetData);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Debug.LogError(e);
                return;
            }

            //根据类的结构,创建类,并且给每个变量赋值(从excel里读出来的值)
            object objClass = CreateClass(className);

            List <string> outKeyList = new List <string>();

            foreach (string str in allSheetClassDic.Keys)
            {
                SheetClass sheetClass = allSheetClassDic[str];
                if (sheetClass.Depth == 1)
                {
                    outKeyList.Add(str);
                }
            }

            for (int i = 0; i < outKeyList.Count; i++)
            {
                ReadDataToClass(objClass, allSheetClassDic[outKeyList[i]], sheetDataDic[outKeyList[i]], allSheetClassDic, sheetDataDic, null);
            }

            BinarySerializeOpt.Xmlserialize(XmlPath + xmlName, objClass);
            //BinarySerializeOpt.BinarySerilize(BinaryPath + className + ".bytes", objClass);
            Debug.Log(excelName + "表导入unity完成!");
            AssetDatabase.Refresh();
        }