代码优化

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
+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,
}
}