/// <summary> /// 根据excel路径和对应的类名来实例化数据 /// </summary> /// <param name="filePath"></param> /// <param name="name"></param> /// <returns></returns> static System.Object CreateData(string filePath, string name) { FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read); ExcelPackage excelReader = new ExcelPackage(stream); ExcelWorkbook result = excelReader.Workbook; ExcelWorksheet workSheet = result.Worksheets.First(); //单个数据 Type dataType = SysUtil.GetTypeByName(PackageName + "." + name + "Config"); if (dataType == null) { Debug.LogError("type====" + name + "===is not find"); return(null); } //列表数据 Type configType = SysUtil.GetTypeByName(PackageName + "." + name + "ConfigData"); if (configType == null) { Debug.LogError("type=====" + name + "Config=======is not find"); return(null); } //获取列表变量 System.Reflection.FieldInfo field = configType.GetField("_config", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.FlattenHierarchy | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.GetField); if (field == null) { Debug.LogError("field not find !!! ======" + configType.Name + "._config"); return(null); } #region 遍历整个excel表读取每一行数据(可以扩展列表,枚举,其他表数据,这里只列出基本数据类型) int columns = workSheet.Dimension.End.Column; int rows = workSheet.Dimension.End.Row; System.Reflection.PropertyInfo[] tmpFileds = dataType.GetProperties(System.Reflection.BindingFlags.SetField | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance); System.Object configObj = Activator.CreateInstance(configType); IList m_DataList = field.GetValue(configObj) as IList; for (int i = 0; i < rows; i++) { if (i > 2) { System.Object target = Activator.CreateInstance(dataType); if (columns > 0) { string dd = workSheet.GetValue <string>(i + 1, 1); if (string.IsNullOrEmpty(dd)) { break; } } for (int j = 0, FiledsIndex = 0; j < columns; j++) { string kk = workSheet.GetValue <string>(i + 1, j + 1); if (FiledsIndex >= tmpFileds.Length) { continue; } TypeCode tpy = Type.GetTypeCode(tmpFileds[FiledsIndex].PropertyType); string value = workSheet.GetValue <string>(i + 1, j + 1); if (string.IsNullOrEmpty(value)) { value = ""; } value = value.TrimEnd(' '); if (!tmpFileds[FiledsIndex].CanWrite) { continue; } switch (tpy) { case TypeCode.Int32: if (kk != null) { if (string.IsNullOrEmpty(value)) { value = "0"; } try { tmpFileds[FiledsIndex].SetValue(target, Int32.Parse(value), null); } catch (System.Exception ex) { Debug.LogError(ex.ToString()); Debug.LogError(string.Format("Data error: {0} : {2}:[{1}] is not int", name, workSheet.GetValue <string>(i + 1, j + 1), tmpFileds[j].Name)); string key = workSheet.GetValue <string>(i + 1, 1); int keyValue; if (int.TryParse(key, out keyValue)) { Debug.LogError("上条错误对应的ID:" + keyValue); } } } else { tmpFileds[FiledsIndex].SetValue(target, 0, null); } break; case TypeCode.String: if (kk != null) { tmpFileds[FiledsIndex].SetValue(target, workSheet.GetValue <string>(i + 1, j + 1), null); } else { tmpFileds[FiledsIndex].SetValue(target, "", null); } break; case TypeCode.Single: if (kk != null) { try { if (string.IsNullOrEmpty(value)) { value = "0"; } tmpFileds[FiledsIndex].SetValue(target, float.Parse(value), null); } catch (System.Exception ex) { Debug.LogError(ex.ToString()); Debug.LogError(string.Format("Data error: {0} : {2}:[{1}] is not float", name, workSheet.GetValue <string>(i + 1, j + 1), tmpFileds[j].Name)); } } else { tmpFileds[FiledsIndex].SetValue(target, 0, null); } break; case TypeCode.Boolean: tmpFileds[FiledsIndex].SetValue(target, workSheet.GetValue <string>(i + 1, j + 1), null); break; default: break; } FiledsIndex++; } m_DataList.Add(target); } } #endregion #region 校验数据 stream.Close(); #endregion return(configObj); }
static void ExportExcelConfigAll() { string m_ExcelPath = ExcelPath; if (string.IsNullOrEmpty(m_ExcelPath)) { Debug.Log("没有设置服务器SNV目录"); return; } #region 设置好需要导出数据的配置表相关信息(因为变量名是驼峰命名,所以提前存起来) DirectoryInfo serverDirectoryInfo = new DirectoryInfo(m_ExcelPath); System.IO.FileInfo[] serverFileInfos = serverDirectoryInfo.GetFiles("*.xlsx", SearchOption.TopDirectoryOnly); Dictionary <string, string> dicPath = new Dictionary <string, string>(); Dictionary <string, string> classNameDic = new Dictionary <string, string>(); foreach (System.IO.FileInfo fileInfo in serverFileInfos) { string name = fileInfo.Name; name = name.Replace(".xlsx", ""); classNameDic[name] = name; dicPath[name] = m_ExcelPath + "/" + fileInfo.Name; } #endregion #region 实例化总表数据 Type AllConfigType = SysUtil.GetTypeByName(PackageName + ".ConfigDatabase"); if (AllConfigType == null) { return; } System.Object AllConfigData = Activator.CreateInstance(AllConfigType); #endregion #region 遍历总表属性,把对应的数据赋值到总表实例里面(通过属性设值) System.Reflection.PropertyInfo[] fields = AllConfigType.GetProperties(); foreach (System.Reflection.PropertyInfo f in fields) { if (f.Name.EndsWith("data")) { var start = Time.realtimeSinceStartup;//System.DateTime.Now.Ticks; string name = f.Name.Substring(2, f.Name.Length - 2); name = name.Substring(0, f.Name.Length - 7); if (!dicPath.ContainsKey(name)) { continue; } string filePath = dicPath[name]; System.Object tempData = CreateData(dicPath[name], classNameDic[name]); if (null == tempData) { continue; } f.SetValue(AllConfigData, tempData, null); } } #endregion System.IO.MemoryStream MstreamConfig = new System.IO.MemoryStream(); ProtoBuf.Serializer.Serialize(MstreamConfig, AllConfigData); byte[] dataConfig = MstreamConfig.ToArray(); var pathConfig = string.Format("{0}/{1}.bytes", ExportBytesPath, "ConfigDatabase"); System.IO.FileStream FstreamConfig = System.IO.File.Create(pathConfig); System.IO.BinaryWriter bwConfig = new System.IO.BinaryWriter(FstreamConfig); bwConfig.Write(dataConfig); FstreamConfig.Close(); bwConfig.Close(); MstreamConfig.Close(); AssetDatabase.Refresh(); }