/// <summary> /// 将某张Excel表格转换为lua table内容保存到文件 /// </summary> public static bool SaveTxtFile(Export export, string excelName, string sheetName) { try { string exportDirectoryPath = FileModule.GetExportDirectoryPath(excelName, export.ExportPath, export.IsExportKeepDirectoryStructure, false); //如果文件夹不存在就创建 if (Directory.Exists(exportDirectoryPath) == false) { Directory.CreateDirectory(exportDirectoryPath); } //if (sheetName.StartsWith("'")) // sheetName = sheetName.Substring(1); //if (sheetName.EndsWith("'")) // sheetName = sheetName.Substring(0, sheetName.Length - 1); //if (sheetName.EndsWith("$")) // sheetName = sheetName.Substring(0, sheetName.Length - 1); string fileName2 = string.Concat(excelName + "-" + sheetName, ".", export.ExportExtension); string savePath = FileModule.CombinePath(exportDirectoryPath, fileName2); StreamWriter writer = new StreamWriter(savePath, false, new UTF8Encoding(false)); writer.Write(export.ExportContent); writer.Flush(); writer.Close(); return(true); } catch { return(false); } }
/// <summary> /// 将某张Excel表格转换为lua table内容保存到文件 /// </summary> public static bool SaveErlangFile(string excelName, string tableName, string content) { try { //if (ErlangStruct.IsBeforeNameAddtb == true) //{ // if (!fileName.StartsWith("tb_")) // fileName = string.Concat("tb_", fileName); //} string exportDirectoryPath = FileModule.GetExportDirectoryPath(excelName, ErlangStruct.SavePath, ErlangStruct.IsExportKeepDirectoryStructure); //如果文件夹不存在就创建 if (Directory.Exists(exportDirectoryPath) == false) { Directory.CreateDirectory(exportDirectoryPath); } string fileName2 = string.Concat(ErlangStruct.ExportNameBeforeAdd + tableName, ".", ErlangStruct.SaveExtension); string savePath = FileModule.CombinePath(exportDirectoryPath, fileName2); StreamWriter writer = new StreamWriter(savePath, false, new UTF8Encoding(false)); writer.Write(content); writer.Flush(); writer.Close(); return(true); } catch { return(false); } }
/// <summary> /// /// </summary> /// <param name="ExcelName">excel名,如item-道具</param> /// <returns></returns> public bool SaveFile(string tableName) { try { if (IsExportKeepDirectoryStructure == true) { ExportPath = FileModule.GetExportDirectoryPath(tableName, ExportPath, IsExportKeepDirectoryStructure, true); } //如果文件夹不存在就创建 if (Directory.Exists(ExportPath) == false) { Directory.CreateDirectory(ExportPath); } string fileName2 = string.Concat(ExportNameBeforeAdd + ExportName + ExportNameAfterLanguageMark, ".", ExportExtension); string savePath = FileModule.CombinePath(ExportPath, fileName2); StreamWriter writer = new StreamWriter(savePath, false, new UTF8Encoding(false)); writer.Write(ExportContent); writer.Flush(); writer.Close(); return(true); } catch (Exception e) { AppLog.LogErrorAndExit(string.Format("导出失败:{0}", e.ToString())); return(false); } }
public bool SaveFile(string tableName, string excelName, string sheetName) { try { if (IsExportKeepDirectoryStructure == true) { ExportPath = FileModule.GetExportDirectoryPath(tableName, ExportPath, IsExportKeepDirectoryStructure, false); } //如果文件夹不存在就创建 if (Directory.Exists(ExportPath) == false) { Directory.CreateDirectory(ExportPath); } string s = ""; if (excelName.Contains("-")) { s = excelName.Split('-')[1]; } string fileName2 = string.Concat(excelName + "-" + sheetName, ".", ExportExtension); string savePath = FileModule.CombinePath(ExportPath, fileName2); StreamWriter writer = new StreamWriter(savePath, false, new UTF8Encoding(false)); writer.Write(ExportContent); writer.Flush(); writer.Close(); return(true); } catch (Exception e) { AppLog.LogErrorAndExit(string.Format("导出失败:{0}", e.ToString())); return(false); } }
/// <summary> /// 获取最终导出的完整文件路径 /// </summary> /// <param name="ExcelName">excel名,如item-道具</param> /// <returns></returns> private string GetPath(string ExcelName) { // 获取表格相对于Excel文件夹的相对路径 string excelFolderPath = ExcelFolder.ExcelPath.Replace('\\', Path.DirectorySeparatorChar).Replace('/', Path.DirectorySeparatorChar); if (!excelFolderPath.EndsWith(Path.DirectorySeparatorChar.ToString())) { excelFolderPath = excelFolderPath + Path.DirectorySeparatorChar; } Uri excelFolderUri = new Uri(excelFolderPath); Uri fileUri = new Uri(ExcelFolder.ExportTables[ExcelName]); Uri relativeUri = excelFolderUri.MakeRelativeUri(fileUri); // 注意:Uri转为的字符串中会将中文转义为%xx,需要恢复为非转义形式 string relativePath = Uri.UnescapeDataString(relativeUri.ToString()); if (ExcelNameSplitString != null) { string[] SplitRelativePath = relativePath.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries); string[] tempSplitRelativePath; relativePath = ""; for (int i = 0; i < SplitRelativePath.Length - 1; i++) { tempSplitRelativePath = SplitRelativePath[i].Split(new string[] { ExcelNameSplitString }, StringSplitOptions.RemoveEmptyEntries); relativePath = relativePath + tempSplitRelativePath[0] + "/"; } relativePath = relativePath + SplitRelativePath[SplitRelativePath.Length - 1]; } return(Path.GetDirectoryName(FileModule.CombinePath(ExportPath, relativePath))); }
/// <summary> /// 将程序运行中检查出的所有错误保存到文本文件中,存储目录为本工具所在目录 /// </summary> /// <param name="SaveName">日志信息保存时的文件名,系统会在文件名后自动添加保存时的时间</param> /// <returns></returns> public static bool SaveErrorInfoToFile(string SaveName) { try { StringBuilder LogStr = new StringBuilder(); if (IsSaveLog == true) { if (LogContent.Length > 0) { LogStr.Append(LogContent.ToString().Replace("\n", "")); } } if (IsSaveLogWarning == true) { if (LogWarningContent.Length > 0) { LogStr.Append(LogWarningContent.ToString().Replace("\n", System.Environment.NewLine)); } } if (IsSaveLogError == true) { if (LogErrorContent.Length > 0) { LogStr.Append(LogErrorContent.ToString().Replace("\n", System.Environment.NewLine)); } } if (LogStr.Length > 0) { // string fileNameTime = string.Format("{0:yyyy年MM月dd日 HH时mm分ss秒}.txt", DateTime.Now); string fileName = SaveName;// string.Format("{0}{1}", SaveName, fileNameTime); string path1 = AppDomain.CurrentDomain.SetupInformation.ApplicationBase; string savePath = FileModule.CombinePath(path1, fileName); StreamWriter writer = new StreamWriter(savePath, false, new UTF8Encoding(false)); writer.WriteLine(LogStr.ToString().Replace("\n", System.Environment.NewLine)); writer.Flush(); writer.Close(); // Log(string.Format("错误信息已全部导出txt文件,文件名为\"{0}\",存储在本工具所在目录下", fileName), ConsoleColor.Red); } return(true); } catch { LogError("错误信息保存到txt文件时失败"); return(false); } }
/// <summary> /// 创建多语言集合,即将简体文本提出取来待翻译 /// </summary> public static void CreateLanguageDictFile() { if (ExcelFolder.TheLanguage != "" && IsExtractChinese == true) { string MoreLanguagePath = FileModule.CombinePath(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "Language"); string TheLanguageDicPath = FileModule.CombinePath(MoreLanguagePath, "Language" + ExcelFolder.TheLanguage); if (Directory.Exists(TheLanguageDicPath) == false) { Directory.CreateDirectory(TheLanguageDicPath); } string TheLanguageDicFile = FileModule.CombinePath(TheLanguageDicPath, ExcelFolder.TheLanguage + "_dict.txt"); string SourceText = FileModule.CombinePath(TheLanguageDicPath, "SourceText.txt"); StringBuilder content = new StringBuilder(); content.AppendLine("#待翻译的文本:"); content.AppendLine(string.Format("#翻译后,请将文本放入:\n#{0}\n#最末尾,并用Tab键分割。", TheLanguageDicFile)); foreach (string str in SourceTextData) { content.AppendLine(str); } // 保存为txt文件 StreamWriter writer = new StreamWriter(SourceText, false, new UTF8Encoding(false)); writer.Write(content.ToString()); writer.Flush(); writer.Close(); if (!File.Exists(TheLanguageDicFile)) { writer = new StreamWriter(TheLanguageDicFile, false, new UTF8Encoding(false)); writer.Write("原文\t译文\n简体\tEnglish"); writer.Flush(); writer.Close(); } AppLog.Log("文本已提取成功,请尽快拿去翻译,提取出的文件为:", ConsoleColor.Green); AppLog.Log(string.Format("{0}", SourceText), ConsoleColor.Yellow); // AppLog.Log("\n按任意键退出"); #if DEBUG Console.ReadKey(); #endif Environment.Exit(0); } }
/// <summary> /// 将翻译后的字典读取文件中 /// </summary> public static void GetLanguageDictData() { if (ExcelFolder.TheLanguage != "") { string errorString = null; string MoreLanguagePath = FileModule.CombinePath(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "Language");; string TheLanguageDicPath = FileModule.CombinePath(MoreLanguagePath, "Language" + ExcelFolder.TheLanguage); string TheLanguageDicFile = FileModule.CombinePath(TheLanguageDicPath, ExcelFolder.TheLanguage + "_dict.txt"); if (!File.Exists(TheLanguageDicFile)) { return; } LanguageDictData = TxtConfigReader.ParseTxtConfigFile(TheLanguageDicFile, "\t", out errorString); if (!string.IsNullOrEmpty(errorString)) { AppLog.LogErrorAndExit(errorString); } } }
public static bool SaveJsonFile(string excelName, string tableName, string content) { try { //string ExportJsonPath = null; //if (AppValues.ExportJsonPath == null)//如果不存在文件夹就创建 //{ // ExportJsonPath = "ExportJson"; // if (!System.IO.Directory.Exists(ExportJsonPath)) // { // System.IO.Directory.CreateDirectory(ExportJsonPath); // } //} //else //{ // ExportJsonPath = AppValues.ExportJsonPath; //} string exportDirectoryPath = FileModule.GetExportDirectoryPath(excelName, JsonStruct.SavePath, JsonStruct.IsExportKeepDirectoryStructure); //如果文件夹不存在就创建 if (Directory.Exists(exportDirectoryPath) == false) { Directory.CreateDirectory(exportDirectoryPath); } string fileName2 = string.Concat(JsonStruct.ExportNameBeforeAdd + tableName, ".", JsonStruct.SaveExtension); string savePath = FileModule.CombinePath(exportDirectoryPath, fileName2); StreamWriter writer = new StreamWriter(savePath, false, new UTF8Encoding(false)); writer.Write(content); writer.Flush(); writer.Close(); return(true); } catch { return(false); } }
/// <summary> /// 将某张Excel表格转换为lua table内容保存到文件 /// </summary> public static bool SaveHrlFile(string excelName, string tableName, string content) { try { string exportDirectoryPath = FileModule.GetExportDirectoryPath(excelName, HrlStruct.SavePath, HrlStruct.IsExportKeepDirectoryStructure); //如果文件夹不存在就创建 if (Directory.Exists(exportDirectoryPath) == false) { Directory.CreateDirectory(exportDirectoryPath); } string fileName2 = string.Concat(HrlStruct.ExportNameBeforeAdd + tableName, ".", HrlStruct.SaveExtension); string savePath = FileModule.CombinePath(exportDirectoryPath, fileName2); StreamWriter writer = new StreamWriter(savePath, false, new UTF8Encoding(false)); writer.Write(content); writer.Flush(); writer.Close(); return(true); } catch { return(false); } }
private static bool ExportOneMergeTable(TableInfo tableInfo, Export export, out string errorString) { try { errorString = null; // 存储每一行数据生成的txt文件内容 List <StringBuilder> rowContentList = new List <StringBuilder>(); // 生成主键列的同时,对每行的StringBuilder进行初始化,主键列只能为int、long或string型,且值不允许为空,直接转为字符串即可 FieldInfo keyColumnFieldInfo = tableInfo.GetKeyColumnFieldInfo(); int rowCount = keyColumnFieldInfo.Data.Count; for (int row = 0; row < rowCount; ++row) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append(keyColumnFieldInfo.Data[row]); rowContentList.Add(stringBuilder); } // 生成其他列的内容(将array、dict这样的集合类型下属字段作为独立字段处理) List <FieldInfo> allFieldInfoIgnoreSetDataStructure = tableInfo.GetAllClientFieldInfoIgnoreSetDataStructure(); for (int i = 1; i < allFieldInfoIgnoreSetDataStructure.Count; ++i) { _GetOneFieldTxtContent(allFieldInfoIgnoreSetDataStructure[i], export, rowContentList); } // 要在txt中显示 导出至数据库名字及类型 StringBuilder tempStringBuilder = new StringBuilder(); for (int i = 0; i < allFieldInfoIgnoreSetDataStructure.Count; ++i) { tempStringBuilder.Append(export.ExportSpaceString); FieldInfo fieldInfo = allFieldInfoIgnoreSetDataStructure[i]; tempStringBuilder.Append(fieldInfo.DatabaseInfoString); } // 去掉开头多加的一个分隔符 rowContentList.Insert(0, tempStringBuilder.Remove(0, 1)); // 要在txt中显示 声明字段检查字符串 StringBuilder tempStringBuilder2 = new StringBuilder(); for (int i = 0; i < allFieldInfoIgnoreSetDataStructure.Count; ++i) { tempStringBuilder2.Append(export.ExportSpaceString); FieldInfo fieldInfo = allFieldInfoIgnoreSetDataStructure[i]; tempStringBuilder2.Append(fieldInfo.CheckRule); } // 去掉开头多加的一个分隔符 rowContentList.Insert(0, tempStringBuilder2.Remove(0, 1)); // 要在txt中显示 Lua等客户端数据类型 StringBuilder tempStringBuilder3 = new StringBuilder(); for (int i = 0; i < allFieldInfoIgnoreSetDataStructure.Count; ++i) { tempStringBuilder3.Append(export.ExportSpaceString); FieldInfo fieldInfo = allFieldInfoIgnoreSetDataStructure[i]; tempStringBuilder3.Append(fieldInfo.DataType); } // 去掉开头多加的一个分隔符 rowContentList.Insert(0, tempStringBuilder3.Remove(0, 1)); StringBuilder tempStringBuilder4 = new StringBuilder(); for (int i = 0; i < allFieldInfoIgnoreSetDataStructure.Count; ++i) { tempStringBuilder4.Append(export.ExportSpaceString); FieldInfo fieldInfo = allFieldInfoIgnoreSetDataStructure[i]; // 如果是array下属的子元素,字段名生成格式为“array字段名[从1开始的下标序号]”。dict下属的子元素,生成格式为“dict字段名.下属字段名” if (fieldInfo.ParentField != null) { String fieldName = fieldInfo.FieldName; FieldInfo tempField = fieldInfo; while (tempField.ParentField != null) { if (tempField.ParentField.DataType == DataType.Array) { fieldName = string.Concat(tempField.ParentField.FieldName, fieldName); } else if (tempField.ParentField.DataType == DataType.Dict) { fieldName = string.Concat(tempField.ParentField.FieldName, ".", fieldName); } tempField = tempField.ParentField; } tempStringBuilder4.Append(fieldName); } else { tempStringBuilder4.Append(fieldInfo.FieldName); } } // 去掉开头多加的一个分隔符 rowContentList.Insert(0, tempStringBuilder4.Remove(0, 1)); // 要在txt中显示 中文字段名 StringBuilder tempStringBuilder5 = new StringBuilder(); for (int i = 0; i < allFieldInfoIgnoreSetDataStructure.Count; ++i) { tempStringBuilder5.Append(export.ExportSpaceString); FieldInfo fieldInfo = allFieldInfoIgnoreSetDataStructure[i]; tempStringBuilder5.Append(fieldInfo.Desc.Replace("\n", "\\n")); } // 去掉开头多加的一个分隔符 rowContentList.Insert(0, tempStringBuilder5.Remove(0, 1)); StringBuilder content = new StringBuilder(); foreach (StringBuilder stringBuilder in rowContentList) { content.AppendLine(stringBuilder.ToString()); } if (export.ExportPath == null || export.ExportPath == "") { export.ExportPath = Path.GetDirectoryName(tableInfo.ExcelFilePath); } string s = AppValues.MergeTableList[tableInfo.TableName][0].TableName; string exportDirectoryPath = FileModule.GetExportDirectoryPath(s, export.ExportPath, export.IsExportKeepDirectoryStructure, false); //如果文件夹不存在就创建 if (Directory.Exists(exportDirectoryPath) == false) { Directory.CreateDirectory(exportDirectoryPath); } //string xname = tableInfo.TableName; //if(ExcelFolder.TheLanguage !="") //{ // foreach (TableInfo tab in AppValues.MergeTableList[tableInfo.TableName]) // { // if (ExcelMethods.GetTableName(tab.ExcelName).EndsWith(ExcelFolder.TheLanguage)) // { // xname = tableInfo.TableName + ExcelFolder.TheLanguage; // } // } //} string fileName2 = string.Concat(tableInfo.TableName + ExcelFolder.TheLanguage, ".", export.ExportExtension); string savePath = FileModule.CombinePath(exportDirectoryPath, fileName2); StreamWriter writer = new StreamWriter(savePath, false, new UTF8Encoding(false)); writer.Write(content); writer.Flush(); writer.Close(); AppLog.Log(string.Format("成功导出合并TXT:{0}", fileName2)); errorString = null; return(true); } catch { errorString = string.Format("[合并]表{0}的{1}保存为txt文件失败\n", tableInfo.ExcelName, tableInfo.TableConfigData2); return(false); } }
/// <summary> /// 用于检查string型的文件路径对应的文件是否存在 /// </summary> public static bool CheckFile(FieldInfo fieldInfo, FieldCheckRule checkRule, out string errorString) { // 先判断是否输入了Client目录的路径 if (AppValues.App_Config_ClientPath == null) { errorString = "文件存在性检查无法进行:必须在程序运行时传入Client目录的路径\n"; return(false); } else if (fieldInfo.DataType == DataType.String) { int colonIndex = checkRule.CheckRuleString.IndexOf(":"); if (colonIndex == -1) { errorString = "文件存在性检查定义错误:必须在英文冒号后声明相对于Client目录的路径\n"; return(false); } else { // 存储不存在的文件信息(key:行号, value:输入的文件名) Dictionary <int, string> inexistFileInfo = new Dictionary <int, string>(); // 存储含有\或/的非法文件名信息 List <int> illegalFileNames = new List <int>(); // 判断规则中填写的文件的路径与Client路径组合后是否为一个已存在路径 string inputPath = checkRule.CheckRuleString.Substring(colonIndex + 1, checkRule.CheckRuleString.Length - colonIndex - 1).Trim(); string pathString = FileModule.CombinePath(AppValues.App_Config_ClientPath, inputPath); if (!Directory.Exists(pathString)) { errorString = string.Format("文件存在性检查定义错误:声明的文件所在目录不存在,请检查定义的路径是否正确,最终拼得的路径为{0}\n", pathString); return(false); } // 提取file和冒号之间的字符串,判断是否声明扩展名 const string START_STRING = "file"; string extensionString = checkRule.CheckRuleString.Substring(START_STRING.Length, colonIndex - START_STRING.Length).Trim(); // 如果声明了扩展名,则遍历出目标目录下所有该扩展名的文件,然后逐行判断文件是否存在 if (!string.IsNullOrEmpty(extensionString)) { if (extensionString.StartsWith("(") && extensionString.EndsWith(")")) { // 提取括号中定义的扩展名 string extension = extensionString.Substring(1, extensionString.Length - 2).Trim(); // 判断扩展名是否合法(只能为数字或小写英文字母) foreach (char c in extension) { if (!((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9'))) { errorString = string.Format("文件存在性检查定义错误:声明文件扩展名不合法,文件扩展名只能由小写英文字母和数字组成,你填写的为{0}\n", extension); return(false); } } // 存储该目录下为该扩展名的文件的文件名 Dictionary <string, bool> existFileNames = new Dictionary <string, bool>(); DirectoryInfo folder = new DirectoryInfo(pathString); foreach (FileInfo file in folder.GetFiles("*." + extension)) { // 注意file.Name中包含扩展名,需要除去 int dotIndex = file.Name.LastIndexOf('.'); string fileNameWithoutExtension = file.Name.Substring(0, dotIndex); existFileNames.Add(fileNameWithoutExtension, true); } for (int i = 0; i < fieldInfo.Data.Count; ++i) { // 忽略无效集合元素下属子类型的空值 if (fieldInfo.Data[i] == null) { continue; } // 文件名中不允许含有\或/,即不支持文件在填写路径的非同级目录 string inputFileName = fieldInfo.Data[i].ToString().Trim(); if (string.IsNullOrEmpty(inputFileName)) { continue; } else if (inputFileName.IndexOf('\\') != -1 || inputFileName.IndexOf('/') != -1) { illegalFileNames.Add(i); } else { if (!existFileNames.ContainsKey(inputFileName)) { inexistFileInfo.Add(i, inputFileName); } } } } else { errorString = "文件存在性检查定义错误:如果要声明扩展名,\"file\"与英文冒号之间必须在英文括号内声明扩展名\n"; return(false); } } // 如果没有声明扩展名,则每行数据都用File.Exists判断是否存在 else { for (int i = 0; i < fieldInfo.Data.Count; ++i) { // 忽略无效集合元素下属子类型的空值 if (fieldInfo.Data[i] == null) { continue; } // 文件名中不允许含有\或/,即不支持文件在填写路径的非同级目录 string inputFileName = fieldInfo.Data[i].ToString().Trim(); if (string.IsNullOrEmpty(inputFileName)) { continue; } else if (inputFileName.IndexOf('\\') != -1 || inputFileName.IndexOf('/') != -1) { illegalFileNames.Add(i); } else { string path = FileModule.CombinePath(pathString, inputFileName); if (!File.Exists(path)) { inexistFileInfo.Add(i, inputFileName); } } } } if (inexistFileInfo.Count > 0 || illegalFileNames.Count > 0) { StringBuilder errorStringBuild = new StringBuilder(); if (illegalFileNames.Count > 0) { errorStringBuild.Append("单元格中填写的文件名中不允许含有\"\\\"或\"/\",即要求填写的文件必须在填写路径的同级目录,以下行对应的文件名不符合此规则:"); string separator = ", "; foreach (int lineNum in illegalFileNames) { errorStringBuild.AppendFormat("{0}{1}", lineNum + ExcelTableSetting.DataFieldDataStartRowIndex + 1, separator); } // 去掉末尾多余的", " errorStringBuild.Remove(errorStringBuild.Length - separator.Length, separator.Length); errorStringBuild.Append("\n"); } if (inexistFileInfo.Count > 0) { errorStringBuild.AppendLine("存在以下找不到的文件:"); foreach (var item in inexistFileInfo) { errorStringBuild.AppendFormat("第{0}行数据,填写的文件名为{1}\n", item.Key + ExcelTableSetting.DataFieldDataStartRowIndex + 1, item.Value); } } errorString = errorStringBuild.ToString(); return(false); } else { errorString = null; return(true); } } } else { errorString = string.Format("文件存在性检查定义只能用于string型的字段,而该字段为{0}型\n", fieldInfo.DataType); return(false); } }