支持同一工作簿内多个sheet导出,跳过#开头的sheet

This commit is contained in:
2026-03-10 15:40:32 +08:00
parent e225e85941
commit a7ef00f627
5 changed files with 668 additions and 530 deletions
+25 -7
View File
@@ -7,9 +7,9 @@ using ExcelTool.Parser;
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
{
@@ -17,7 +17,16 @@ namespace ExcelTool
using FileStream fs = File.OpenRead(fileName);
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 typeRow = sheet.GetRow(1); // 类型
@@ -39,7 +48,7 @@ namespace ExcelTool
{
FieldName = fieldName,
FieldType = fieldType,
FieldDesc = fieldDesc
FieldDesc = fieldDesc,
});
}
@@ -48,20 +57,27 @@ namespace ExcelTool
catch (Exception ex)
{
ex.ToString().WriteErrorLine();
sheetName = null;
sheetCount = 0;
return null;
}
}
public static TableExcelData ExcelDatas(string fileName)
public static TableExcelData ExcelDatas(string fileName, out string sheetName, out int sheetCount, int sheetNum = 0)
{
try
{
var excelHeader = ExcelHeaders(fileName);
var excelHeader = ExcelHeaders(fileName, out sheetName, out sheetCount);
var tableRows = new List<TableExcelRow>();
using FileStream fs = File.OpenRead(fileName);
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++)
{
@@ -84,6 +100,8 @@ namespace ExcelTool
catch (Exception ex)
{
ex.ToString().WriteErrorLine();
sheetName = null;
sheetCount = 0;
return null;
}
}
+1 -1
View File
@@ -8,7 +8,7 @@ namespace ExcelTool
/// <summary>
/// 文件操作类
/// </summary>
public class FileManager
public static class FileManager
{
public static bool CreateDir(string dirPath)
{
File diff suppressed because it is too large Load Diff
+21 -15
View File
@@ -4,34 +4,40 @@ using System.IO;
namespace ExcelTool.Parser
{
public class TableExcelExportBytes
public static class TableExcelExportBytes
{
public static bool ExportToFile(string fileName, string outputDir = null)
{
try
{
FileInfo fileInfo = new FileInfo(fileName);
FileInfo fileInfo = new(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.RowCounts.ToString());
datas.Add(rowCount);
foreach (var row in tableData.Rows)
for (int sheetNum = 0; ; sheetNum++)
{
for (int i = 0; i < tableData.CollonCount; i++)
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();
//先写入行数,然后每一行的数据一次写入 小写类型、字符串
Tuple<string, string> rowCount = new("int", tableData.RowCounts.ToString());
datas.Add(rowCount);
foreach (var row in tableData.Rows)
{
var type = tableData.Headers[i].FieldType.ToLower();
var data = row.StrList[i];
datas.Add(new Tuple<string, string>(type, data));
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, $"{sheetName}.bytes");
FileManager.WriteBinaryDatasToFile(binaryFilePath, datas);
}
var binaryFilePath = Path.Combine(outputDir, $"{excelName}.bytes");
FileManager.WriteBinaryDatasToFile(binaryFilePath, datas);
return true;
}
catch (Exception ex)
+37 -13
View File
@@ -11,21 +11,47 @@ namespace ExcelTool
static void Main(string[] args)
{
string path = AppDomain.CurrentDomain.BaseDirectory;
string exportPath = "";
string outputCodeDir = "";
string outputDataDir = "";
string nameSpace = "";
//TableExportFormat format = TableExportFormat.Bytes;
if (args is { Length: >= 1 })
foreach (var arg in args)
{
path = args[0]; //第一个是路径
if (arg.StartsWith("--input="))
{
path = arg["--input=".Length..];
}
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 (args is { Length: >= 2 })
if (string.IsNullOrEmpty(outputCodeDir) && string.IsNullOrEmpty(outputDataDir))
{
exportPath = args[1]; //第二个是输出路径
outputDataDir = outputCodeDir = path;
}else if (string.IsNullOrEmpty(outputCodeDir))
{
outputCodeDir = outputDataDir;
}else if (string.IsNullOrEmpty(outputDataDir))
{
outputDataDir = outputCodeDir;
}
DirectoryInfo dirInfo = new DirectoryInfo(path);
DirectoryInfo dirInfo = new(path);
FileInfo[] csvs = dirInfo.GetFiles("*.csv", 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();
}
@@ -58,7 +84,7 @@ namespace ExcelTool
if (file.Name.StartsWith("~$")) return;
//生成CS文件
bool res = GenModels.GenCSharpModel(file.FullName, exportPath);
bool res = GenModels.GenCSharpModel(file.FullName, outputCodeDir, nameSpace);
if (res)
{
$"{file.Name}CS模板生成成功".WriteSuccessLine();
@@ -69,7 +95,7 @@ namespace ExcelTool
}
//生成二进制文件,如果list或者vector数据为空则写入0,要根据类型来读取csv的字段数据强转成对应的数据类型然后写入
res = TableExcelExportBytes.ExportToFile(file.FullName, exportPath);
res = TableExcelExportBytes.ExportToFile(file.FullName, outputDataDir);
if (res)
{
$"{file.Name}二进制数据生成成功".WriteSuccessLine();
@@ -86,7 +112,7 @@ namespace ExcelTool
File.Delete(genExcels[i]);
}
Dictionary<int, string> dics = new Dictionary<int, string>();
Dictionary<int, string> dics = new();
new List<string>(dics.Values);
//读取测试
@@ -102,8 +128,6 @@ namespace ExcelTool
//{
// ConsoleHelper.WriteErrorLine("读取失败");
//}
Console.Read();
}
}
}