/// <summary> /// Excel -> 二进制文件 /// </summary> /// <param name="inPath"></param> /// <param name="inFolderName"></param> /// <returns></returns> public bool Bin(string inPath, string inFolderName) { Assist.CheckFolderExist(inFolderName); PBData data = new PBData(); Assist.GetObjPaths(".xls", inPath).ForEach(delegate(string path) { DataSet ds = Assist.ExcelToData(path); DataTable dt = ds.Tables[0]; string classname = Stringer.FirstLetterUp(dt.TableName); Type classType = Type.GetType(classname); FieldInfo fieldInfo = typeof(PBData).GetField(classname + "Dic"); IDictionary fieldValue = fieldInfo.GetValue(data) as IDictionary; if (classType != null) { for (int i = 4; i < dt.Rows.Count; i++) { string key = ""; // key List <string> ls = new List <string>(); for (int x = 0; x < dt.Columns.Count; x++) { ls.Add(dt.Rows[i][x].ToString()); if (dt.Rows[2][x].ToString().Split('+')[2] == "1")// 当前值为Key值的一部分 { key += dt.Rows[i][x].ToString() + "_"; } } key = key.Remove(key.Length - 1, 1); IProtoBufable value = Activator.CreateInstance(classType) as IProtoBufable;// value value.Set(ls); fieldValue.Add(key, value); } fieldInfo.SetValue(data, fieldValue); } }); #region SceneLayout 读取同级目录下的“Excel/SceneLayout/”内的所有 xml 文件,并将其数据写入 PBData Dictionary <string, XmlDocument> doc = Assist.GetXml(Assist.RootPath + "Excel/SceneLayout/"); foreach (KeyValuePair <string, XmlDocument> item in doc) { SceneLayout sl = new SceneLayout(); XmlNodeList xcc = item.Value.SelectSingleNode("Config").ChildNodes; for (int i = 0; i < xcc.Count; i++) { SceneLayoutItem sli = new SceneLayoutItem(); IProtoBufable xmlItemValue = new SceneLayoutItem() as IProtoBufable;// value List <string> xls = new List <string>(); for (int x = 0; x < xcc[i].Attributes.Count; x++) { xls.Add(xcc[i].Attributes[x].Value); } xmlItemValue.Set(xls); sl.item.Add(xmlItemValue as SceneLayoutItem); } data.SceneLayoutDic.Add(item.Key, sl); } #endregion using (var file = System.IO.File.Create("PBData")) { try { ProtoBuf.Serializer.Serialize(file, data); } catch (Exception e) { MainWindow.Show(e.ToString()); } } return(true); }
/// <summary> /// Excel -> 二进制文件 /// </summary> /// <param name="inPath"></param> /// <param name="inFolderName"></param> /// <returns></returns> public bool Bin(string inPath, string inFolderName) { Assist.CheckFolderExist(inFolderName); PBData data = new PBData(); Assist.GetObjPaths(".xls", inPath).ForEach(delegate(string path) { DataSet ds = Assist.ExcelToData(path); DataTable dt = ds.Tables[0]; string classname = Stringer.FirstLetterUp(dt.TableName); Type classType = Type.GetType(classname); FieldInfo fieldInfo = typeof(PBData).GetField(classname + "Dic"); IDictionary fieldValue = fieldInfo.GetValue(data) as IDictionary; if (classType != null) { for (int i = 4; i < dt.Rows.Count; i++) { string key = ""; // key List<string> ls = new List<string>(); for (int x = 0; x < dt.Columns.Count; x++) { ls.Add(dt.Rows[i][x].ToString()); if (dt.Rows[2][x].ToString().Split('+')[2] == "1")// 当前值为Key值的一部分 { key += dt.Rows[i][x].ToString() + "_"; } } key = key.Remove(key.Length - 1, 1); IProtoBufable value = Activator.CreateInstance(classType) as IProtoBufable;// value value.Set(ls); fieldValue.Add(key, value); } fieldInfo.SetValue(data, fieldValue); } }); #region SceneLayout 读取同级目录下的“Excel/SceneLayout/”内的所有 xml 文件,并将其数据写入 PBData Dictionary<string, XmlDocument> doc = Assist.GetXml(Assist.RootPath + "Excel/SceneLayout/"); foreach (KeyValuePair<string, XmlDocument> item in doc) { SceneLayout sl = new SceneLayout(); XmlNodeList xcc = item.Value.SelectSingleNode("Config").ChildNodes; for (int i = 0; i < xcc.Count; i++) { SceneLayoutItem sli = new SceneLayoutItem(); IProtoBufable xmlItemValue = new SceneLayoutItem() as IProtoBufable;// value List<string> xls = new List<string>(); for (int x = 0; x < xcc[i].Attributes.Count; x++) { xls.Add(xcc[i].Attributes[x].Value); } xmlItemValue.Set(xls); sl.item.Add(xmlItemValue as SceneLayoutItem); } data.SceneLayoutDic.Add(item.Key, sl); } #endregion using (var file = System.IO.File.Create("PBData")) { try { ProtoBuf.Serializer.Serialize(file, data); } catch (Exception e) { MainWindow.Show(e.ToString()); } } return true; }