- 支持生成枚举
This commit is contained in:
@@ -9,9 +9,10 @@ namespace ExcelTool
|
|||||||
{
|
{
|
||||||
public static class ExcelHelper
|
public static class ExcelHelper
|
||||||
{
|
{
|
||||||
public static List<ParsedSheet> ParseAllSheets(string fileName)
|
public static List<ParsedSheet> ParseAllSheets(string fileName, out List<ParsedEnum> enumSheets)
|
||||||
{
|
{
|
||||||
List<ParsedSheet> result = [];
|
List<ParsedSheet> result = [];
|
||||||
|
enumSheets = [];
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
using FileStream fs = File.OpenRead(fileName);
|
using FileStream fs = File.OpenRead(fileName);
|
||||||
@@ -27,7 +28,13 @@ namespace ExcelTool
|
|||||||
if (string.IsNullOrEmpty(sheetName) || sheetName.StartsWith('#'))
|
if (string.IsNullOrEmpty(sheetName) || sheetName.StartsWith('#'))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
var parsed = ParseSheet(sheet, sheetName);
|
if (sheetName.Equals("__enums__", StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
enumSheets = ParseEnumSheet(sheet);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
ParsedSheet parsed = ParseSheet(sheet, sheetName);
|
||||||
if (parsed != null)
|
if (parsed != null)
|
||||||
result.Add(parsed);
|
result.Add(parsed);
|
||||||
}
|
}
|
||||||
@@ -47,7 +54,7 @@ namespace ExcelTool
|
|||||||
IRow typeRow = sheet.GetRow(1);
|
IRow typeRow = sheet.GetRow(1);
|
||||||
IRow descRow = sheet.GetRow(2);
|
IRow descRow = sheet.GetRow(2);
|
||||||
|
|
||||||
var headers = new List<TableExcelHeader>();
|
List<TableExcelHeader> headers = [];
|
||||||
for (int j = 0; j < nameRow.LastCellNum; j++)
|
for (int j = 0; j < nameRow.LastCellNum; j++)
|
||||||
{
|
{
|
||||||
string fieldName = nameRow.GetCell(j)?.ToString() ?? "";
|
string fieldName = nameRow.GetCell(j)?.ToString() ?? "";
|
||||||
@@ -69,13 +76,13 @@ namespace ExcelTool
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 数据从第 6 行开始(0-indexed)
|
// 数据从第 6 行开始(0-indexed)
|
||||||
var tableRows = new List<TableExcelRow>();
|
List<TableExcelRow> tableRows = [];
|
||||||
for (int i = 5; i <= sheet.LastRowNum; i++)
|
for (int i = 5; i <= sheet.LastRowNum; i++)
|
||||||
{
|
{
|
||||||
IRow row = sheet.GetRow(i);
|
IRow row = sheet.GetRow(i);
|
||||||
if (row == null) continue;
|
if (row == null) continue;
|
||||||
|
|
||||||
var tableExcelRow = new TableExcelRow();
|
TableExcelRow tableExcelRow = new();
|
||||||
for (int j = 0; j < headers.Count; j++)
|
for (int j = 0; j < headers.Count; j++)
|
||||||
tableExcelRow.Add(GetCellValue(row.GetCell(j)));
|
tableExcelRow.Add(GetCellValue(row.GetCell(j)));
|
||||||
|
|
||||||
@@ -95,8 +102,52 @@ namespace ExcelTool
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string GetCellValue(ICell cell)
|
static List<ParsedEnum> ParseEnumSheet(ISheet sheet)
|
||||||
|
{
|
||||||
|
List<ParsedEnum> result = [];
|
||||||
|
// 列索引约定:A=0 Id, B=1 EnumName, C=2 items.Name, D=3 items.Value, E=4 items.Desc
|
||||||
|
// 数据从第 6 行开始(0-indexed = 5)
|
||||||
|
ParsedEnum currentEnum = null;
|
||||||
|
|
||||||
|
for (int i = 5; i <= sheet.LastRowNum; i++)
|
||||||
|
{
|
||||||
|
IRow row = sheet.GetRow(i);
|
||||||
|
if (row == null) continue;
|
||||||
|
|
||||||
|
string idStr = GetCellValue(row.GetCell(0));
|
||||||
|
string enumName = GetCellValue(row.GetCell(1));
|
||||||
|
string memberKey = GetCellValue(row.GetCell(2));
|
||||||
|
string memberVal = GetCellValue(row.GetCell(3));
|
||||||
|
string memberDesc = GetCellValue(row.GetCell(4));
|
||||||
|
|
||||||
|
// 成员名为空则跳过此行
|
||||||
|
if (string.IsNullOrEmpty(memberKey)) continue;
|
||||||
|
|
||||||
|
// Id / EnumName 非空时,开启新枚举
|
||||||
|
if (!string.IsNullOrEmpty(enumName))
|
||||||
|
{
|
||||||
|
currentEnum = new ParsedEnum
|
||||||
|
{
|
||||||
|
Id = string.IsNullOrEmpty(idStr) ? 0 : Convert.ToUInt32(double.Parse(idStr)),
|
||||||
|
EnumName = enumName,
|
||||||
|
};
|
||||||
|
result.Add(currentEnum);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果没有任何枚举上下文就跳过
|
||||||
|
currentEnum?.Members.Add(new ParsedEnumMember
|
||||||
|
{
|
||||||
|
Key = memberKey,
|
||||||
|
Value = string.IsNullOrEmpty(memberVal) ? null : Convert.ToInt32(double.Parse(memberVal)),
|
||||||
|
Desc = memberDesc,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
static string GetCellValue(ICell cell)
|
||||||
{
|
{
|
||||||
if (cell == null)
|
if (cell == null)
|
||||||
return "";
|
return "";
|
||||||
|
|||||||
@@ -0,0 +1,83 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace ExcelTool.Parser
|
||||||
|
{
|
||||||
|
public static class GenEnums
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 所有枚举写入同一个文件 AudioEnums.cs
|
||||||
|
/// </summary>
|
||||||
|
public static bool GenCSharpEnum(List<ParsedEnum> enumSheet, string outputDir, string nameSpace = "")
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
StringBuilder sb = new();
|
||||||
|
sb.Append("/*\n * auto generated by tools(注意:千万不要手动修改本文件)\n * AudioEnums\n */\n\n");
|
||||||
|
|
||||||
|
if (!string.IsNullOrEmpty(nameSpace))
|
||||||
|
sb.Append($"namespace {nameSpace}\n{{\n");
|
||||||
|
|
||||||
|
foreach (ParsedEnum parsedEnum in enumSheet)
|
||||||
|
{
|
||||||
|
sb.Append($"\tpublic enum {parsedEnum.EnumName}\n\t{{\n");
|
||||||
|
|
||||||
|
foreach (ParsedEnumMember member in parsedEnum.Members)
|
||||||
|
{
|
||||||
|
string valuePart = member.Value.HasValue ? $" = {member.Value.Value}" : "";
|
||||||
|
string descPart = string.IsNullOrEmpty(member.Desc) ? "" : $" // {member.Desc}";
|
||||||
|
sb.Append($"\t\t{member.Key}{valuePart},{descPart}\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
sb.Append("\t}\n\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!string.IsNullOrEmpty(nameSpace))
|
||||||
|
sb.Append("}\n");
|
||||||
|
|
||||||
|
FileManager.WriteToFile(Path.Combine(outputDir, "AudioEnums.cs"), sb.ToString());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
ex.ToString().WriteErrorLine();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 将所有枚举的 Id 汇总,生成 EnumIds.cs 静态常量类
|
||||||
|
/// </summary>
|
||||||
|
public static bool GenEnumIds(List<ParsedEnum> enumSheets, string outputDir, string nameSpace = "")
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
StringBuilder sb = new();
|
||||||
|
sb.Append("/*\n * auto generated by tools(注意:千万不要手动修改本文件)\n * EnumIds\n */\n\n");
|
||||||
|
|
||||||
|
if (!string.IsNullOrEmpty(nameSpace))
|
||||||
|
sb.Append($"namespace {nameSpace}\n{{\n");
|
||||||
|
|
||||||
|
sb.Append("\tpublic static class EnumIds\n\t{\n");
|
||||||
|
|
||||||
|
foreach (ParsedEnum enumSheet in enumSheets)
|
||||||
|
sb.Append($"\t\tpublic const uint {enumSheet.EnumName} = {enumSheet.Id};\n");
|
||||||
|
|
||||||
|
sb.Append("\t}\n");
|
||||||
|
|
||||||
|
if (!string.IsNullOrEmpty(nameSpace))
|
||||||
|
sb.Append("}\n");
|
||||||
|
|
||||||
|
FileManager.WriteToFile(Path.Combine(outputDir, "AudioEnumIds.cs"), sb.ToString());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
ex.ToString().WriteErrorLine();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace ExcelTool.Parser
|
||||||
|
{
|
||||||
|
public class ParsedEnumMember
|
||||||
|
{
|
||||||
|
/// <summary>成员名,如 "Home"</summary>
|
||||||
|
public string Key { get; set; }
|
||||||
|
|
||||||
|
/// <summary>显式值,为 null 时代码生成时自动递增(不写值)</summary>
|
||||||
|
public int? Value { get; set; }
|
||||||
|
|
||||||
|
/// <summary>行内注释,如 "主界面"</summary>
|
||||||
|
public string Desc { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class ParsedEnum
|
||||||
|
{
|
||||||
|
/// <summary>A 列 Id,供外部系统通过 EnumIds 常量类引用</summary>
|
||||||
|
public uint Id { get; set; }
|
||||||
|
|
||||||
|
/// <summary>枚举类型名,如 "GameState"</summary>
|
||||||
|
public string EnumName { get; set; }
|
||||||
|
|
||||||
|
public List<ParsedEnumMember> Members { get; set; } = new();
|
||||||
|
}
|
||||||
|
}
|
||||||
+17
-1
@@ -76,7 +76,7 @@ namespace ExcelTool
|
|||||||
{
|
{
|
||||||
if (file.Name.StartsWith("~$")) continue;
|
if (file.Name.StartsWith("~$")) continue;
|
||||||
|
|
||||||
List<ParsedSheet> sheets = ExcelHelper.ParseAllSheets(file.FullName);
|
List<ParsedSheet> sheets = ExcelHelper.ParseAllSheets(file.FullName, out List<ParsedEnum>? enumSheets);
|
||||||
|
|
||||||
//生成CS文件
|
//生成CS文件
|
||||||
bool res = GenModels.GenCSharpModel(sheets, codeDir, nameSpace);
|
bool res = GenModels.GenCSharpModel(sheets, codeDir, nameSpace);
|
||||||
@@ -88,6 +88,22 @@ namespace ExcelTool
|
|||||||
{
|
{
|
||||||
$"{file.Name}CS模板生成失败".WriteErrorLine();
|
$"{file.Name}CS模板生成失败".WriteErrorLine();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 生成CS枚举文件
|
||||||
|
if (enumSheets.Count > 0)
|
||||||
|
{
|
||||||
|
bool enumRes = GenEnums.GenCSharpEnum(enumSheets, codeDir, nameSpace);
|
||||||
|
if (enumRes)
|
||||||
|
$"{file.Name} 枚举代码生成成功".WriteSuccessLine();
|
||||||
|
else
|
||||||
|
$"{file.Name} 枚举代码生成失败".WriteErrorLine();
|
||||||
|
|
||||||
|
bool enumIdsRes = GenEnums.GenEnumIds(enumSheets, codeDir, nameSpace);
|
||||||
|
if (enumIdsRes)
|
||||||
|
$"{file.Name} EnumIds 生成成功".WriteSuccessLine();
|
||||||
|
else
|
||||||
|
$"{file.Name} EnumIds 生成失败".WriteErrorLine();
|
||||||
|
}
|
||||||
|
|
||||||
//生成二进制文件,如果list或者vector数据为空则写入0,要根据类型来读取csv的字段数据强转成对应的数据类型然后写入
|
//生成二进制文件,如果list或者vector数据为空则写入0,要根据类型来读取csv的字段数据强转成对应的数据类型然后写入
|
||||||
res = TableExcelExportBytes.ExportToFile(sheets, dataDir);
|
res = TableExcelExportBytes.ExportToFile(sheets, dataDir);
|
||||||
|
|||||||
Reference in New Issue
Block a user