支持同一工作簿内多个sheet导出,跳过#开头的sheet
This commit is contained in:
@@ -7,9 +7,9 @@ using ExcelTool.Parser;
|
|||||||
|
|
||||||
namespace ExcelTool
|
namespace ExcelTool
|
||||||
{
|
{
|
||||||
public class ExcelHelper
|
public static class ExcelHelper
|
||||||
{
|
{
|
||||||
public static List<TableExcelHeader> ExcelHeaders(string fileName)
|
public static List<TableExcelHeader> ExcelHeaders(string fileName, out string sheetName, out int sheetCount, int sheetNum = 0)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -17,7 +17,16 @@ namespace ExcelTool
|
|||||||
|
|
||||||
using FileStream fs = File.OpenRead(fileName);
|
using FileStream fs = File.OpenRead(fileName);
|
||||||
IWorkbook wk = new XSSFWorkbook(fs);
|
IWorkbook wk = new XSSFWorkbook(fs);
|
||||||
ISheet sheet = wk.GetSheetAt(0);
|
|
||||||
|
sheetCount = wk.NumberOfSheets;
|
||||||
|
if (sheetNum >= sheetCount)
|
||||||
|
{
|
||||||
|
sheetName = "";
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
ISheet sheet = wk.GetSheetAt(sheetNum);
|
||||||
|
sheetName = sheet.SheetName;
|
||||||
|
|
||||||
IRow nameRow = sheet.GetRow(0); // 字段名
|
IRow nameRow = sheet.GetRow(0); // 字段名
|
||||||
IRow typeRow = sheet.GetRow(1); // 类型
|
IRow typeRow = sheet.GetRow(1); // 类型
|
||||||
@@ -39,7 +48,7 @@ namespace ExcelTool
|
|||||||
{
|
{
|
||||||
FieldName = fieldName,
|
FieldName = fieldName,
|
||||||
FieldType = fieldType,
|
FieldType = fieldType,
|
||||||
FieldDesc = fieldDesc
|
FieldDesc = fieldDesc,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -48,20 +57,27 @@ namespace ExcelTool
|
|||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
ex.ToString().WriteErrorLine();
|
ex.ToString().WriteErrorLine();
|
||||||
|
sheetName = null;
|
||||||
|
sheetCount = 0;
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static TableExcelData ExcelDatas(string fileName)
|
public static TableExcelData ExcelDatas(string fileName, out string sheetName, out int sheetCount, int sheetNum = 0)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var excelHeader = ExcelHeaders(fileName);
|
var excelHeader = ExcelHeaders(fileName, out sheetName, out sheetCount);
|
||||||
var tableRows = new List<TableExcelRow>();
|
var tableRows = new List<TableExcelRow>();
|
||||||
|
|
||||||
using FileStream fs = File.OpenRead(fileName);
|
using FileStream fs = File.OpenRead(fileName);
|
||||||
IWorkbook wk = new XSSFWorkbook(fs);
|
IWorkbook wk = new XSSFWorkbook(fs);
|
||||||
ISheet sheet = wk.GetSheetAt(0);
|
|
||||||
|
if (sheetNum >= sheetCount)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
ISheet sheet = wk.GetSheetAt(sheetNum);
|
||||||
|
|
||||||
for (int i = 6; i <= sheet.LastRowNum; i++)
|
for (int i = 6; i <= sheet.LastRowNum; i++)
|
||||||
{
|
{
|
||||||
@@ -84,6 +100,8 @@ namespace ExcelTool
|
|||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
ex.ToString().WriteErrorLine();
|
ex.ToString().WriteErrorLine();
|
||||||
|
sheetName = null;
|
||||||
|
sheetCount = 0;
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ namespace ExcelTool
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 文件操作类
|
/// 文件操作类
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class FileManager
|
public static class FileManager
|
||||||
{
|
{
|
||||||
public static bool CreateDir(string dirPath)
|
public static bool CreateDir(string dirPath)
|
||||||
{
|
{
|
||||||
|
|||||||
+122
-32
@@ -1,37 +1,49 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace ExcelTool.Parser
|
namespace ExcelTool.Parser
|
||||||
{
|
{
|
||||||
public class GenModels
|
public static class GenModels
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 生成对应的C#Model类
|
/// 生成对应的C#Model类
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="fileName"></param>
|
/// <param name="fileName">传入的文件名</param>
|
||||||
|
/// <param name="outputDir">输出cs文件的路径</param>
|
||||||
|
/// <param name="nameSpace">cs文件要使用的命名空间</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static bool GenCSharpModel(string fileName, string outputDir)
|
public static bool GenCSharpModel(string fileName, string outputDir, string nameSpace = "")
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(fileName))
|
if (string.IsNullOrEmpty(fileName))
|
||||||
{
|
{
|
||||||
ConsoleHelper.WriteErrorLine("GenCSharpModel 参数传递有误");
|
"GenCSharpModel 参数传递有误".WriteErrorLine();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
FileInfo fileInfo = new FileInfo(fileName);
|
FileInfo fileInfo = new(fileName);
|
||||||
if (string.IsNullOrEmpty(outputDir))
|
if (string.IsNullOrEmpty(outputDir))
|
||||||
{
|
{
|
||||||
outputDir = fileInfo.DirectoryName;
|
outputDir = fileInfo.DirectoryName;
|
||||||
}
|
}
|
||||||
var headers = ExcelHelper.ExcelHeaders(fileName);
|
for (int sheetNum = 0; ; sheetNum++)
|
||||||
var excelName = fileInfo.Name.Remove(fileInfo.Name.IndexOf(".xlsx"));
|
{
|
||||||
StringBuilder sb = new StringBuilder();
|
var headers = ExcelHelper.ExcelHeaders(fileName, out string sheetName, out int sheetCount, sheetNum);
|
||||||
sb.Append($"/*\n * auto generated by tools(注意:千万不要手动修改本文件)\n * {excelName}\n */\n");
|
if (headers == null || headers.Count == 0 || sheetName.StartsWith("#") || sheetNum > sheetCount)
|
||||||
sb.Append("using System;\nusing System.IO;\nusing System.Collections.Generic;\nusing System.Text;\n\n");
|
break;
|
||||||
|
StringBuilder sb = new();
|
||||||
|
sb.Append($"/*\n * auto generated by tools(注意:千万不要手动修改本文件)\n * {sheetName}\n */\n");
|
||||||
|
sb.Append("using System;\nusing System.IO;\nusing System.Collections.Generic;\nusing System.Text;\nusing WKMobile.Generated;\n\n");
|
||||||
|
|
||||||
|
|
||||||
|
if (!string.IsNullOrEmpty(nameSpace))
|
||||||
|
{
|
||||||
|
sb.Append($"namespace {nameSpace}\n{{\n");
|
||||||
|
}
|
||||||
|
|
||||||
sb.Append("[Serializable]\n");
|
sb.Append("[Serializable]\n");
|
||||||
sb.Append($"public partial class {excelName} : IBinarySerializable\n");
|
sb.Append($"public partial class {sheetName} : IBinarySerializable\n");
|
||||||
sb.Append("{\n");
|
sb.Append("{\n");
|
||||||
for (int i = 0; i < headers.Count; i++)
|
for (int i = 0; i < headers.Count; i++)
|
||||||
{
|
{
|
||||||
@@ -124,7 +136,6 @@ namespace ExcelTool.Parser
|
|||||||
sb.Append(string.Format("\tpublic {0} {1}", headers[i].FieldType.ToLower(), headers[i].FieldName));
|
sb.Append(string.Format("\tpublic {0} {1}", headers[i].FieldType.ToLower(), headers[i].FieldName));
|
||||||
}
|
}
|
||||||
sb.Append(" { get; set; }\n\n");
|
sb.Append(" { get; set; }\n\n");
|
||||||
|
|
||||||
}
|
}
|
||||||
sb.Append("\n\tpublic void DeSerialize(BinaryReader reader)\n");
|
sb.Append("\n\tpublic void DeSerialize(BinaryReader reader)\n");
|
||||||
sb.Append("\t{\n");
|
sb.Append("\t{\n");
|
||||||
@@ -139,7 +150,8 @@ namespace ExcelTool.Parser
|
|||||||
else if (type.Equals("uint"))
|
else if (type.Equals("uint"))
|
||||||
{
|
{
|
||||||
sb.Append($"\t\t{name} = reader.ReadUInt32();\n");
|
sb.Append($"\t\t{name} = reader.ReadUInt32();\n");
|
||||||
}else if (type.Equals("short"))
|
}
|
||||||
|
else if (type.Equals("short"))
|
||||||
{
|
{
|
||||||
sb.Append($"\t\t{name} = reader.ReadInt16();\n");
|
sb.Append($"\t\t{name} = reader.ReadInt16();\n");
|
||||||
}
|
}
|
||||||
@@ -220,6 +232,21 @@ namespace ExcelTool.Parser
|
|||||||
sb.Append("\t\t{\n");
|
sb.Append("\t\t{\n");
|
||||||
sb.Append($"\t\t\t{name} = null;\n");
|
sb.Append($"\t\t\t{name} = null;\n");
|
||||||
sb.Append("\t\t}\n");
|
sb.Append("\t\t}\n");
|
||||||
|
}else if (type.Equals("uintlist"))
|
||||||
|
{
|
||||||
|
sb.Append($"\t\tvar {name}Count = reader.ReadInt32();\n");
|
||||||
|
sb.Append($"\t\tif ({name}Count > 0)\n");
|
||||||
|
sb.Append("\t\t{\n");
|
||||||
|
sb.Append($"\t\t\t{name} = new List<uint>();\n");
|
||||||
|
sb.Append($"\t\t\tfor (int i = 0; i < {name}Count; i++)\n");
|
||||||
|
sb.Append("\t\t\t{\n");
|
||||||
|
sb.Append($"\t\t\t\t{name}.Add(reader.ReadUInt32());\n");
|
||||||
|
sb.Append("\t\t\t}\n");
|
||||||
|
sb.Append("\t\t}\n");
|
||||||
|
sb.Append("\t\telse\n");
|
||||||
|
sb.Append("\t\t{\n");
|
||||||
|
sb.Append($"\t\t\t{name} = null;\n");
|
||||||
|
sb.Append("\t\t}\n");
|
||||||
}
|
}
|
||||||
else if (type.Equals("boollist"))
|
else if (type.Equals("boollist"))
|
||||||
{
|
{
|
||||||
@@ -472,6 +499,67 @@ namespace ExcelTool.Parser
|
|||||||
sb.Append("\t\t\t}\n");
|
sb.Append("\t\t\t}\n");
|
||||||
sb.Append("\t\t}\n");
|
sb.Append("\t\t}\n");
|
||||||
}
|
}
|
||||||
|
else if (type.Equals("uintlist"))
|
||||||
|
{
|
||||||
|
sb.Append($"\t\tif ({name} == null || {name}.Count == 0)\n");
|
||||||
|
sb.Append("\t\t{\n");
|
||||||
|
sb.Append("\t\t\twriter.Write(0);\n");
|
||||||
|
sb.Append("\t\t}\n");
|
||||||
|
sb.Append("\t\telse\n");
|
||||||
|
sb.Append("\t\t{\n");
|
||||||
|
sb.Append($"\t\t\twriter.Write({name}.Count);\n");
|
||||||
|
sb.Append($"\t\t\tfor (int i = 0; i < {name}.Count; i++)\n");
|
||||||
|
sb.Append("\t\t\t{\n");
|
||||||
|
sb.Append($"\t\t\t\twriter.Write({name}[i]);\n");
|
||||||
|
sb.Append("\t\t\t}\n");
|
||||||
|
sb.Append("\t\t}\n");
|
||||||
|
}
|
||||||
|
else if (type.Equals("ushortlist"))
|
||||||
|
{
|
||||||
|
sb.Append($"\t\tif ({name} == null || {name}.Count == 0)\n");
|
||||||
|
sb.Append("\t\t{\n");
|
||||||
|
sb.Append("\t\t\twriter.Write(0);\n");
|
||||||
|
sb.Append("\t\t}\n");
|
||||||
|
sb.Append("\t\telse\n");
|
||||||
|
sb.Append("\t\t{\n");
|
||||||
|
sb.Append($"\t\t\twriter.Write({name}.Count);\n");
|
||||||
|
sb.Append($"\t\t\tfor (int i = 0; i < {name}.Count; i++)\n");
|
||||||
|
sb.Append("\t\t\t{\n");
|
||||||
|
sb.Append($"\t\t\t\twriter.Write({name}[i]);\n");
|
||||||
|
sb.Append("\t\t\t}\n");
|
||||||
|
sb.Append("\t\t}\n");
|
||||||
|
}
|
||||||
|
else if (type.Equals("sbytelist"))
|
||||||
|
{
|
||||||
|
sb.Append($"\t\tif ({name} == null || {name}.Count == 0)\n");
|
||||||
|
sb.Append("\t\t{\n");
|
||||||
|
sb.Append("\t\t\twriter.Write(0);\n");
|
||||||
|
sb.Append("\t\t}\n");
|
||||||
|
sb.Append("\t\telse\n");
|
||||||
|
sb.Append("\t\t{\n");
|
||||||
|
sb.Append($"\t\t\twriter.Write({name}.Count);\n");
|
||||||
|
sb.Append($"\t\t\tfor (int i = 0; i < {name}.Count; i++)\n");
|
||||||
|
sb.Append("\t\t\t{\n");
|
||||||
|
sb.Append($"\t\t\t\twriter.Write({name}[i]);\n");
|
||||||
|
sb.Append("\t\t\t}\n");
|
||||||
|
sb.Append("\t\t}\n");
|
||||||
|
}
|
||||||
|
else if (type.Equals("bytelist"))
|
||||||
|
{
|
||||||
|
sb.Append($"\t\tif ({name} == null || {name}.Count == 0)\n");
|
||||||
|
sb.Append("\t\t{\n");
|
||||||
|
sb.Append("\t\t\twriter.Write(0);\n");
|
||||||
|
sb.Append("\t\t}\n");
|
||||||
|
sb.Append("\t\telse\n");
|
||||||
|
sb.Append("\t\t{\n");
|
||||||
|
sb.Append($"\t\t\twriter.Write({name}.Count);\n");
|
||||||
|
sb.Append($"\t\t\tfor (int i = 0; i < {name}.Count; i++)\n");
|
||||||
|
sb.Append("\t\t\t{\n");
|
||||||
|
sb.Append($"\t\t\t\twriter.Write({name}[i]);\n");
|
||||||
|
sb.Append("\t\t\t}\n");
|
||||||
|
sb.Append("\t\t}\n");
|
||||||
|
}
|
||||||
|
|
||||||
else if (type.Contains("list<"))
|
else if (type.Contains("list<"))
|
||||||
{
|
{
|
||||||
var tempS = type.Substring(5);
|
var tempS = type.Substring(5);
|
||||||
@@ -524,17 +612,16 @@ namespace ExcelTool.Parser
|
|||||||
|
|
||||||
sb.Append("\n");
|
sb.Append("\n");
|
||||||
sb.Append("[Serializable]\n");
|
sb.Append("[Serializable]\n");
|
||||||
sb.Append($"public partial class {excelName}Config : IBinarySerializable\n");
|
sb.Append($"public partial class {sheetName}Config : IBinarySerializable\n");
|
||||||
sb.Append("{\n");
|
sb.Append("{\n");
|
||||||
// sb.Append($"\tpublic List<{excelName}> {excelName}Infos = new List<{excelName}>();\n");
|
// sb.Append($"\tpublic List<{excelName}> {excelName}Infos = new List<{excelName}>();\n");
|
||||||
sb.Append($"\tDictionary<int,{excelName}> {excelName}Infos = new Dictionary<int,{excelName}>();\n");
|
sb.Append($"\tDictionary<uint,{sheetName}> m_{sheetName.ToCamelCase()}Infos = new();\n");
|
||||||
sb.Append($"\tList<{excelName}> {excelName}InfoList;\n");
|
sb.Append($"\tList<{sheetName}> m_{sheetName.ToCamelCase()}InfoList;\n");
|
||||||
sb.Append("\n");
|
sb.Append("\n");
|
||||||
sb.Append($"\tpublic List<{excelName}> {excelName}List()\n");
|
sb.Append($"\tpublic List<{sheetName}> {sheetName}List()\n");
|
||||||
sb.Append("\t{\n");
|
sb.Append("\t{\n");
|
||||||
sb.Append($"\t\tif ({excelName}InfoList == null)\n");
|
sb.Append($"\t\tthis.m_{sheetName.ToCamelCase()}InfoList ??= new List<{sheetName}>(m_{sheetName.ToCamelCase()}Infos.Values);\n");
|
||||||
sb.Append($"\t\t\t{excelName}InfoList = new List<{excelName}>({excelName}Infos.Values);\n");
|
sb.Append($"\t\treturn this.m_{sheetName.ToCamelCase()}InfoList;\n");
|
||||||
sb.Append($"\t\treturn {excelName}InfoList;\n");
|
|
||||||
sb.Append("\t}\n");
|
sb.Append("\t}\n");
|
||||||
sb.Append("\n");
|
sb.Append("\n");
|
||||||
sb.Append($"\tpublic void DeSerialize(BinaryReader reader)\n");
|
sb.Append($"\tpublic void DeSerialize(BinaryReader reader)\n");
|
||||||
@@ -542,34 +629,37 @@ namespace ExcelTool.Parser
|
|||||||
sb.Append($"\t\tint count = reader.ReadInt32();\n");
|
sb.Append($"\t\tint count = reader.ReadInt32();\n");
|
||||||
sb.Append($"\t\tfor (int i = 0;i < count; i++)\n");
|
sb.Append($"\t\tfor (int i = 0;i < count; i++)\n");
|
||||||
sb.Append("\t\t{\n");
|
sb.Append("\t\t{\n");
|
||||||
sb.Append($"\t\t\t{excelName} tempData = new {excelName}();\n");
|
sb.Append($"\t\t\t{sheetName} tempData = new();\n");
|
||||||
sb.Append($"\t\t\ttempData.DeSerialize(reader);\n");
|
sb.Append($"\t\t\ttempData.DeSerialize(reader);\n");
|
||||||
// sb.Append($"\t\t\t{excelName}Infos.Add(tempData);\n");
|
// sb.Append($"\t\t\t{excelName}Infos.Add(tempData);\n");
|
||||||
sb.Append($"\t\t\t{excelName}Infos.Add(tempData.Id, tempData);\n");
|
sb.Append($"\t\t\tthis.m_{sheetName.ToCamelCase()}Infos.Add(tempData.Id, tempData);\n");
|
||||||
sb.Append("\t\t}\n");
|
sb.Append("\t\t}\n");
|
||||||
sb.Append("\t}\n");
|
sb.Append("\t}\n");
|
||||||
sb.Append("\n");
|
sb.Append("\n");
|
||||||
sb.Append("\tpublic void Serialize(BinaryWriter writer)\n");
|
sb.Append("\tpublic void Serialize(BinaryWriter writer)\n");
|
||||||
sb.Append("\t{\n");
|
sb.Append("\t{\n");
|
||||||
sb.Append($"\t\twriter.Write({excelName}Infos.Count);\n");
|
sb.Append($"\t\twriter.Write(this.m_{sheetName.ToCamelCase()}Infos.Count);\n");
|
||||||
sb.Append($"\t\tfor (int i = 0; i < {excelName}Infos.Count; i++)\n");
|
sb.Append($"\t\tfor (uint i = 0; i < this.m_{sheetName.ToCamelCase()}Infos.Count; i++)\n");
|
||||||
sb.Append("\t\t{\n");
|
sb.Append("\t\t{\n");
|
||||||
sb.Append($"\t\t\t{excelName}Infos[i].Serialize(writer);\n");
|
sb.Append($"\t\t\tthis.m_{sheetName.ToCamelCase()}Infos[i].Serialize(writer);\n");
|
||||||
sb.Append("\t\t}\n");
|
sb.Append("\t\t}\n");
|
||||||
sb.Append("\t}\n\n");
|
sb.Append("\t}\n\n");
|
||||||
sb.Append($"\tpublic {excelName} QueryById(int id)\n");
|
sb.Append($"\tpublic {sheetName} QueryById(uint id)\n");
|
||||||
sb.Append("\t{\n");
|
sb.Append("\t{\n");
|
||||||
// sb.Append($"\t\tvar datas = from d in {excelName}Infos\n");
|
// sb.Append($"\t\tvar datas = from d in {excelName}Infos\n");
|
||||||
// sb.Append($"\t\t\t\t\twhere d.Id == id\n");
|
// sb.Append($"\t\t\t\t\twhere d.Id == id\n");
|
||||||
// sb.Append($"\t\t\t\t\tselect d;\n");
|
// sb.Append($"\t\t\t\t\tselect d;\n");
|
||||||
// sb.Append("\t\treturn datas.First();\n");
|
// sb.Append("\t\treturn datas.First();\n");
|
||||||
sb.Append($"\t\tif ({excelName}Infos.ContainsKey(id))\n");
|
sb.Append($"\t\treturn this.m_{sheetName.ToCamelCase()}Infos.GetValueOrDefault(id);\n");
|
||||||
sb.Append($"\t\t\treturn {excelName}Infos[id];\n");
|
|
||||||
sb.Append($"\t\telse\n");
|
|
||||||
sb.Append($"\t\t\treturn null;\n");
|
|
||||||
sb.Append("\t}\n");
|
sb.Append("\t}\n");
|
||||||
sb.Append("}\n");
|
sb.Append("}\n");
|
||||||
FileManager.WriteToFile(Path.Combine(outputDir, $"{excelName}.cs"), sb.ToString());
|
|
||||||
|
if (!string.IsNullOrEmpty(nameSpace))
|
||||||
|
{
|
||||||
|
sb.Append("}\n"); // namespace 结束
|
||||||
|
}
|
||||||
|
FileManager.WriteToFile(Path.Combine(outputDir, $"{sheetName}.cs"), sb.ToString());
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
|||||||
@@ -4,22 +4,27 @@ using System.IO;
|
|||||||
|
|
||||||
namespace ExcelTool.Parser
|
namespace ExcelTool.Parser
|
||||||
{
|
{
|
||||||
public class TableExcelExportBytes
|
public static class TableExcelExportBytes
|
||||||
{
|
{
|
||||||
public static bool ExportToFile(string fileName, string outputDir = null)
|
public static bool ExportToFile(string fileName, string outputDir = null)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
FileInfo fileInfo = new FileInfo(fileName);
|
FileInfo fileInfo = new(fileName);
|
||||||
if (string.IsNullOrEmpty(outputDir))
|
if (string.IsNullOrEmpty(outputDir))
|
||||||
{
|
{
|
||||||
outputDir = fileInfo.DirectoryName;
|
outputDir = fileInfo.DirectoryName;
|
||||||
}
|
}
|
||||||
var excelName = fileInfo.Name.Remove(fileInfo.Name.IndexOf(".xlsx"));
|
|
||||||
|
for (int sheetNum = 0; ; sheetNum++)
|
||||||
|
{
|
||||||
|
var tableData = ExcelHelper.ExcelDatas(fileName, out string sheetName, out int sheetCount, sheetNum);
|
||||||
|
if (tableData == null || sheetName.StartsWith($"#") || sheetNum > sheetCount)
|
||||||
|
break;
|
||||||
|
|
||||||
|
List<Tuple<string, string>> datas = new();
|
||||||
//先写入行数,然后每一行的数据一次写入 小写类型、字符串
|
//先写入行数,然后每一行的数据一次写入 小写类型、字符串
|
||||||
List<Tuple<string, string>> datas = new List<Tuple<string, string>>();
|
Tuple<string, string> rowCount = new("int", tableData.RowCounts.ToString());
|
||||||
var tableData = ExcelHelper.ExcelDatas(fileName);
|
|
||||||
Tuple<string, string> rowCount = new Tuple<string, string>("int", tableData.RowCounts.ToString());
|
|
||||||
datas.Add(rowCount);
|
datas.Add(rowCount);
|
||||||
foreach (var row in tableData.Rows)
|
foreach (var row in tableData.Rows)
|
||||||
{
|
{
|
||||||
@@ -30,8 +35,9 @@ namespace ExcelTool.Parser
|
|||||||
datas.Add(new Tuple<string, string>(type, data));
|
datas.Add(new Tuple<string, string>(type, data));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var binaryFilePath = Path.Combine(outputDir, $"{excelName}.bytes");
|
var binaryFilePath = Path.Combine(outputDir, $"{sheetName}.bytes");
|
||||||
FileManager.WriteBinaryDatasToFile(binaryFilePath, datas);
|
FileManager.WriteBinaryDatasToFile(binaryFilePath, datas);
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
|||||||
+38
-14
@@ -11,21 +11,47 @@ namespace ExcelTool
|
|||||||
static void Main(string[] args)
|
static void Main(string[] args)
|
||||||
{
|
{
|
||||||
string path = AppDomain.CurrentDomain.BaseDirectory;
|
string path = AppDomain.CurrentDomain.BaseDirectory;
|
||||||
string exportPath = "";
|
string outputCodeDir = "";
|
||||||
|
string outputDataDir = "";
|
||||||
|
string nameSpace = "";
|
||||||
|
|
||||||
//TableExportFormat format = TableExportFormat.Bytes;
|
foreach (var arg in args)
|
||||||
if (args is { Length: >= 1 })
|
|
||||||
{
|
{
|
||||||
path = args[0]; //第一个是路径
|
if (arg.StartsWith("--input="))
|
||||||
}
|
|
||||||
if (args is { Length: >= 2 })
|
|
||||||
{
|
{
|
||||||
exportPath = args[1]; //第二个是输出路径
|
path = arg["--input=".Length..];
|
||||||
}
|
}
|
||||||
DirectoryInfo dirInfo = new DirectoryInfo(path);
|
else if (arg.StartsWith("--outputCodeDir="))
|
||||||
|
{
|
||||||
|
outputCodeDir = arg["--outputCodeDir=".Length..];
|
||||||
|
|
||||||
|
}
|
||||||
|
else if (arg.StartsWith("--outputDataDir="))
|
||||||
|
{
|
||||||
|
outputDataDir = arg["--outputDataDir=".Length..];
|
||||||
|
}
|
||||||
|
else if (arg.StartsWith("--namespace="))
|
||||||
|
{
|
||||||
|
nameSpace = arg["--namespace=".Length..];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (string.IsNullOrEmpty(outputCodeDir) && string.IsNullOrEmpty(outputDataDir))
|
||||||
|
{
|
||||||
|
outputDataDir = outputCodeDir = path;
|
||||||
|
}else if (string.IsNullOrEmpty(outputCodeDir))
|
||||||
|
{
|
||||||
|
outputCodeDir = outputDataDir;
|
||||||
|
}else if (string.IsNullOrEmpty(outputDataDir))
|
||||||
|
{
|
||||||
|
outputDataDir = outputCodeDir;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
DirectoryInfo dirInfo = new(path);
|
||||||
FileInfo[] csvs = dirInfo.GetFiles("*.csv", SearchOption.AllDirectories);
|
FileInfo[] csvs = dirInfo.GetFiles("*.csv", SearchOption.AllDirectories);
|
||||||
FileInfo[] excels = dirInfo.GetFiles("*.xlsx", SearchOption.AllDirectories);
|
FileInfo[] excels = dirInfo.GetFiles("*.xlsx", SearchOption.AllDirectories);
|
||||||
if ((csvs == null || csvs.Length <= 0) && (excels == null || excels.Length <= 0))
|
if ((csvs.Length <= 0) && (excels.Length <= 0))
|
||||||
{
|
{
|
||||||
"当前exe目录或者目标目录没有csv文件或者excels文件,请重新设置目录".WriteErrorLine();
|
"当前exe目录或者目标目录没有csv文件或者excels文件,请重新设置目录".WriteErrorLine();
|
||||||
}
|
}
|
||||||
@@ -58,7 +84,7 @@ namespace ExcelTool
|
|||||||
if (file.Name.StartsWith("~$")) return;
|
if (file.Name.StartsWith("~$")) return;
|
||||||
|
|
||||||
//生成CS文件
|
//生成CS文件
|
||||||
bool res = GenModels.GenCSharpModel(file.FullName, exportPath);
|
bool res = GenModels.GenCSharpModel(file.FullName, outputCodeDir, nameSpace);
|
||||||
if (res)
|
if (res)
|
||||||
{
|
{
|
||||||
$"{file.Name}CS模板生成成功".WriteSuccessLine();
|
$"{file.Name}CS模板生成成功".WriteSuccessLine();
|
||||||
@@ -69,7 +95,7 @@ namespace ExcelTool
|
|||||||
}
|
}
|
||||||
|
|
||||||
//生成二进制文件,如果list或者vector数据为空则写入0,要根据类型来读取csv的字段数据强转成对应的数据类型然后写入
|
//生成二进制文件,如果list或者vector数据为空则写入0,要根据类型来读取csv的字段数据强转成对应的数据类型然后写入
|
||||||
res = TableExcelExportBytes.ExportToFile(file.FullName, exportPath);
|
res = TableExcelExportBytes.ExportToFile(file.FullName, outputDataDir);
|
||||||
if (res)
|
if (res)
|
||||||
{
|
{
|
||||||
$"{file.Name}二进制数据生成成功".WriteSuccessLine();
|
$"{file.Name}二进制数据生成成功".WriteSuccessLine();
|
||||||
@@ -86,7 +112,7 @@ namespace ExcelTool
|
|||||||
File.Delete(genExcels[i]);
|
File.Delete(genExcels[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
Dictionary<int, string> dics = new Dictionary<int, string>();
|
Dictionary<int, string> dics = new();
|
||||||
new List<string>(dics.Values);
|
new List<string>(dics.Values);
|
||||||
|
|
||||||
//读取测试
|
//读取测试
|
||||||
@@ -102,8 +128,6 @@ namespace ExcelTool
|
|||||||
//{
|
//{
|
||||||
// ConsoleHelper.WriteErrorLine("读取失败");
|
// ConsoleHelper.WriteErrorLine("读取失败");
|
||||||
//}
|
//}
|
||||||
|
|
||||||
Console.Read();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user