zoukankan      html  css  js  c++  java
  • 根据Excal表生成代码

    Excal格式要求

    第一行是类型名   第二行是类型

    例如: string  name;   string是类型,name是类型名。

    具体代码

        [MenuItem("Tools/ExcalToModel.cs")]
        public static void GenerationModel()
        {
    
            string selectPath = AssetDatabase.GetAssetOrScenePath(Selection.activeObject);
    
            Debug.Log(selectPath);
            string className = selectPath.Substring(selectPath.LastIndexOf('/') + 1, selectPath.LastIndexOf('.') - selectPath.LastIndexOf('/') - 1);
            string fileName = className + ".cs";
            string[] name;
            string[] pro;
            //读取Excal
            using (FileStream fs = File.Open(selectPath, FileMode.Open))
            {
                IExcelDataReader excelDataReader = ExcelReaderFactory.CreateOpenXmlReader(fs);
                DataSet dataSet = excelDataReader.AsDataSet();
                int cow = dataSet.Tables[0].Columns.Count;
                name = new string[cow];  //名字
                pro = new string[cow];     //类型
                for (int i = 0; i < cow; i++)
                {
                    name[i] = dataSet.Tables[0].Rows[0][i].ToString();
                    pro[i] = dataSet.Tables[0].Rows[1][i].ToString();
                    Debug.Log("name="+ name[i]+"_____"+"pro="+pro[i]);
                }
            }
            string path = Application.dataPath + "/Scripts/Model/";
            if (!System.IO.Directory.Exists(path))
            {
                System.IO.Directory.CreateDirectory(path);
            }
            path += fileName;
            if (File.Exists(path))
                File.Delete(path);
            using (FileStream fs = File.Open(path, FileMode.OpenOrCreate))
            {
                string code = "using System;
    " +
                    " 
    " +
                $"public class {className}
    " +
                "{
    ";
                for (int i = 0; i < name.Length; i++)
                {
                    code += $"    public {pro[i]} {name[i]}"+ "{get;set;}
    ";
                }
                code += "}
    ";
                fs.Write(System.Text.UTF8Encoding.UTF8.GetBytes(code),0, System.Text.UTF8Encoding.UTF8.GetBytes(code).Length);
            }
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
        }

    使用方式

    文件夹选中需要生成的Excal(excal后缀名必须是xlsx才可以用IExcelDataReader 读取),点击Tools/ExcalToModel.cs 。

    生成结果

    using System;
     
    public class WarSkill
    {
        public string name{get;set;}
        public int damage{get;set;}
        public int coolDown{get;set;}
        public SkillType SkillType{get;set;}
        public int duringTime{get;set;}
        public int SuperArmordefense{get;set;}
        public int SuperArmorAttack{get;set;}
        public int SkilPoint{get;set;}
        public Element Element{get;set;}
        public string describe{get;set;}
    }
  • 相关阅读:
    二分图的最大匹配-hdu-3729-I'm Telling the Truth
    hdu3308LCIS(线段树,点更新,段查寻,查寻时一定要注意跨越时如何计算)
    小智慧58
    【开源项目】Android 手写记事 App(半成品)
    持续集成之戏说Check-in Dance
    XSS与字符编码的那些事儿
    十大渗透测试演练系统
    Google DNS劫持背后的技术分析
    黑客是怎样绕过WAF之三重防护绕过讲解
    Seay工具分享
  • 原文地址:https://www.cnblogs.com/DazeJiang/p/14349173.html
Copyright © 2011-2022 走看看