C# Excel 转 Txt 配置表 及 C# 解析文件
2018-01-31 18:10
218 查看
需求:
假如有我有这些配置表:
![](https://img-blog.csdn.net/20180131180143693?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMTgxOTIxNjE=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
想要生成txt配置表文件和cs解析文件,结果:
cs解析文件:
![](https://img-blog.csdn.net/20180131180345103?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMTgxOTIxNjE=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
txt配置文件
![](https://img-blog.csdn.net/20180131180418860?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMTgxOTIxNjE=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
1.下载Excel.dll文件,应用到工程中
2.创建ExcelReader类,读取excel文件
ConfigData类:
ConfigInfo类:
主类:
最后将Excel文件放到debug目录下,运行后就可得到txt文件和cs文件。
配置表书写格式
假如有我有这些配置表:
想要生成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 9c91 (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文件。
配置表书写格式
相关文章推荐
- C# 解析 Excel 并且生成 Csv 文件代码分析
- C#用Com组件解析Excel文件 然后删除Excel 并且彻底关闭Excel进程
- C#用Com组件解析Excel文件 然后删除Excel 并且彻底关闭Excel进程(公司实战项目经验)
- C#仪器数据文件解析-Excel文件(xls、xlsx)
- C# 各种导入 Excel 文件的数据的方法总结
- C# .Net :Excel NPOI导入导出操作教程之将Excel文件读取并写到数据库表,示例分享
- Excel文件的上传和解析
- C#中csv文件与DataTable互相导入处理实例解析
- 2012年11月23日-c#生成Excel文件在服务器并且可以导出到本地
- Java基础系列19:使用JXL或者POI生成和解析Excel文件
- [转] 从数据库中读取图片并导入Excel文件,C#方式
- C#中下载Excel文件(其实就是将Excel文件从一个路径Copy到另一个路径)
- 在使用C#调用COM组件操作Excel的时候阻止其他程序对此文件操作
- 用正则表达式解析C#文件
- 基于C#.net开发的hosts静态域名解析文件修改工具
- Java解析Excel文件并把数据存入数据库
- GridView导出到Excel或Word文件的C#代码
- C#中获取Excel文件中的表名
- C#创建(从数据库中导出)Excel文件(含Interop.Excel.Dll)
- 【C#.NET】将excel或将CSV文件中的sheet1导入到dataGridView1中 ,或者导入到DataTable,导入到DataSet