源码更新:加入导出C#
百度云:链接:https://pan.baidu.com/s/1Vcg8OCr4eczalMPdFqiOTg 提取码:h3bz
CSDN:https://download.csdn.net/download/sun_zeliang/11389398
工具:
百度云:链接:https://pan.baidu.com/s/1E1CjIU2ogeG8RRonx-B-Yw 提取码:9c5i
CSDN:https://download.csdn.net/download/sun_zeliang/11389399
使用的过程中觉得要加上导出C#代码,不然每次调整表结构都得重新修改C#类,太麻烦了。
接下来将导出C#代码写一下。
主要代码:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ExcelTool
{
public static class CSharpHelper
{
/// <summary>
/// 导出一个C#
/// </summary>
/// <param name="mdl_ExportXml">配置</param>
/// <param name="csharpPath">输出目录</param>
public static void ExportOneCSharp(Mdl_ExportXml mdl_ExportXml, string csharpPath)
{
string csharpFileName = mdl_ExportXml.CSharpFileName; //C#类名
List<string> csharpColumnNameList = mdl_ExportXml.CSharpColumnNameList;
List<string> csharpColumnTypeList = mdl_ExportXml.CSharpColumnTypeList;
List<string> excelColumnNameList = mdl_ExportXml.ExcelColumnNameList;
string excelName = mdl_ExportXml.ExcelFileName.Replace(".xlsx", "");
//类模板
StringBuilder sbClass = new StringBuilder();
sbClass.Append("//======================================\r\n");
sbClass.Append("// \r\n");
sbClass.Append("// Export by ExcelTool \r\n");
sbClass.Append("// \r\n");
sbClass.Append("// 此代码为工具根据配置自动生成, 建议不要修改 \r\n");
sbClass.Append("// \r\n");
sbClass.Append("//======================================\r\n");
sbClass.Append(" \r\n");
sbClass.Append("using System; \r\n");
sbClass.Append("using UnityEngine; \r\n");
sbClass.Append(" \r\n");
sbClass.Append("// [excelName] 实体类 \r\n");
sbClass.Append("public class [csharpFileName] \r\n");
sbClass.Append("{ \r\n");
sbClass.Append(" public [csharpFileName] () \r\n");
sbClass.Append(" { \r\n");
sbClass.Append(" } \r\n");
sbClass.Append(" [csharpColumn] \r\n");
sbClass.Append("} \r\n");
//类属性
StringBuilder sbColumn = new StringBuilder();
for (int i = 0; i < csharpColumnNameList.Count; i++)
{
string name = csharpColumnNameList[i]; //字段名
string type = csharpColumnTypeList[i]; //字段类型
string remark = excelColumnNameList[i]; //注释
string first = name.Substring(0, 1); //第一个字母
name = name.Substring(1, name.Length - 1); //不含第一个字母
string _f = first.ToLower() + name;
string pF = first.ToUpper() + name;
sbColumn.Append(" \r\n");
sbColumn.AppendFormat(" private {0} {1}; \r\n", type, _f);
sbColumn.AppendFormat(" //{0} \r\n", remark);
sbColumn.AppendFormat(" public {0} {1} \r\n", type, pF);
sbColumn.Append(" { \r\n");
sbColumn.Append(" get { return " + _f + "; } \r\n");
sbColumn.Append(" set { " + _f + " = value; } \r\n");
sbColumn.Append(" } \r\n");
}
string r = sbClass.ToString()
.Replace("[excelName]", excelName)
.Replace("[csharpFileName]", csharpFileName)
.Replace("[csharpColumn]", sbColumn.ToString());
CreateTxt(csharpPath + "\\" + csharpFileName + "Model.cs", csharpPath, r);
}
//生成cs文件
private static void CreateTxt(string filePath, string folderPath, string fileContent)
{
if (!Directory.Exists(folderPath))//如果不存在就创建文件夹
Directory.CreateDirectory(folderPath);
FileStream fs = new FileStream(filePath, FileMode.Append, FileAccess.Write, FileShare.None);
StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.UTF8);
sw.Write(fileContent);
sw.Close();
fs.Close();
}
}
}
相应的配置Xml也要改一下,这边就不贴出来了,因为都和以前的写一起了
接下来是源码更新,老规矩,有百度云和CSDN两个版本,大家根据自己需要下载,都是一样的。