diff --git a/ExcelToLuaJson.zip b/ExcelToLuaJson.zip deleted file mode 100644 index e561fee..0000000 Binary files a/ExcelToLuaJson.zip and /dev/null differ diff --git a/Export.cs b/Export.cs index f93da3a..fb3d114 100644 --- a/Export.cs +++ b/Export.cs @@ -12,27 +12,27 @@ class Export private string _ExportExcelFileName = ""; private ExcelTableData excelTable; public Export(string excelFileName) - { + { _ExportExcelFileName = excelFileName; excelTable = new ExcelTableData(); excelTable.LoadFromFile(excelFileName); } - public void DoExport(string exportBasePath,string exportMode,string exportType) + public void DoExport(string exportBasePath, string exportMode, string exportType) { ExporterBase exporter = null; switch (exportType) { case "lua": { - exporter = new ExporterLua(); + exporter = new ExporterLua(exportMode); } break; case "json": { - exporter = new ExporterJson(); + exporter = new ExporterJson(exportMode); } - break; + break; } @@ -44,6 +44,6 @@ public void DoExport(string exportBasePath,string exportMode,string exportType) } - + } } diff --git a/Exporter/ExporterBase.cs b/Exporter/ExporterBase.cs index 6dfd475..7de578d 100644 --- a/Exporter/ExporterBase.cs +++ b/Exporter/ExporterBase.cs @@ -10,13 +10,18 @@ namespace ExcelExport.Exporter { public class ExporterBase { + private string _exportMode; + protected ExporterBase(string exportMode) + { + this._exportMode = exportMode; + } - protected virtual void ExportTiny(ExcelSheetData data,StreamWriter writer) + protected virtual void ExportTiny(ExcelSheetData data, StreamWriter writer) { //nothing } - protected virtual void ExportBase(ExcelSheetData data,StreamWriter writer) + protected virtual void ExportBase(ExcelSheetData data, StreamWriter writer, string exportMode) { //nothing } @@ -39,12 +44,12 @@ private void CheckCreateDir(string dir) { var targetDir = Directory.GetParent(dir).ToString(); if (!Directory.Exists(targetDir)) - { + { Directory.CreateDirectory(targetDir); } } - public void SaveToFile(ExcelSheetData data , string fileName) + public void SaveToFile(ExcelSheetData data, string fileName) { CheckCreateDir(fileName); @@ -52,16 +57,17 @@ public void SaveToFile(ExcelSheetData data , string fileName) var stream = new FileStream(fileName, FileMode.Create); var writer = new StreamWriter(stream); - AddHeader(data, writer); if (data.exportSchema == "base") { - ExportBase(data, writer); - } else if(data.exportSchema == "tiny") + AddHeader(data, writer); + ExportBase(data, writer, this._exportMode); + AddEnd(data, writer); + } + else if (data.exportSchema == "tiny") { ExportTiny(data, writer); } - AddEnd(data, writer); writer.Close(); stream.Close(); diff --git a/Exporter/ExporterJson.cs b/Exporter/ExporterJson.cs index 29c91d5..7f1ed81 100644 --- a/Exporter/ExporterJson.cs +++ b/Exporter/ExporterJson.cs @@ -9,15 +9,17 @@ namespace ExcelExport.Exporter { public class ExporterJson : ExporterBase { - + public ExporterJson(string exportMode) : base(exportMode) + { + } protected override void AddHeader(ExcelSheetData data, StreamWriter writer) { - writer.WriteLine("{"); + writer.WriteLine("["); } protected override void AddEnd(ExcelSheetData data, StreamWriter writer) { - writer.WriteLine("}"); + writer.WriteLine("]"); } protected override void ExportTiny(ExcelSheetData data, StreamWriter writer) @@ -30,23 +32,42 @@ protected override void ExportTiny(ExcelSheetData data, StreamWriter writer) for (var rowCount = 0; rowCount < fieldDataKey.dataList.Count; rowCount++) { string str = ""; - str = string.Format("{0}:\"{1}\"",fieldDataKey.dataList[rowCount], fieldDataValue.dataList[rowCount]); + int intRes = -1; + double doubleRes = 0; + string value = fieldDataValue.dataList[rowCount]; + if (int.TryParse(value, out intRes)) + { + str = string.Format("\"{0}\":{1}", fieldDataKey.dataList[rowCount], intRes); + } + else if (double.TryParse(value, out doubleRes)) + { + str = string.Format("\"{0}\":{1}", fieldDataKey.dataList[rowCount], doubleRes); + } + else + { + if (value.StartsWith("[") && value.EndsWith("]")) + { + str = string.Format("\"{0}\":{1}", fieldDataKey.dataList[rowCount], value); + } + else + { + str = string.Format("\"{0}\":\"{1}\"", fieldDataKey.dataList[rowCount], value); + } + } - if (rowCount != fieldDataKey.dataList.Count) + if (rowCount != fieldDataKey.dataList.Count - 1) { str += ","; } + writer.WriteLine(str); } - writer.WriteLine("}"); } - protected override void ExportBase(ExcelSheetData data, StreamWriter writer) + protected override void ExportBase(ExcelSheetData data, StreamWriter writer, string exportMode) { FieldData fieldData = null; var rowCount = data.filedList[0].RowCount; - - for (var rowIndex = 0; rowIndex < rowCount; rowIndex++) { @@ -54,14 +75,21 @@ protected override void ExportBase(ExcelSheetData data, StreamWriter writer) string key = ""; for (var i = 0; i < data.keyCount; i++) { - key += string.Format("\"{0}\" : ", data.filedList[i].dataList[rowIndex]) + "{"; + //key += string.Format("\"{0}\" : ", data.filedList[i].dataList[rowIndex]) + "{"; + key += "{"; } - writer.WriteLine(key); + string appendStr = (key + "\n"); for (var i = 0; i < data.filedList.Count; i++) { fieldData = data.filedList[i]; + bool isAppendEndStr = true; + if (!fieldData.CanExportTo(exportMode)) + { + continue; + } + //数组列表 if (fieldData.IsArrayField()) { @@ -79,42 +107,59 @@ protected override void ExportBase(ExcelSheetData data, StreamWriter writer) str += "]"; - writer.Write(str); + appendStr += str; } else { + var tempData = fieldData.dataList[rowIndex]; //普通字段 if (fieldData.fieldType == typeof(string)) { - string str = string.Format(" \"{0}\":\"{1}\"", fieldData.fieldName, fieldData.dataList[rowIndex]); - writer.Write(str); + string str; + if ((tempData.StartsWith("{") && tempData.EndsWith("}")) || (tempData.StartsWith("[") && tempData.EndsWith("]"))) + { + str = string.Format(" \"{0}\":{1}", fieldData.fieldName, tempData); + } + else + { + str = string.Format(" \"{0}\":\"{1}\"", fieldData.fieldName, tempData); + } + appendStr += str; } else { - string str = string.Format(" \"{0}\":{1}", fieldData.fieldName, fieldData.dataList[rowIndex]); - writer.Write(str); + if (tempData != "") + { + string str = string.Format(" \"{0}\":{1}", fieldData.fieldName, tempData); + appendStr += str; + } + else { + isAppendEndStr = false; + } } } - //最后一行不给逗号分隔 - if (i != data.filedList.Count - 1) - { - writer.WriteLine(","); - } - else + if (isAppendEndStr) { - writer.WriteLine(" "); + appendStr += ",\n"; } } - if (rowIndex == rowCount - 1) - { - writer.WriteLine(" }"); - } - else + + appendStr = appendStr.Substring(0, appendStr.Length - 2); + writer.WriteLine(appendStr); + + if (appendStr.Length > 2) { - writer.WriteLine(" },"); + if (rowIndex == rowCount - 1) + { + writer.WriteLine(" }"); + } + else + { + writer.WriteLine(" },"); + } } } diff --git a/Exporter/ExporterLua.cs b/Exporter/ExporterLua.cs index a03b392..779a10a 100644 --- a/Exporter/ExporterLua.cs +++ b/Exporter/ExporterLua.cs @@ -9,6 +9,10 @@ namespace ExcelExport.Exporter { public class ExporterLua : ExporterBase { + public ExporterLua(string exportMode) : base(exportMode) + { + } + protected override void ExportTiny(ExcelSheetData data, StreamWriter writer) { var fieldDataKey = data.filedList[0]; @@ -30,7 +34,7 @@ protected override void ExportTiny(ExcelSheetData data, StreamWriter writer) writer.WriteLine("}"); } - protected override void ExportBase(ExcelSheetData data, StreamWriter writer) + protected override void ExportBase(ExcelSheetData data, StreamWriter writer, string exportMode) { FieldData fieldData = null; var rowCount = data.filedList[0].RowCount; @@ -38,7 +42,7 @@ protected override void ExportBase(ExcelSheetData data, StreamWriter writer) for (var rowIndex = 0; rowIndex < rowCount; rowIndex++) { - + //处理keyCount string key = ""; for (var i = 0; i < data.keyCount; i++) @@ -46,11 +50,17 @@ protected override void ExportBase(ExcelSheetData data, StreamWriter writer) key += string.Format("[{0}] = ", data.filedList[i].dataList[rowIndex]) + "{"; } - writer.WriteLine(key); + string appendStr = key + "\n"; for (var i = 0; i < data.filedList.Count; i++) { - fieldData = data.filedList[i]; + fieldData = data.filedList[i]; + + if (!fieldData.CanExportTo("s")) + { + continue; + } + //数组列表 if (fieldData.IsArrayField()) { @@ -66,7 +76,7 @@ protected override void ExportBase(ExcelSheetData data, StreamWriter writer) } } - writer.Write(str); + appendStr += str; } else { @@ -74,33 +84,30 @@ protected override void ExportBase(ExcelSheetData data, StreamWriter writer) if (fieldData.fieldType == typeof(string)) { string str = string.Format(" {0}=\"{1}\"", fieldData.fieldName, fieldData.dataList[rowIndex]); - writer.Write(str); + appendStr += str; } else { string str = string.Format(" {0}={1}", fieldData.fieldName, fieldData.dataList[rowIndex]); - writer.Write(str); + appendStr += str; } } - //最后一行不给逗号分隔 - if (i != data.filedList.Count - 1) - { - writer.WriteLine(","); - } - else - { - writer.WriteLine(" "); - } + appendStr += ",\n"; } - if(rowIndex == rowCount - 1) + + appendStr = appendStr.Substring(0, appendStr.Length - 2); + writer.WriteLine(appendStr); + + if (rowIndex == rowCount - 1) { writer.WriteLine(" }"); - } else + } + else { writer.WriteLine(" },"); } - + } } diff --git a/FormMain.cs b/FormMain.cs index 773ca49..75db944 100644 --- a/FormMain.cs +++ b/FormMain.cs @@ -74,10 +74,11 @@ private void btn_Export_Click(object sender, EventArgs e) LogUtils.instance.AddLog("读取完成 : " + fileName); LogUtils.instance.AddLog("开始导出到JSON"); - exporter.DoExport(export_path.Text + "\\data\\client\\", "c", "json"); + exporter.DoExport(export_path.Text + "\\client\\", "c", "json"); + exporter.DoExport(export_path.Text + "\\server\\", "s", "json"); - LogUtils.instance.AddLog("开始导出到LUA"); - exporter.DoExport(export_path.Text + "\\data\\server\\", "s", "lua"); + //LogUtils.instance.AddLog("开始导出到LUA"); + //exporter.DoExport(export_path.Text + "\\data\\server\\", "s", "lua"); LogUtils.instance.AddLog("完成处理Excel :" + fileName);