更新读取表格字段添加到代码模板中
This commit is contained in:
+35
-53
@@ -11,52 +11,6 @@ namespace ExcelTool
|
||||
{
|
||||
public class ExcelHelper
|
||||
{
|
||||
//static DataTable ReadFromExcelFile(string filePath)
|
||||
//{
|
||||
// DataTable dt = new DataTable();
|
||||
// IWorkbook wk = null;
|
||||
// string extension = Path.GetExtension(filePath);
|
||||
// try
|
||||
// {
|
||||
// FileStream fs = File.OpenRead(filePath);
|
||||
// //if (extension.Equals(".xls"))
|
||||
// //{
|
||||
// // //把xls文件中的数据写入wk中
|
||||
// // wk = new HSSFWorkbook(fs);
|
||||
// //}
|
||||
// //else
|
||||
// //{
|
||||
// if (extension.Equals(".xlsx")) //这里只考虑xlsx的情况
|
||||
// //把xlsx文件中的数据写入wk中
|
||||
// wk = new XSSFWorkbook(fs);
|
||||
// //}
|
||||
|
||||
// fs.Close();
|
||||
// fs.Dispose();
|
||||
// ISheet sheet = wk.GetSheetAt(0);
|
||||
// IRow row = sheet.GetRow(0); //读取当前行数据
|
||||
// //LastRowNum 是当前表的总行数-1(注意)
|
||||
// for (int i = 0; i <= sheet.LastRowNum; i++)
|
||||
// {
|
||||
// row = sheet.GetRow(i);
|
||||
// if (row != null)
|
||||
// {
|
||||
// for (int j = 0; j < row.LastCellNum; j++)
|
||||
// {
|
||||
// string value = row.GetCell(j).ToString();
|
||||
// Console.Write(value.ToString() + " ");
|
||||
// }
|
||||
// Console.WriteLine("\n");
|
||||
// }
|
||||
// }
|
||||
// return dt;
|
||||
// }
|
||||
// catch (Exception e)
|
||||
// {
|
||||
// ConsoleHelper.WriteErrorLine(e.Message);
|
||||
// }
|
||||
//}
|
||||
|
||||
static DataTable ExcelHeader(string fileName)
|
||||
{
|
||||
try
|
||||
@@ -83,6 +37,18 @@ namespace ExcelTool
|
||||
dt.Columns.Add(dc);
|
||||
}
|
||||
}
|
||||
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;
|
||||
@@ -108,6 +74,11 @@ namespace ExcelTool
|
||||
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)
|
||||
@@ -124,7 +95,7 @@ namespace ExcelTool
|
||||
for (int i = 2; i <= sheet.LastRowNum; i++)
|
||||
{
|
||||
row = sheet.GetRow(i);
|
||||
DataRow dr = dt.NewRow();
|
||||
var dr = dt.NewRow();
|
||||
for (int j = 0; j < row.LastCellNum; j++)
|
||||
{
|
||||
var cellValue = row.GetCell(j);
|
||||
@@ -169,7 +140,6 @@ namespace ExcelTool
|
||||
datas.Add(t);
|
||||
}
|
||||
}
|
||||
|
||||
var binaryFilePath = Path.Combine(fileInfo.DirectoryName, $"{excelName}.bytes");
|
||||
FileManager.WriteBinaryDatasToFile(binaryFilePath, datas);
|
||||
return true;
|
||||
@@ -194,6 +164,15 @@ namespace ExcelTool
|
||||
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)
|
||||
//{
|
||||
@@ -205,20 +184,23 @@ namespace ExcelTool
|
||||
sb.Append("[Serializable]\n");
|
||||
sb.Append($"public class {excelName} : IBinarySerializable\n");
|
||||
sb.Append("{\n");
|
||||
foreach (var header in headers)
|
||||
for (int i = 0; i < headers.Count; i++)
|
||||
{
|
||||
var type = header.Item2.ToLower();
|
||||
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}", header.Item1));
|
||||
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}", header.Item1));
|
||||
sb.Append(string.Format("\tpublic List<List<float>> {0}", headers[i].Item1));
|
||||
}
|
||||
else
|
||||
{
|
||||
sb.Append(string.Format("\tpublic {0} {1}", header.Item2.ToLower(), header.Item1));
|
||||
sb.Append(string.Format("\tpublic {0} {1}", headers[i].Item2.ToLower(), headers[i].Item1));
|
||||
}
|
||||
sb.Append(" { get; set; }\n");
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using Spire.Xls;
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
|
||||
@@ -1 +1 @@
|
||||
e8f99ac55eff58f68485e1025b84f8bc7ff8a4e9
|
||||
3770a330c7ef4b58251471e824d29ae88622bbc0
|
||||
|
||||
@@ -16,3 +16,39 @@ C:\Users\d00605132\Desktop\ExcelTool\ExcelTool\bin\Debug\NPOI.dll
|
||||
C:\Users\d00605132\Desktop\ExcelTool\ExcelTool\bin\Debug\NPOI.OOXML.dll
|
||||
C:\Users\d00605132\Desktop\ExcelTool\ExcelTool\bin\Debug\NPOI.OpenXml4Net.dll
|
||||
C:\Users\d00605132\Desktop\ExcelTool\ExcelTool\bin\Debug\NPOI.OpenXmlFormats.dll
|
||||
D:\Work\CodeHubG\ExcelTool\ExcelTool\bin\Debug\ExcelTool.exe.config
|
||||
D:\Work\CodeHubG\ExcelTool\ExcelTool\bin\Debug\ExcelTool.exe
|
||||
D:\Work\CodeHubG\ExcelTool\ExcelTool\bin\Debug\ExcelTool.pdb
|
||||
D:\Work\CodeHubG\ExcelTool\ExcelTool\bin\Debug\ICSharpCode.SharpZipLib.dll
|
||||
D:\Work\CodeHubG\ExcelTool\ExcelTool\bin\Debug\NPOI.dll
|
||||
D:\Work\CodeHubG\ExcelTool\ExcelTool\bin\Debug\NPOI.OOXML.dll
|
||||
D:\Work\CodeHubG\ExcelTool\ExcelTool\bin\Debug\NPOI.OpenXml4Net.dll
|
||||
D:\Work\CodeHubG\ExcelTool\ExcelTool\bin\Debug\NPOI.OpenXmlFormats.dll
|
||||
D:\Work\CodeHubG\ExcelTool\ExcelTool\bin\Debug\Spire.Pdf.dll
|
||||
D:\Work\CodeHubG\ExcelTool\ExcelTool\bin\Debug\Spire.XLS.dll
|
||||
D:\Work\CodeHubG\ExcelTool\ExcelTool\bin\Debug\Microsoft.mshtml.dll
|
||||
D:\Work\CodeHubG\ExcelTool\ExcelTool\bin\Debug\Spire.Pdf.xml
|
||||
D:\Work\CodeHubG\ExcelTool\ExcelTool\bin\Debug\Spire.XLS.xml
|
||||
D:\Work\CodeHubG\ExcelTool\ExcelTool\obj\Debug\ExcelTool.csprojAssemblyReference.cache
|
||||
D:\Work\CodeHubG\ExcelTool\ExcelTool\obj\Debug\ExcelTool.csproj.CoreCompileInputs.cache
|
||||
D:\Work\CodeHubG\ExcelTool\ExcelTool\obj\Debug\ExcelTool.csproj.CopyComplete
|
||||
D:\Work\CodeHubG\ExcelTool\ExcelTool\obj\Debug\ExcelTool.exe
|
||||
D:\Work\CodeHubG\ExcelTool\ExcelTool\obj\Debug\ExcelTool.pdb
|
||||
D:\Study\github\ExcelTool\ExcelTool\bin\Debug\ExcelTool.exe.config
|
||||
D:\Study\github\ExcelTool\ExcelTool\bin\Debug\ExcelTool.exe
|
||||
D:\Study\github\ExcelTool\ExcelTool\bin\Debug\ExcelTool.pdb
|
||||
D:\Study\github\ExcelTool\ExcelTool\bin\Debug\ICSharpCode.SharpZipLib.dll
|
||||
D:\Study\github\ExcelTool\ExcelTool\bin\Debug\NPOI.dll
|
||||
D:\Study\github\ExcelTool\ExcelTool\bin\Debug\NPOI.OOXML.dll
|
||||
D:\Study\github\ExcelTool\ExcelTool\bin\Debug\NPOI.OpenXml4Net.dll
|
||||
D:\Study\github\ExcelTool\ExcelTool\bin\Debug\NPOI.OpenXmlFormats.dll
|
||||
D:\Study\github\ExcelTool\ExcelTool\bin\Debug\Spire.Pdf.dll
|
||||
D:\Study\github\ExcelTool\ExcelTool\bin\Debug\Spire.XLS.dll
|
||||
D:\Study\github\ExcelTool\ExcelTool\bin\Debug\Microsoft.mshtml.dll
|
||||
D:\Study\github\ExcelTool\ExcelTool\bin\Debug\Spire.Pdf.xml
|
||||
D:\Study\github\ExcelTool\ExcelTool\bin\Debug\Spire.XLS.xml
|
||||
D:\Study\github\ExcelTool\ExcelTool\obj\Debug\ExcelTool.csprojAssemblyReference.cache
|
||||
D:\Study\github\ExcelTool\ExcelTool\obj\Debug\ExcelTool.csproj.CoreCompileInputs.cache
|
||||
D:\Study\github\ExcelTool\ExcelTool\obj\Debug\ExcelTool.csproj.CopyComplete
|
||||
D:\Study\github\ExcelTool\ExcelTool\obj\Debug\ExcelTool.exe
|
||||
D:\Study\github\ExcelTool\ExcelTool\obj\Debug\ExcelTool.pdb
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user