bool SingleConvert(string excelPath, string coding, FormatOption format) { ExcelUtility excel = new ExcelUtility(excelPath); Encoding encoding = Encoding.GetEncoding(coding); bool convertFlag = false; switch (format) { case FormatOption.JSON: convertFlag = excel.ConvertToJson(ExportPath, encoding); break; case FormatOption.XML: convertFlag = excel.ConvertToXml(ExportPath); break; case FormatOption.CVS: convertFlag = excel.ConvertToCSV(ExportPath, encoding); break; default: break; } return(convertFlag); }
/// <summary> /// 转换Excel文件 /// </summary> private static void Convert() { foreach (string assetsPath in excelList) { //获取Excel文件的绝对路径 string excelPath = pathRoot + "/" + assetsPath; //构造Excel工具类 ExcelUtility excel = new ExcelUtility(excelPath); //判断编码类型 Encoding encoding = null; if (indexOfEncoding == 0 || indexOfEncoding == 3) { encoding = Encoding.GetEncoding("utf-8"); } else if (indexOfEncoding == 1) { encoding = Encoding.GetEncoding("gb2312"); } //判断输出类型 string output = ""; if (indexOfFormat == 0) { output = excelPath.Replace(".xlsx", ".json"); excel.ConvertToJson(output, encoding); } else if (indexOfFormat == 1) { output = excelPath.Replace(".xlsx", ".csv"); excel.ConvertToCSV(output, encoding); } else if (indexOfFormat == 2) { output = excelPath.Replace(".xlsx", ".xml"); excel.ConvertToXml(output); } else if (indexOfFormat == 3) { output = excelPath.Replace(".xlsx", ".lua"); excel.ConvertToLua(output, encoding); } //判断是否保留源文件 if (!keepSource) { FileUtil.DeleteFileOrDirectory(excelPath); } //刷新本地资源 AssetDatabase.Refresh(); } //转换完后关闭插件 //这样做是为了解决窗口 //再次点击时路径错误的Bug instance.Close(); }
/// <summary> /// 转换Excel文件 /// </summary> private static void Convert() { foreach (string assetsPath in excelList) { //获取Excel文件的绝对路径 string excelPath = Application.dataPath.Replace("Assets", "") + assetsPath; //构造Excel工具类 ExcelUtility excel = new ExcelUtility(excelPath); //判断编码类型 Encoding encoding = null; if (indexOfEncoding == 0 || indexOfEncoding == 3) { encoding = Encoding.GetEncoding("utf-8"); } else if (indexOfEncoding == 1) { encoding = Encoding.GetEncoding("gb2312"); } //判断输出类型 string output = ""; if (indexOfFormat == 0) { output = excelPath.Replace(".xlsx", ".json"); excel.ConvertToJson(output, encoding); } else if (indexOfFormat == 1) { output = excelPath.Replace(".xlsx", ".csv"); excel.ConvertToCSV(output, encoding); } else if (indexOfFormat == 2) { output = excelPath.Replace(".xlsx", ".xml"); excel.ConvertToXml(output); } else if (indexOfFormat == 3) { output = excelPath.Replace(".xlsx", ".lua"); excel.ConvertToLua(output, encoding); } else if (indexOfFormat == 4) { output = excelPath.Replace(".xlsx", "CSV.cs"); excel.ConvertToCSharp(output, encoding); } //判断是否保留源文件 if (!keepSource) { FileUtil.DeleteFileOrDirectory(excelPath); } //刷新本地资源 AssetDatabase.Refresh(); } }
private static void Convert() { foreach (string assetsPath in excelList) { string excelPath = pathRoot + "/" + assetsPath; ExcelUtility excel = new ExcelUtility(excelPath); //判断输出类型 string outputPath = excelPath.Replace(".xlsx", ".json"); excel.ConvertToJson(outputPath, Encoding.GetEncoding("utf-8")); //是否保留源文件 if (!keepSource) { FileUtil.DeleteFileOrDirectory(excelPath); } AssetDatabase.Refresh(); } //转换后关闭 instance.Close(); }
static void ExcelToJson() { string dataFolderPath = ProjectPathConfig.externalTablePath; string outJsonPath = FilePath.streamingAssetsPath4Config; IO.CheckDirAndCreate(dataFolderPath); string[] allTableFiles = Directory.GetFiles(dataFolderPath, "*.xlsx", SearchOption.AllDirectories); if (allTableFiles == null || allTableFiles.Length <= 0) { Debug.LogError("#No Table Found in" + dataFolderPath); return; } IO.CheckDirAndCreate(outJsonPath); for (int i = 0; i < allTableFiles.Length; i++) { EditorUtility.DisplayProgressBar("Hold on", "Processing", (float)(i + 1) / allTableFiles.Length); if (PathHelper.Path2Name(allTableFiles[i]).StartsWith("~")) { continue; } string dictName = new DirectoryInfo(Path.GetDirectoryName(allTableFiles[i])).Name; string fileName = Path.GetFileNameWithoutExtension(allTableFiles[i]); //构造Excel工具类 ExcelUtility excel = new ExcelUtility(allTableFiles[i]); //判断编码类型 Encoding encoding = Encoding.GetEncoding("utf-8"); //判断输出类型 string output = outJsonPath + fileName + ".json"; excel.ConvertToJson(output, encoding); } EditorUtility.ClearProgressBar(); AssetDatabase.Refresh(); }
/// <summary> /// 转换Excel文件 /// </summary> private static void Convert() { foreach (string assetsPath in excelList) { //获取Excel文件的绝对路径 string excelPath = pathRoot + "/" + assetsPath; //构造Excel工具类 ExcelUtility excel = new ExcelUtility(excelPath); //判断编码类型 Encoding encoding = null; if (indexOfEncoding == 0) { encoding = Encoding.GetEncoding("utf-8"); } else if (indexOfEncoding == 1) { encoding = Encoding.GetEncoding("gb2312"); } ////判断输出类型 //string output=""; //string halfName = excelPath.Substring(excelPath.LastIndexOf("/")+1); //Debug.Log(halfName); // string fileName ="Setting"+ halfName.Replace(".xlsx", ".json"); //output = pathRoot + "/Resources/DataTable/" + fileName; //string excelPath = pathRoot + "/" + s; string output = ""; string halfName = excelPath.Substring(excelPath.LastIndexOf("/") + 1); string SettingName = halfName.Insert(halfName.IndexOf("."), "Setting"); string fileName = SettingName.Replace(".xlsx", ".json"); //output = pathRoot + "/Assets/Resources/DataTable/" + fileName; if (!Directory.Exists(Application.streamingAssetsPath + "/res/DataTable")) { Directory.CreateDirectory(Application.streamingAssetsPath + "/res/DataTable"); } output = Application.streamingAssetsPath + "/res/DataTable/" + fileName; if (indexOfFormat == 0) { //output=excelPath.Replace(".xlsx",".json"); excel.ConvertToJson(output, encoding); } else if (indexOfFormat == 1) { //output=excelPath.Replace(".xlsx",".csv"); //excel.ConvertToCSV(output,encoding); } else if (indexOfFormat == 2) { //output=excelPath.Replace(".xlsx",".xml"); //excel.ConvertToXml(output); } //判断是否保留源文件 if (!keepSource) { FileUtil.DeleteFileOrDirectory(excelPath); } //刷新本地资源 AssetDatabase.Refresh(); } //转换完后关闭插件 //这样做是为了解决窗口 //再次点击时路径错误的Bug if (instance != null) { instance.Close(); } //提示框 UnityEditor.EditorUtility.DisplayDialog("提示", "转换成功", "确定"); }
/// <summary> /// 转换Excel文件 /// </summary> private static void Convert() { foreach(string assetsPath in excelList) { //获取Excel文件的绝对路径 string excelPath=pathRoot + "/" + assetsPath; //构造Excel工具类 ExcelUtility excel=new ExcelUtility(excelPath); //判断编码类型 Encoding encoding=null; if(indexOfEncoding==0){ encoding=Encoding.GetEncoding("utf-8"); }else if(indexOfEncoding==1){ encoding=Encoding.GetEncoding("gb2312"); } //判断输出类型 string output=""; if(indexOfFormat==0){ output=excelPath.Replace(".xlsx",".json"); excel.ConvertToJson(output,encoding); }else if(indexOfFormat==1){ output=excelPath.Replace(".xlsx",".csv"); excel.ConvertToCSV(output,encoding); }else if(indexOfFormat==2){ output=excelPath.Replace(".xlsx",".xml"); excel.ConvertToXml(output); } //判断是否保留源文件 if(!keepSource) { FileUtil.DeleteFileOrDirectory(excelPath); } //刷新本地资源 AssetDatabase.Refresh(); } //转换完后关闭插件 //这样做是为了解决窗口 //再次点击时路径错误的Bug instance.Close(); }
/// <summary> /// 转换Excel文件 /// </summary> private static void Convert() { PlayerPrefs.SetString("sourcePath", sourcePath); PlayerPrefs.SetString("targetPath", targetPath); //D:/ExcalToTxt/Assets/sourcefile/Pass.xlsx //"D:/ExcalToTxt/Assets/sourcefilePass.xlsx" DirectoryInfo direction = new DirectoryInfo(sourcePath); FileInfo[] alldatatabletext = direction.GetFiles(); for (int i = 0; i < alldatatabletext.Length; i++) { if (alldatatabletext[i].Name.EndsWith(".xlsx")) { //获取Excel文件的绝对路径 string excelPath = sourcePath +"/"+ alldatatabletext[i].Name; string txtPath = targetPath + "/" + alldatatabletext[i].Name; //构造Excel工具类 ExcelUtility excel = new ExcelUtility(excelPath); //判断编码类型 Encoding encoding = null; if (indexOfEncoding == 0) { encoding = Encoding.GetEncoding("utf-8"); } else if (indexOfEncoding == 1) { encoding = Encoding.GetEncoding("gb2312"); } //判断输出类型 string output = ""; if (indexOfFormat == 0) { output = txtPath.Replace(".xlsx", ".txt"); excel.ConvertToCSV(output, encoding); } else if (indexOfFormat == 1) { output = txtPath.Replace(".xlsx", ".json"); excel.ConvertToJson(output, encoding); } else if (indexOfFormat == 2) { output = txtPath.Replace(".xlsx", ".xml"); excel.ConvertToXml(output); } //判断是否保留源文件 if (!keepSource) { FileUtil.DeleteFileOrDirectory(excelPath); } //刷新本地资源 AssetDatabase.Refresh(); } } /*foreach (string assetsPath in excelList) { //获取Excel文件的绝对路径 string excelPath = pathRoot + "/" + assetsPath; //构造Excel工具类 ExcelUtility excel = new ExcelUtility(excelPath); //判断编码类型 Encoding encoding = null; if (indexOfEncoding == 0) { encoding = Encoding.GetEncoding("utf-8"); } else if (indexOfEncoding == 1) { encoding = Encoding.GetEncoding("gb2312"); } //判断输出类型 string output = ""; if (indexOfFormat == 0) { output = excelPath.Replace(".xlsx", ".json"); excel.ConvertToJson(output, encoding); } else if (indexOfFormat == 1) { output = excelPath.Replace(".xlsx", ".txt"); excel.ConvertToCSV(output, encoding); } else if (indexOfFormat == 2) { output = excelPath.Replace(".xlsx", ".xml"); excel.ConvertToXml(output); } //判断是否保留源文件 if (!keepSource) { FileUtil.DeleteFileOrDirectory(excelPath); } //刷新本地资源 AssetDatabase.Refresh(); }*/ //转换完后关闭插件 //这样做是为了解决窗口 //再次点击时路径错误的Bug //instance.Close(); }