代码优化

This commit is contained in:
dingxiaowei
2022-08-26 19:00:58 +08:00
parent 265eeafdda
commit 52f0c31497
21 changed files with 409 additions and 302 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+18 -298
View File
@@ -1,21 +1,18 @@
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;
namespace ExcelTool
{
public class ExcelHelper
{
static DataTable ExcelHeader(string fileName)
public static List<TableExcelHeader> ExcelHeaders(string fileName)
{
try
{
DataTable dt = new DataTable();
var headers = new List<TableExcelHeader>();
IWorkbook wk = null;
string extension = Path.GetExtension(fileName);
FileStream fs = File.OpenRead(fileName);
@@ -23,9 +20,11 @@ namespace ExcelTool
wk = new XSSFWorkbook(fs);
ISheet sheet = wk.GetSheetAt(0);
IRow row = sheet.GetRow(0); //读取当前第一行的数据
var descRow = sheet.GetRow(1);//读取注释
for (int j = 0; j < row.LastCellNum; j++)
{
string value = row.GetCell(j).ToString().Replace(" ", "");
var descValue = descRow.GetCell(j) == null ? "" : descRow.GetCell(j).ToString();
var array = value.Split(',');
if (array.Length != 2)
{
@@ -33,25 +32,12 @@ namespace ExcelTool
}
else
{
DataColumn dc = new DataColumn($"{array[1]}|{array[0]}"); //命名|类型
dt.Columns.Add(dc);
headers.Add(new TableExcelHeader() { FieldName = array[1], FieldType = array[0], FieldDesc = descValue });
}
}
row = sheet.GetRow(1);//读取注释
if (row != null)
{
DataRow dr = dt.NewRow();
for (int j = 0; j < row.LastCellNum; j++)
{
var cellValue = row.GetCell(j);
string cellValueStr = cellValue == null ? "" : cellValue.ToString();
dr[j] = cellValueStr;
}
dt.Rows.Add(dr);
}
fs.Close();
fs.Dispose();
return dt;
return headers;
}
catch (Exception ex)
{
@@ -60,56 +46,38 @@ namespace ExcelTool
}
}
public static DataTable Excel2DataTable(string fileName)
public static TableExcelData ExcelDatas(string fileName)
{
try
{
DataTable dt = new DataTable();
var excelHeader = ExcelHeaders(fileName);
var tableRows = new List<TableExcelRow>();
IWorkbook wk = null;
string extension = Path.GetExtension(fileName);
FileStream fs = File.OpenRead(fileName);
if (extension.Equals(".xlsx"))
wk = new XSSFWorkbook(fs);
ISheet sheet = wk.GetSheetAt(0);
IRow row = sheet.GetRow(0); //读取当前第一行的数据
for (int j = 0; j < row.LastCellNum; j++)
{
var cellValue = row.GetCell(j);
if (cellValue == null)
{
throw new Exception($"文件:{fileName}第一行存在空类型配置,请检查");
}
string value = row.GetCell(j).ToString().Replace(" ", "").Replace("\"", "");
var array = value.Split(',');
if (array.Length != 2)
{
ConsoleHelper.WriteErrorLine("表格第一行类型定义有异常,不是类型,命名的形式");
}
else
{
DataColumn dc = new DataColumn($"{array[1]}|{array[0]}"); //命名|类型
dt.Columns.Add(dc);
}
}
//跳过注释读取数据
for (int i = 2; i <= sheet.LastRowNum; i++)
{
row = sheet.GetRow(i);
var dr = dt.NewRow();
for (int j = 0; j < row.LastCellNum; j++)
var row = sheet.GetRow(i);
var tableExcelRow = new TableExcelRow();
for (int j = 0; j <= row.LastCellNum; j++)
{
var cellValue = row.GetCell(j);
if (cellValue != null)
dr[j] = row.GetCell(j).ToString().Replace(" ", "");
tableExcelRow.Add(row.GetCell(j).ToString().Replace(" ", ""));
else
dr[j] = "";
tableExcelRow.Add("");
}
dt.Rows.Add(dr);
tableRows.Add(tableExcelRow);
}
fs.Close();
fs.Dispose();
return dt;
var tableExcelData = new TableExcelData(excelHeader, tableRows);
return tableExcelData;
}
catch (Exception ex)
{
@@ -117,253 +85,5 @@ namespace ExcelTool
return null;
}
}
public static bool GenBinaryData(string fileName)
{
try
{
FileInfo fileInfo = new FileInfo(fileName);
var excelName = fileInfo.Name.Remove(fileInfo.Name.IndexOf(".xlsx"));
//先写入行数,然后没一行的数据一次写入 小写类型、字符串
List<Tuple<string, string>> datas = new List<Tuple<string, string>>();
var dataTable = Excel2DataTable(fileName);
Tuple<string, string> rowCount = new Tuple<string, string>("int", dataTable.Rows.Count.ToString());
datas.Add(rowCount);
foreach (DataRow row in dataTable.Rows)
{
for (int i = 0; i < dataTable.Columns.Count; i++)
{
var typeHeader = dataTable.Columns[i].ToString().ToLower();
var headers = typeHeader.Split('|');
Tuple<string, string> t = new Tuple<string, string>(headers[1], row[i].ToString());
datas.Add(t);
}
}
var binaryFilePath = Path.Combine(fileInfo.DirectoryName, $"{excelName}.bytes");
FileManager.WriteBinaryDatasToFile(binaryFilePath, datas);
return true;
}
catch (Exception ex)
{
ConsoleHelper.WriteErrorLine(ex.ToString());
return false;
}
}
/// <summary>
/// 生成对应的C#Model类
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public static bool GenCSharpModel(string fileName)
{
try
{
FileInfo fileInfo = new FileInfo(fileName);
var excelName = fileInfo.Name.Remove(fileInfo.Name.IndexOf(".xlsx"));
var dt = ExcelHeader(fileName);
var headers = dt.Headers();
List<string> notesStr = new List<string>();
if (dt.Rows != null && dt.Rows.Count > 0)
{
var notes = dt.Rows[0];
for (int i = 0; i < dt.Columns.Count; i++)
{
notesStr.Add(notes[i].ToString());
}
}
//前面是字段,后面是类型 vector是3个float [1.1,2.2,3.3]
//foreach (var header in headers)
//{
// ConsoleHelper.WriteInfoLine($"{header.Item1}|{header.Item2}");
//}
StringBuilder sb = new StringBuilder();
sb.Append($"/*\n * auto generated by tools(注意:千万不要手动修改本文件)\n * {excelName}\n */\n");
sb.Append("using System;\nusing System.IO;\nusing System.Collections.Generic;\nusing System.Text;\nusing System.Linq;\n\n");
sb.Append("[Serializable]\n");
sb.Append($"public class {excelName} : IBinarySerializable\n");
sb.Append("{\n");
for (int i = 0; i < headers.Count; i++)
{
sb.Append($"\t/// <summary>\n");
sb.Append($"\t/// {notesStr[i]}\n");
sb.Append($"\t/// </summary>\n");
var type = headers[i].Item2.ToLower();
if (type.Equals("vector"))
{
sb.Append(string.Format("\tpublic List<float> {0}", headers[i].Item1));
}
else if (type.Equals("list"))
{
sb.Append(string.Format("\tpublic List<List<float>> {0}", headers[i].Item1));
}
else
{
sb.Append(string.Format("\tpublic {0} {1}", headers[i].Item2.ToLower(), headers[i].Item1));
}
sb.Append(" { get; set; }\n");
}
sb.Append("\n\tpublic void DeSerialize(BinaryReader reader)\n");
sb.Append("\t{\n");
foreach (var header in headers)
{
var type = header.Item2.ToLower();
var name = header.Item1.ToString();
if (type.Equals("int"))
{
sb.Append($"\t\t{name} = reader.ReadInt32();\n");
}
else if (type.Equals("bool"))
{
sb.Append($"\t\t{name} = reader.ReadBoolean();\n");
}
else if (type.Equals("float"))
{
sb.Append($"\t\t{name} = reader.ReadSingle();\n");
}
else if (type.Equals("string"))
{
sb.Append($"\t\t{name} = reader.ReadString();\n");
}
else if (type.Equals("vector"))
{
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<float>();\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.ReadSingle());\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("list"))
{
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<List<float>>();\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\tvar tempList = new List<float>();\n");
sb.Append($"\t\t\t\tvar tempListCount = reader.ReadInt32();\n");
sb.Append($"\t\t\t\tfor (int j = 0; j < tempListCount; j++)\n");
sb.Append("\t\t\t\t{\n");
sb.Append($"\t\t\t\t\ttempList.Add(reader.ReadSingle());\n");
sb.Append("\t\t\t\t}\n");
sb.Append($"\t\t\t\t{name}.Add(tempList);\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
{
ConsoleHelper.WriteErrorLine($"类型:{type}没有解析 {fileName}处理异常");
return false;
}
}
sb.Append("\t}\n\n");
sb.Append("\tpublic void Serialize(BinaryWriter writer)\n");
sb.Append("\t{\n");
foreach (var header in headers)
{
var type = header.Item2.ToLower();
var name = header.Item1.ToString();
if (type.Equals("int") || type.Equals("bool") || type.Equals("float") || type.Equals("string"))
{
sb.Append($"\t\twriter.Write({name});\n");
}
else if (type.Equals("vector"))
{
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("list"))
{
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].Count);\n");
sb.Append($"\t\t\t\tfor (int j = 0; j < {name}[i].Count; j++)\n");
sb.Append("\t\t\t\t{\n");
sb.Append($"\t\t\t\t\twriter.Write({name}[i][j]);\n");
sb.Append("\t\t\t\t}\n");
sb.Append("\t\t\t}\n");
sb.Append("\t\t}\n");
}
else
{
ConsoleHelper.WriteErrorLine($"类型:{type}没有解析 {fileName}处理异常");
return false;
}
}
sb.Append("\t}\n");
sb.Append("}\n");
sb.Append("\n");
sb.Append("[Serializable]\n");
sb.Append($"public partial class {excelName}Config : IBinarySerializable\n");
sb.Append("{\n");
sb.Append($"\tpublic List<{excelName}> {excelName}Infos = new List<{excelName}>();\n");
sb.Append($"\tpublic void DeSerialize(BinaryReader reader)\n");
sb.Append("\t{\n");
sb.Append($"\t\tint count = reader.ReadInt32();\n");
sb.Append($"\t\tfor (int i = 0;i < count; i++)\n");
sb.Append("\t\t{\n");
sb.Append($"\t\t\t{excelName} tempData = new {excelName}();\n");
sb.Append($"\t\t\ttempData.DeSerialize(reader);\n");
sb.Append($"\t\t\t{excelName}Infos.Add(tempData);\n");
sb.Append("\t\t}\n");
sb.Append("\t}\n");
sb.Append("\n");
sb.Append("\tpublic void Serialize(BinaryWriter writer)\n");
sb.Append("\t{\n");
sb.Append($"\t\twriter.Write({excelName}Infos.Count);\n");
sb.Append($"\t\tfor (int i = 0; i < {excelName}Infos.Count; i++)\n");
sb.Append("\t\t{\n");
sb.Append($"\t\t\t{excelName}Infos[i].Serialize(writer);\n");
sb.Append("\t\t}\n");
sb.Append("\t}\n\n");
sb.Append($"\tpublic IEnumerable<{excelName}> QueryById(int id)\n");
sb.Append("\t{\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\tselect d;\n");
sb.Append("\t\treturn datas;\n");
sb.Append("\t}\n");
sb.Append("}\n");
FileManager.WriteToFile(Path.Combine(fileInfo.DirectoryName, $"{excelName}.cs"), sb.ToString());
return true;
}
catch (Exception ex)
{
ConsoleHelper.WriteErrorLine(ex.ToString());
return false;
}
}
}
}
+6
View File
@@ -70,6 +70,12 @@
<Compile Include="Extend.cs" />
<Compile Include="FileManager.cs" />
<Compile Include="IBinarySerializable.cs" />
<Compile Include="Parser\GenModels.cs" />
<Compile Include="Parser\TableExcelData.cs" />
<Compile Include="Parser\TableExcelExportBytes.cs" />
<Compile Include="Parser\TableExcelHeader.cs" />
<Compile Include="Parser\TableExcelRow.cs" />
<Compile Include="Parser\TableExportFormat.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
+237
View File
@@ -0,0 +1,237 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ExcelTool
{
public class GenModels
{
/// <summary>
/// 生成对应的C#Model类
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public static bool GenCSharpModel(string fileName, string outputDir)
{
if (string.IsNullOrEmpty(fileName))
{
ConsoleHelper.WriteErrorLine("GenCSharpModel 参数传递有误");
return false;
}
try
{
FileInfo fileInfo = new FileInfo(fileName);
if (string.IsNullOrEmpty(outputDir))
{
outputDir = fileInfo.DirectoryName;
}
var headers = ExcelHelper.ExcelHeaders(fileName);
var excelName = fileInfo.Name.Remove(fileInfo.Name.IndexOf(".xlsx"));
//var dt = ExcelHeader(fileName);
//var headers = dt.Headers();
//List<string> notesStr = new List<string>();
//if (dt.Rows != null && dt.Rows.Count > 0)
//{
// var notes = dt.Rows[0];
// for (int i = 0; i < dt.Columns.Count; i++)
// {
// notesStr.Add(notes[i].ToString());
// }
//}
//前面是字段,后面是类型 vector是3个float [1.1,2.2,3.3]
//foreach (var header in headers)
//{
// ConsoleHelper.WriteInfoLine($"{header.Item1}|{header.Item2}");
//}
StringBuilder sb = new StringBuilder();
sb.Append($"/*\n * auto generated by tools(注意:千万不要手动修改本文件)\n * {excelName}\n */\n");
sb.Append("using System;\nusing System.IO;\nusing System.Collections.Generic;\nusing System.Text;\nusing System.Linq;\n\n");
sb.Append("[Serializable]\n");
sb.Append($"public class {excelName} : IBinarySerializable\n");
sb.Append("{\n");
for (int i = 0; i < headers.Count; i++)
{
sb.Append($"\t/// <summary>\n");
sb.Append($"\t/// {headers[i].FieldDesc}\n");
sb.Append($"\t/// </summary>\n");
var type = headers[i].FieldType.ToLower();
if (type.Equals("vector"))
{
sb.Append(string.Format("\tpublic List<float> {0}", headers[i].FieldName));
}
else if (type.Equals("list"))
{
sb.Append(string.Format("\tpublic List<List<float>> {0}", headers[i].FieldName));
}
else
{
sb.Append(string.Format("\tpublic {0} {1}", headers[i].FieldType.ToLower(), headers[i].FieldName));
}
sb.Append(" { get; set; }\n");
}
sb.Append("\n\tpublic void DeSerialize(BinaryReader reader)\n");
sb.Append("\t{\n");
foreach (var header in headers)
{
var type = header.FieldType.ToLower();
var name = header.FieldName;
if (type.Equals("int"))
{
sb.Append($"\t\t{name} = reader.ReadInt32();\n");
}
else if (type.Equals("bool"))
{
sb.Append($"\t\t{name} = reader.ReadBoolean();\n");
}
else if (type.Equals("float"))
{
sb.Append($"\t\t{name} = reader.ReadSingle();\n");
}
else if (type.Equals("string"))
{
sb.Append($"\t\t{name} = reader.ReadString();\n");
}
else if (type.Equals("vector"))
{
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<float>();\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.ReadSingle());\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("list"))
{
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<List<float>>();\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\tvar tempList = new List<float>();\n");
sb.Append($"\t\t\t\tvar tempListCount = reader.ReadInt32();\n");
sb.Append($"\t\t\t\tfor (int j = 0; j < tempListCount; j++)\n");
sb.Append("\t\t\t\t{\n");
sb.Append($"\t\t\t\t\ttempList.Add(reader.ReadSingle());\n");
sb.Append("\t\t\t\t}\n");
sb.Append($"\t\t\t\t{name}.Add(tempList);\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
{
ConsoleHelper.WriteErrorLine($"类型:{type}没有解析 {fileName}处理异常");
return false;
}
}
sb.Append("\t}\n\n");
sb.Append("\tpublic void Serialize(BinaryWriter writer)\n");
sb.Append("\t{\n");
foreach (var header in headers)
{
var type = header.FieldType.ToLower();
var name = header.FieldName;
if (type.Equals("int") || type.Equals("bool") || type.Equals("float") || type.Equals("string"))
{
sb.Append($"\t\twriter.Write({name});\n");
}
else if (type.Equals("vector"))
{
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("list"))
{
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].Count);\n");
sb.Append($"\t\t\t\tfor (int j = 0; j < {name}[i].Count; j++)\n");
sb.Append("\t\t\t\t{\n");
sb.Append($"\t\t\t\t\twriter.Write({name}[i][j]);\n");
sb.Append("\t\t\t\t}\n");
sb.Append("\t\t\t}\n");
sb.Append("\t\t}\n");
}
else
{
ConsoleHelper.WriteErrorLine($"类型:{type}没有解析 {fileName}处理异常");
return false;
}
}
sb.Append("\t}\n");
sb.Append("}\n");
sb.Append("\n");
sb.Append("[Serializable]\n");
sb.Append($"public partial class {excelName}Config : IBinarySerializable\n");
sb.Append("{\n");
sb.Append($"\tpublic List<{excelName}> {excelName}Infos = new List<{excelName}>();\n");
sb.Append($"\tpublic void DeSerialize(BinaryReader reader)\n");
sb.Append("\t{\n");
sb.Append($"\t\tint count = reader.ReadInt32();\n");
sb.Append($"\t\tfor (int i = 0;i < count; i++)\n");
sb.Append("\t\t{\n");
sb.Append($"\t\t\t{excelName} tempData = new {excelName}();\n");
sb.Append($"\t\t\ttempData.DeSerialize(reader);\n");
sb.Append($"\t\t\t{excelName}Infos.Add(tempData);\n");
sb.Append("\t\t}\n");
sb.Append("\t}\n");
sb.Append("\n");
sb.Append("\tpublic void Serialize(BinaryWriter writer)\n");
sb.Append("\t{\n");
sb.Append($"\t\twriter.Write({excelName}Infos.Count);\n");
sb.Append($"\t\tfor (int i = 0; i < {excelName}Infos.Count; i++)\n");
sb.Append("\t\t{\n");
sb.Append($"\t\t\t{excelName}Infos[i].Serialize(writer);\n");
sb.Append("\t\t}\n");
sb.Append("\t}\n\n");
sb.Append($"\tpublic IEnumerable<{excelName}> QueryById(int id)\n");
sb.Append("\t{\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\tselect d;\n");
sb.Append("\t\treturn datas;\n");
sb.Append("\t}\n");
sb.Append("}\n");
FileManager.WriteToFile(Path.Combine(outputDir, $"{excelName}.cs"), sb.ToString());
return true;
}
catch (Exception ex)
{
ConsoleHelper.WriteErrorLine(ex.ToString());
return false;
}
}
}
}
+38
View File
@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ExcelTool
{
public class TableExcelData
{
private List<TableExcelHeader> headers = new List<TableExcelHeader>();
private List<TableExcelRow> rows = new List<TableExcelRow>();
public int CollonCount = 0;
public TableExcelData(IEnumerable<TableExcelHeader> headers, IEnumerable<TableExcelRow> rows)
{
this.headers = headers.ToList();
this.rows = rows.ToList();
this.CollonCount = this.headers.Count;
}
public List<TableExcelHeader> Headers
{
get { return this.headers; }
}
public List<TableExcelRow> Rows
{
get { return this.rows; }
}
//TODO:待检查数据类型的合法性
//public bool CheckUnique(out string errorMsg)
//{
//}
}
}
+44
View File
@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.IO;
namespace ExcelTool
{
public class TableExcelExportBytes
{
public static bool ExportToFile(string fileName, string outputDir = null)
{
try
{
FileInfo fileInfo = new FileInfo(fileName);
if (string.IsNullOrEmpty(outputDir))
{
outputDir = fileInfo.DirectoryName;
}
var excelName = fileInfo.Name.Remove(fileInfo.Name.IndexOf(".xlsx"));
//先写入行数,然后每一行的数据一次写入 小写类型、字符串
List<Tuple<string, string>> datas = new List<Tuple<string, string>>();
var tableData = ExcelHelper.ExcelDatas(fileName);
Tuple<string, string> rowCount = new Tuple<string, string>("int", tableData.CollonCount.ToString());
datas.Add(rowCount);
foreach (var row in tableData.Rows)
{
for (int i = 0; i < tableData.CollonCount; i++)
{
var type = tableData.Headers[i].FieldType.ToLower();
var data = row.StrList[i];
datas.Add(new Tuple<string, string>(type, data));
}
}
var binaryFilePath = Path.Combine(outputDir, $"{excelName}.bytes");
FileManager.WriteBinaryDatasToFile(binaryFilePath, datas);
return true;
}
catch (Exception ex)
{
ConsoleHelper.WriteErrorLine(ex.ToString());
return false;
}
}
}
}
+9
View File
@@ -0,0 +1,9 @@
namespace ExcelTool
{
public class TableExcelHeader
{
public string FieldName { get; set; }
public string FieldType { get; set; }
public string FieldDesc { get; set; }
}
}
+18
View File
@@ -0,0 +1,18 @@
using System.Collections.Generic;
namespace ExcelTool
{
public class TableExcelRow
{
public List<string> StrList { get; set; }
public TableExcelRow()
{
StrList = new List<string>();
}
public void Add(string str)
{
StrList.Add(str);
}
}
}
+29
View File
@@ -0,0 +1,29 @@
namespace ExcelTool
{
/// <summary>
/// 导出格式
/// </summary>
public enum TableExportFormat
{
/// <summary>
/// 未知
/// </summary>
Unknown = 0,
/// <summary>
/// 二进制
/// </summary>
Bytes,
/// <summary>
/// Json格式
/// </summary>
Json,
/// <summary>
/// Xml格式
/// </summary>
Xml,
/// <summary>
/// Lua格式
/// </summary>
Lua,
}
}
+9 -3
View File
@@ -9,10 +9,16 @@ namespace ExcelTool
static void Main(string[] args)
{
string path = AppDomain.CurrentDomain.BaseDirectory;
if (args != null && args.Length >= 1)
string exportPath = "";
//TableExportFormat format = TableExportFormat.Bytes;
if (args != null && args.Length == 1)
{
path = args[0]; //第一个是路径
}
if (args != null && args.Length == 2)
{
exportPath = args[1]; //第二个是输出路径
}
DirectoryInfo dirInfo = new DirectoryInfo(path);
var csvs = dirInfo.GetFiles("*.csv", SearchOption.AllDirectories);
var excels = dirInfo.GetFiles("*.xlsx", SearchOption.AllDirectories);
@@ -47,7 +53,7 @@ namespace ExcelTool
foreach (var file in excels)
{
//生成CS文件
bool res = ExcelHelper.GenCSharpModel(file.FullName);
bool res = GenModels.GenCSharpModel(file.FullName, exportPath);
if (res)
{
ConsoleHelper.WriteSuccessLine($"{file.Name}CS模板生成成功");
@@ -58,7 +64,7 @@ namespace ExcelTool
}
//生成二进制文件,如果list或者vector数据为空则写入0,要根据类型来读取csv的字段数据强转成对应的数据类型然后写入
res = ExcelHelper.GenBinaryData(file.FullName);
res = TableExcelExportBytes.ExportToFile(file.FullName, exportPath);
if (res)
{
ConsoleHelper.WriteSuccessLine($"{file.Name}二进制数据生成成功");
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1 +1 @@
3770a330c7ef4b58251471e824d29ae88622bbc0
5be383d8458e8adf3321425f1a6ef7ed98f16846
Binary file not shown.
Binary file not shown.