/// <summary> /// 通过excel生成class /// </summary> /// <param name="filename"></param> static private void GenClassByExcel(string filename) { var excel = new ExcelUtility(filename); string json = excel.GetJson(); List <object> statements = excel.GetLine(0); //这里将前三列进行判断 //如果第二列第一行是"Id",则用自动推测字段模式 //如果第三列第一行是"Id",则用自定义字段类型模式 var list = excel.GetLine(1); var list2 = excel.GetLine(2); if (list[0].Equals("Id")) { Json2Class(filename, json, statements, null); Debug.Log("[自动分析]导出:" + filename); } else if (list2[0].Equals("Id")) { List <object> fieldTypes = excel.GetLine(1); Json2Class(filename, json, statements, fieldTypes); Debug.Log("[自定义字段]导出:" + filename); } else { Debug.LogError("不符合规范内容:" + filename); } }
public static void SingleExcel2Class() { string path = AssetDatabase.GetAssetPath(Selection.activeObject); FileInfo file = new FileInfo(path); string fname = Path.GetFileNameWithoutExtension(file.FullName).ToLower(); fname = UpperFirst(fname); string destPath = Path.GetDirectoryName(file.FullName) + "\\" + fname + Path.GetExtension(file.FullName); // //判断是否重名 string oldPath = "Assets" + file.FullName.Replace('\\', '/').Replace(Application.dataPath, ""); string newPath = "Assets" + destPath.Replace('\\', '/').Replace(Application.dataPath, ""); if (!oldPath.Equals(newPath)) { AssetDatabase.CopyAsset(oldPath, newPath); } AssetDatabase.Refresh(); string f = file.FullName; var excel = new ExcelUtility(f); var json = excel.GetJson(); var statements = excel.GetLine(0); Json2Class(f, json, statements); Debug.Log("导出:" + f); EditorUtility.DisplayDialog("提示", "生成完成!", "确定"); AssetDatabase.Refresh(); }
public static void GenCode() { var tablePath = Path.Combine(Application.dataPath, "Resource/Table"); DirectoryInfo info = new DirectoryInfo(tablePath); foreach (var file in info.GetFiles()) { if (!file.FullName.ToLower().EndsWith("xlsx") && !file.FullName.ToLower().EndsWith("xls")) { continue; } string fname = Path.GetFileNameWithoutExtension(file.FullName).ToLower(); fname = UpperFirst(fname); string destPath = Path.GetDirectoryName(file.FullName) + "\\" + fname + Path.GetExtension(file.FullName); // //判断是否重名 string oldPath = "Assets" + file.FullName.Replace('\\', '/').Replace(Application.dataPath, ""); string newPath = "Assets" + destPath.Replace('\\', '/').Replace(Application.dataPath, ""); if (!oldPath.Equals(newPath)) { AssetDatabase.CopyAsset(oldPath, newPath); } } AssetDatabase.Refresh(); var tableDir = Path.GetDirectoryName(tablePath); var xlslFiles = Directory.GetFiles(tablePath, "*.xlsx", SearchOption.AllDirectories); if (xlslFiles.Length == 0) { EditorUtility.DisplayDialog("提示", "未发现xlsx文件,请注意不是xls", "确定"); return; } foreach (var f in xlslFiles) { var excel = new ExcelUtility(f); var json = excel.GetJson(); var statements = excel.GetLine(0); Json2Class(f, json, statements); Debug.Log("导出:" + f); } EditorUtility.DisplayDialog("提示", "生成完成!", "确定"); AssetDatabase.Refresh(); }