1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
| [MenuItem("Tools/Xml/所有Excel转Xml")] public static void AllExcelToXml() { string[] regPaths = Directory.GetFiles(RegPath, "*", SearchOption.AllDirectories);
for (int i = 0; i < regPaths.Length; i++) { if (Path.GetExtension(regPaths[i]) != ".xml") { continue; } EditorUtility.DisplayProgressBar("查找文件夹下的Reg", regPaths[i], 1.0f / regPaths.Length * i); string regName = Path.GetFileNameWithoutExtension(regPaths[i]); ExcelToXml(regName); } AssetDatabase.Refresh(); EditorUtility.ClearProgressBar(); } private static void ExcelToXml(string name) { string className = string.Empty; string excelName = string.Empty; string xmlName = string.Empty; Dictionary<string, SheetClass> allSheetClassDic = ReadRegToDic(name, ref excelName, ref xmlName, ref className); string excelPath = ExcelPath + excelName; Dictionary<string,SheetData> sheetDataDic = new Dictionary<string,SheetData>(); try { using(FileStream fs = new FileStream(excelPath,FileMode.Open,FileAccess.Read,FileShare.ReadWrite)) { using(ExcelPackage package = new ExcelPackage(fs)) { 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.AllColName.Add(sheetClass.VarList[n].Name); sheetData.AllType.Add(sheetClass.VarList[n].Type); } for (int m = 1; m < rowCount; m++) { RowData rowData = new(); int n = 0; if (string.IsNullOrEmpty(sheetClass.Split) && sheetClass.ParentVar != null && !string.IsNullOrEmpty(sheetClass.ParentVar.Foreign)) { rowData.ParentValue = worksheet.Cells[m+1,1].Value.ToString().Trim(); n = 1; } for (; n < colCount; n++) { ExcelRange range = worksheet.Cells[m+1, n+1]; string rangeValue = ""; if(range.Value != null) { rangeValue = range.Value.ToString().Trim().Replace(" ", ""); } string colName = worksheet.Cells[1,n+1].Value.ToString().Trim(); string varName = GetNameFormCol(sheetClass.VarList, colName); rowData.RowDataDic.Add(varName,rangeValue);
} sheetData.AllRowData.Add(rowData); } sheetDataDic.Add(worksheet.Name, sheetData); } } } } catch (Exception e) {
Debug.LogError(e.Message); return; } object objClass = CreateClassInstance(className);
List<string> outKeyList = new List<string>(); foreach (string sheetName in allSheetClassDic.Keys) { SheetClass sheetClass = allSheetClassDic[sheetName]; if(sheetClass.Depth == 1) { outKeyList.Add(sheetName); } } for (int i = 0; i < outKeyList.Count; i++) { ReadDataToClass(objClass, allSheetClassDic[outKeyList[i]], sheetDataDic[outKeyList[i]], allSheetClassDic, sheetDataDic, null); } BinarySerializeOpt.XmlSerialize(XmlPath + xmlName, objClass); Debug.Log(excelName + " 表导入unity完成"); AssetDatabase.Refresh(); }
private static string GetNameFormCol(List<VarClass> varList,string col) { foreach (VarClass varClass in varList) { if (varClass.Col == col) { return varClass.Name; } } return null; }
private static void ReadDataToClass(object objClass, SheetClass sheetClass, SheetData sheetData, Dictionary<string, SheetClass> sheetClassDic, Dictionary<string, SheetData> sheetDataDic,object subListMainKey) { object item = CreateClassInstance(sheetClass.Name); object listGeneric = CreateListGenericType(item.GetType());
for (int i = 0; i < sheetData.AllRowData.Count; i++) { if (subListMainKey != null || !string.IsNullOrEmpty(sheetData.AllRowData[i].ParentValue)) { if(sheetData.AllRowData[i].ParentValue != subListMainKey.ToString()) { continue; } } object addItem = CreateClassInstance(sheetClass.Name); for (int j = 0; j < sheetClass.VarList.Count; j++) { VarClass varClass = sheetClass.VarList[j]; if (varClass.Type == "list" && string.IsNullOrEmpty(varClass.Split)) { ReadDataToClass(addItem, sheetClassDic[varClass.ListSheetName], sheetDataDic[varClass.ListSheetName], sheetClassDic, sheetDataDic,GetMemberValue(addItem,sheetClass.MainKey)); } else if(varClass.Type == "list") { string value = sheetData.AllRowData[i].RowDataDic[sheetData.AllColName[j]]; SetSplitCustomClass(addItem, sheetClassDic[varClass.ListSheetName],value); } else if(varClass.Type == "listStr" || varClass.Type == "listFlost" || varClass.Type == "listInt" || varClass.Type == "listBool") { string value = sheetData.AllRowData[i].RowDataDic[sheetData.AllColName[j]]; SetSplitBaseClass(addItem,varClass,value); } else { string value = sheetData.AllRowData[i].RowDataDic[sheetData.AllColName[j]]; if (string.IsNullOrEmpty(value) && !string.IsNullOrEmpty(varClass.DefaultValue)) { value = varClass.DefaultValue; } if(string.IsNullOrEmpty(value)) { Debug.LogError(sheetClass.SheetName + " 表格中有空数据,或者Reg文件未配置defaultValue"); continue; } PropertyInfo propertyInfo = addItem.GetType().GetProperty(sheetData.AllColName[j]); string type = sheetData.AllType[j]; SetValue(propertyInfo, addItem, value, type); } } listGeneric.GetType().InvokeMember("Add",BindingFlags.Default | BindingFlags.InvokeMethod,null,listGeneric,new object[] {addItem}); } objClass.GetType().GetProperty(sheetClass.ParentVar.Name).SetValue(objClass, listGeneric);
} private static void SetSplitBaseClass(object item,VarClass varClass,string value) { Type type = null; if (varClass.Type == "listStr") { type = typeof(string); } else if (varClass.Type == "listFloat") { type= typeof(float); } else if (varClass.Type == "listInt") { type=typeof(int); } else if (varClass.Type == "listBool") { type = typeof(bool); } object list = CreateListGenericType(type); string[] rowArray = value.Split(new string[] {varClass.Split},StringSplitOptions.None); for (int i = 0; i < rowArray.Length; i++) { object addItem = rowArray[i].Trim(); try { list.GetType().InvokeMember("Add",BindingFlags.Default|BindingFlags.InvokeMethod,null,list,new object[] {addItem});
} catch (Exception) { Debug.Log(varClass.ListSheetName + " 里" + varClass.Name + " 列表添加失败!具体数值是:" + addItem); } } item.GetType().GetProperty(varClass.Name).SetValue(item, list); } private static void SetSplitCustomClass(object item, SheetClass sheetClass, string value) { object itemForType = CreateClassInstance(sheetClass.Name); object list = CreateListGenericType(itemForType.GetType());
if (string.IsNullOrEmpty(value)) { Debug.Log("Excel里面自定义List的列里有空值!" + sheetClass.Name); return; } else {
string listsSplit = sheetClass.ParentVar.Split.Replace("\\n","\n").Replace("\\r","\r"); string varSplit = sheetClass.Split; string[] rowArray = value.Split(new string[] {listsSplit},StringSplitOptions.None); for (int i = 0; i < rowArray.Length; i++) { object addItem = CreateClassInstance(sheetClass.Name); string[] valueList = rowArray[i].Trim().Split(new string[] {varSplit},StringSplitOptions.None); for (int j = 0; j < valueList.Length; j++) { SetValue(addItem.GetType().GetProperty(sheetClass.VarList[j].Name), addItem, valueList[j].Trim(), sheetClass.VarList[j].Type); } list.GetType().InvokeMember("Add",BindingFlags.Default|BindingFlags.InvokeMethod,null,list,new object[] {addItem}); } } item.GetType().GetProperty(sheetClass.ParentVar.Name).SetValue(item, list); }
|