예제 #1
0
 /// <summary>
 /// 实际的类转XML
 /// </summary>
 /// <param name="name"></param>
 static void ClassToXml(string name)
 {
     if (string.IsNullOrEmpty(name))
     {
         return;
     }
     try
     {
         Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
         Type       type       = null;
         foreach (Assembly assembly in assemblies)
         {
             type = assembly.GetType(name);
             if (type != null)
             {
                 break;
             }
         }
         if (type != null)
         {
             //反射创建类
             var tempClass = Activator.CreateInstance(type);
             if (tempClass is ExcelBase)
             {
                 //执行编译器下构造方法
                 (tempClass as ExcelBase).Construction();
             }
             string xmlPath = XML_DATA_PATH + name + ".xml";
             if (File.Exists(xmlPath))
             {
                 Debug.LogError("该类已生成过Xml,需删除后重新生成 path:" + xmlPath);
             }
             else
             {
                 BinarySerializeOpt.XmlSerialize(xmlPath, tempClass);
                 Debug.Log("类转Xml成功 path:" + xmlPath);
             }
         }
     }
     catch (System.Exception)
     {
         Debug.LogError("ClassToXml 转换失败 name:" + name);
     }
 }
예제 #2
0
    /// <summary>
    /// 将运行中的实际类转成Xml
    /// </summary>
    /// <param name="className"></param>
    public static void ClassToXml(string className)
    {
        if (string.IsNullOrEmpty(className))
        {
            return;
        }
        try
        {
            Type type = null;
            foreach (var asm in AppDomain.CurrentDomain.GetAssemblies())
            {
                Type tempType = asm.GetType(className);
                if (tempType != null)
                {
                    type = tempType;
                    break;
                }
            }

            if (type != null)
            {
                var obj = Activator.CreateInstance(type);
                if (obj is ExcelBase)
                {
                    (obj as ExcelBase).Construction();
                }

                BinarySerializeOpt.XmlSerialize(XMLPATH + className + ".xml", obj);
                Debug.Log(className + "类转成Xml成功!");
            }
        }
        catch (Exception e)
        {
            Debug.LogError(className + "类转xml失败!" + e);
        }
    }
