版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_18192161/article/details/79220570
需求:
假如有我有这些配置表:
想要生成txt配置表文件和cs解析文件,结果:
cs解析文件:
txt配置文件
1.下载Excel.dll文件,应用到工程中
2.创建ExcelReader类,读取excel文件
using System;
using Excel;
public class ExcelReader
{
public void OpenExcel(string strFileName,Action<Worksheet> OnWorkSheet)
{
object missing = System.Reflection.Missing.Value;
Application excel = new Application();
if (excel == null) return;
excel.Visible = false;excel.UserControl = true;
Workbook wb = excel.Application.Workbooks.Open(strFileName, missing, true, missing, missing, missing, missing, missing, missing, true, missing, missing, missing, missing, missing);
for (int i = 0; i < wb.Worksheets.Count; i++)
{
Worksheet ws = wb.Worksheets.get_Item(i+1);
if (ws != null&& OnWorkSheet!=null)
OnWorkSheet(ws);
}
excel.Quit();
excel = null;
}
}
ConfigData类:
// 存储类的属性及模板
public class ConfigData
{
public string ConfigName;
public List<ConfigInfo> ConfigProperty = new List<ConfigInfo>();
public Dictionary<int, List<ConfigInfo>> ConfigDatas = new Dictionary<int, List<ConfigInfo>>();
public string GetClass
{
get {
string c = "using UnityEngine;\nusing System;\nusing System.Collections;\nusing System.Collections.Generic;\n\n";
c += string.Format("public class {0} : TxtConfig<{1}> \n", ConfigName, ConfigName);
c += "{";
for (int i = 0; i < ConfigProperty.Count; i++)
c += ConfigProperty[i].GetProperty;
c += "\nprotected override void Parse(string[] ary)\n{\n";
{
for (int i = 0; i < ConfigProperty.Count; i++)
c += "\t"+ConfigProperty[i].GetParse + "\n";
}
c += "}\n";
c += "}";
return c;
}
}
}
ConfigInfo类:
//属性信息等
public class ConfigInfo
{
public int Index;
public string Type;
public string Name;
public string Des;
public string Data;
public string GetProperty
{
get {
return "\n///\n// "+Des+"\n///\npublic "+Type+" "+Name+"{get;private set;} \n";
}
}
public string GetParse
{
get {
return Parse;
}
}
private string Parse
{
get {
switch (Type)
{
case "string":
return string.Format("{0} = ary[{1}];",Name, Index);
case "string[]":
return string.Format("{0} = ary[{1}].Split(',');",Name,Index);
case "int":
return string.Format("{0} = int.Parse(ary[{1}]);",Name, Index);
case "float":
return string.Format("{0} = float.Parse(ary[{1}]);",Name, Index);
case "double":
return string.Format("{0} = double.Parse(ary[{1}]);", Name,Index);
case "bool":
return string.Format("{0} = bool.Parse(ary[{1}]);",Name,Index);
case "Vector2":
string v2 = "\n\tstring[] " + Name + string.Format("Ary = ary[{0}].Split(',');\n",Index);
v2 += string.Format("\t{0} = new Vector2(float.Parse({0}Ary[0]),float.Parse({0}Ary[1]));",Name);
return v2;
case "Vector3":
string v3 = "\n\tstring[] " + Name + string.Format("Ary = ary[{0}].Split(',');\n",Index);
v3 += string.Format("\t{0} = new Vector3(float.Parse({0}Ary[0]),float.Parse({0}Ary[1]),float.Parse({0}Ary[2]));",Name);
return v3;
}
return "";
}
}
}
主类:
class Program
{
static void Main(string[] args)
{
ExcelReader er = new ExcelReader();
er.OpenExcel(System.Environment.CurrentDirectory +@"\Data.xlsx", delegate (Worksheet ws)
{
Console.WriteLine("配置表 {0} 开始解析....",ws.Name);
ConfigData cd= new ConfigData();
cd.ConfigName = ws.Name;
int rowsint = ws.UsedRange.Cells.Rows.Count;
int colsint = ws.UsedRange.Cells.Columns.Count;
if (rowsint < 1 || colsint < 1) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Table {0} is Null", ws.Name); Console.ForegroundColor = ConsoleColor.White; return; }
for (int i = 1; i <= colsint; i++)
{
string colName = ws.UsedRange.Cells[1, i].Text.ToString();
string colDes = ws.UsedRange.Cells[2, i].Text.ToString();
string[] strAry = colName.Split(':');
if (strAry.Length < 2) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Table {0} Col {1} IsError", ws.Name,colName); Console.ForegroundColor = ConsoleColor.White; return; }
ConfigInfo ci = new ConfigInfo();
ci.Index = i - 1;
ci.Type = strAry[1];
ci.Name = strAry[0];
ci.Des = colDes;
ci.Data = colName;
cd.ConfigProperty.Add(ci);
}
for (int i = 1; i <=rowsint; i++)
{
cd.ConfigDatas.Add(i, new List<ConfigInfo>());
for (int j = 1; j <= colsint; j++)
{
ConfigInfo ci = new ConfigInfo();
ci.Data = ws.UsedRange.Cells[i, j].Text.ToString();
cd.ConfigDatas[i].Add(ci);
}
}
Console.WriteLine("配置表 {0} 解析完成,共 {1} 行,{2} 列,准备写入文件....", ws.Name,rowsint,colsint);
WriteTxt(cd);
WriteCs(cd);
Console.WriteLine();
});
Console.WriteLine("按任意键退出...");
Console.ReadKey();
}
static void WriteTxt(ConfigData cd)
{
Console.WriteLine("配置表 {0} 正在生成txt配置文档...",cd.ConfigName);
string data = "";
foreach (var value in cd.ConfigDatas)
{
List<ConfigInfo> ci = value.Value;
for (int i = 0; i < ci.Count; i++)
{
if (i != ci.Count - 1)
data += ci[i].Data + "\t";
else
data += ci[i].Data + "\n";
}
}
if (!Directory.Exists(TxtPath))
Directory.CreateDirectory(TxtPath);
if(!File.Exists(TxtPath + cd.ConfigName + ".txt"))
File.Create(TxtPath + cd.ConfigName + ".txt");
File.WriteAllText(TxtPath+cd.ConfigName+".txt", data);
Console.WriteLine("配置表 {0} txt配置文档生成完毕...", cd.ConfigName);
}
static void WriteCs(ConfigData cd)
{
Console.WriteLine("配置表 {0} 正在生成cs解析文件...", cd.ConfigName);
if (!Directory.Exists(CsPath))
Directory.CreateDirectory(CsPath);
if (!File.Exists(CsPath + cd.ConfigName + ".cs"))
File.Create(CsPath + cd.ConfigName + ".cs");
File.WriteAllText(CsPath + cd.ConfigName + ".cs", cd.GetClass);
Console.WriteLine("配置表 {0} cs解析文件生成完毕...", cd.ConfigName);
}
static string TxtPath
{
get {
return Environment.CurrentDirectory + @"\txt\";
}
}
static string CsPath
{
get {
return Environment.CurrentDirectory + @"\cs\";
}
}
}
最后将Excel文件放到debug目录下,运行后就可得到txt文件和cs文件。
配置表书写格式