예제 #3
0
    public static void ExcelToXml(string xmlRegName)
    {
        string className = "";
        string excelName = "";
        string xmlName   = "";
        //读取reg文件数据
        Dictionary <string, SheetClass> allSheetClassDic = ReadReg(xmlRegName, ref className, ref excelName, ref xmlName);
        //读取excel文件数据
        string excelPath = EXCELPATH + excelName;
        Dictionary <string, SheetData> allSheetDataDic = new Dictionary <string, SheetData>();

        try
        {
            using (FileStream stream = new FileStream(excelPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                using (ExcelPackage excelPackage = new ExcelPackage(stream))
                {
                    ExcelWorksheets worksheetArray = excelPackage.Workbook.Worksheets;
                    for (int i = 0; i < worksheetArray.Count; i++)
                    {
                        SheetData      sheetData = new SheetData();
                        ExcelWorksheet worksheet = worksheetArray[i + 1];//索引从1开始
                        string         key       = GetDicKey(allSheetClassDic, worksheet.Name);
                        if (string.IsNullOrEmpty(key))
                        {
                            Debug.Log("配置表在reg文件中不存在!请检查");
                            return;
                        }
                        SheetClass sheetClass = allSheetClassDic[key];
                        int        colCount   = worksheet.Dimension.End.Column;
                        int        rowCount   = worksheet.Dimension.End.Row;
                        for (int j = 0; j < sheetClass.VarList.Count; j++)
                        {
                            sheetData.AllName.Add(sheetClass.VarList[j].Name);
                            sheetData.AllType.Add(sheetClass.VarList[j].Type);
                        }

                        for (int j = 1; j < rowCount; j++)
                        {
                            RowData rowData = new RowData();
                            int     k       = 0;
                            if (string.IsNullOrEmpty(sheetClass.SplitStr) && sheetClass.ParentVar != null &&
                                !string.IsNullOrEmpty(sheetClass.ParentVar.Foregin))
                            {
                                rowData.parentValue = worksheet.Cells[j + 1, 1].Value.ToString().Trim();
                                k = 1;
                            }
                            for (; k < colCount; k++)
                            {
                                ExcelRange range = worksheet.Cells[j + 1, k + 1];//索引从1开始
                                string     value = "";
                                if (range.Value != null)
                                {
                                    value = range.Value.ToString().Trim();
                                }
                                string colValue = worksheet.Cells[1, k + 1].Value.ToString().Trim();
                                rowData.RowDataDic.Add(GetNameFormCol(sheetClass.VarList, colValue), value);
                            }
                            sheetData.AllData.Add(rowData);
                        }
                        allSheetDataDic.Add(worksheet.Name, sheetData);
                    }
                }
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }
        //根据类结构创建类,并将execl数据将变量赋值,然后调用xml序列化
        object            objClass   = CreatClass(className);
        List <SheetClass> outKeyList = new List <SheetClass>();

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

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

        BinarySerializeOpt.XmlSerialize(XMLPATH + xmlName, objClass);
        Debug.Log(excelName + "表导入完成!");
        AssetDatabase.Refresh();
    }
예제 #4
0
    private static void ExcelToXml(string name)
    {
        string className = "";
        string from      = "";
        string to        = "";
        //储存所有变量 sheet名 SheetClass
        Dictionary <string, SheetClass> allSheetClassDic = ReadReg(name, ref className, ref from, ref to);
        //储存所有data sheet名 sheetdata
        Dictionary <string, SheetData> allSheetDataDic = new Dictionary <string, SheetData>();

        string excelPath = EXCEL_DATA_PATH + from;

        // string xmlPath = XML_DATA_PATH+to;

        //读取excel文件
        try{
            using (FileStream fs = new FileStream(excelPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                using (ExcelPackage package = new ExcelPackage(fs)){
                    ExcelWorksheets workSheets = package.Workbook.Worksheets;
                    for (int i = 1; i <= workSheets.Count; i++)
                    {
                        ExcelWorksheet workSheet = workSheets[i];
                        int            colCount  = workSheet.Dimension.End.Column;
                        int            rowCount  = workSheet.Dimension.End.Row;
                        SheetData      sheetData = new SheetData();//储存这一页的信息
                        //Reg中sheet的信息
                        SheetClass sheetClass = allSheetClassDic[workSheet.Name];
                        for (int j = 0; j < sheetClass.VarList.Count; j++)
                        {
                            VarClass curVarClass = sheetClass.VarList[j];
                            //储存 变量名 类型
                            sheetData.AllName.Add(curVarClass.Name);
                            sheetData.AllType.Add(curVarClass.Type);
                        }
                        //读取excel中的数据
                        //第一行是标题 所以跳过
                        for (int row = 1; row < rowCount; row++)
                        {
                            RowData data = new RowData();
                            int     col  = 0;
                            //如果这页sheet是外键数据 则第一列对应mainKey 数据从第二列开始
                            if (string.IsNullOrEmpty(sheetClass.SplitStr) && !string.IsNullOrEmpty(sheetClass.ParentVar.Foreign))
                            {
                                data.ParentKey = workSheet.Cells[row + 1, 1].Value.ToString().Trim();
                                col            = 1;
                            }
                            for (; col < colCount; col++)
                            {
                                //每一行的信息
                                ExcelRange range   = workSheet.Cells[row + 1, col + 1];
                                string     colName = workSheet.Cells[1, col + 1].Value.ToString();
                                string     value   = range.Value != null?range.Value.ToString().Trim() : "";

                                data.RowDataDic.Add(GetNameFormCol(sheetClass.VarList, colName), value);
                            }
                            sheetData.AllRowData.Add(data);
                        }
                        allSheetDataDic.Add(workSheet.Name, sheetData);
                    }
                }
        }
        catch (System.Exception e) {
            Debug.LogError("XmlToExcel:Excel写入错误 " + e);
            return;
        }
        //创建类 并赋值
        object        objClass   = CreateClass(className);
        List <string> outKeyList = new List <string>();

        foreach (var item in allSheetClassDic)
        {
            SheetClass sheetClass = item.Value;
            if (sheetClass.Depth == 1)
            {
                outKeyList.Add(item.Key);
            }
        }
        for (int i = 0; i < outKeyList.Count; i++)
        {
            string key = outKeyList[i];
            ReadDataToClass(objClass, allSheetClassDic[key], allSheetDataDic[key], allSheetClassDic, allSheetDataDic);
        }
        string xmlPath = XML_DATA_PATH + name + ".xml";
        object obj     = BinarySerializeOpt.XmlSerialize(xmlPath, objClass);

        //转成二进制
        // BinarySerializeOpt.BinarySerialize(BINARY_DATA_PATH, obj);
        Debug.Log("Excel转Xml完成!" + from + "-->" + to);
        AssetDatabase.Refresh();
    }