zoukankan      html  css  js  c++  java
  • 读取Excal数据通过反射赋值

     第一行属性名字,第二行 类型。

     对于自定义类型需要在   TypeSetValue<T> 添加对应类型的转换。

    tableIndex  指的是Excal下图表的索引。

     //读取Excal
        public static List<T> ReadExcal<T>(string path,int tableIndex)where T:class,new()
        {
            List<T> list = new List<T>();
            using (FileStream fs=File.Open(path,FileMode.Open))
            {
               
                IExcelDataReader excelDataReader = ExcelReaderFactory.CreateOpenXmlReader(fs);
                DataSet dataSet = excelDataReader.AsDataSet();
                Type type = typeof(T);
                PropertyInfo[] propertyInfos = type.GetProperties();
                #region Debug
                //for (int i = 0; i < propertyInfos.Length; i++)
                //{
                //    Debug.Log(propertyInfos[i].Name);
                //}
                #endregion
                string[] proertyNames = GetProName(dataSet,tableIndex);
                string[] typeNames = GetTypeName(dataSet,tableIndex);
                int[] propertyProjectTable = MappedIndex(propertyInfos, proertyNames);
    
    
                for (int i = 2; i < dataSet.Tables[tableIndex].Rows.Count; i++)
                {
                    T t = new T();
                    for (int j = 0; j < dataSet.Tables[tableIndex].Columns.Count; j++)
                    {
                        string str = dataSet.Tables[tableIndex].Rows[i][j].ToString();
                        t= TypeSetValue(str,typeNames[j], t,GetPropertyName(propertyInfos, propertyProjectTable[j]));
                    }
                    list.Add(t);
                }
    
            }
           
            return list;
        }
        //读取Excal
        public static T[] ReadExcalArray<T>(string path,int tableIndex) where T : class, new()
        {
            return ReadExcal<T>(path, tableIndex).ToArray();
        }
        //获取Excal中第一行名字对应PropertyInfo[]的索引表
        public static int[] MappedIndex(PropertyInfo[] propertyInfos,string[] proertyNames)
        {
            int[] propertyProjectTable = new int[propertyInfos.Length];
    
            for (int i = 0; i < proertyNames.Length; i++)
            {
                for (int j = 0; j < propertyInfos.Length; j++)
                {
                    if (propertyInfos[j].Name == proertyNames[i])
                    {
                        propertyProjectTable[i] = j;
                        break;
                    }
                }
            }
            return propertyProjectTable;
        }
        //获取PropertyInfo名字
        public static string GetPropertyName(PropertyInfo[] propertyInfos,int index)
        {
            return propertyInfos[index].Name;
        }
        //实例化T赋值     
        public static T TypeSetValue<T>(string str,string typeName,T t,string propertyName) where T : class, new()
        {
            switch (typeName)
            {
                case "string":
                    t.GetType().GetProperty(propertyName).SetValue(t, str);
                    break;
                case "int":
                    t.GetType().GetProperty(propertyName).SetValue(t, int.Parse(str));
                    break;
                case "float":
                    t.GetType().GetProperty(propertyName).SetValue(t, float.Parse(str));
                    break;
                case "uint":
                    t.GetType().GetProperty(propertyName).SetValue(t, uint.Parse(str));
                    break;
                case "ArticleType":
                    t.GetType().GetProperty(propertyName).SetValue(t, Enum.Parse(typeof(ArticleType), str));
                    break;
                case "Element":
                    t.GetType().GetProperty(propertyName).SetValue(t, Enum.Parse(typeof(Element), str));
                    break;
                case "SkillType":
                    t.GetType().GetProperty(propertyName).SetValue(t, Enum.Parse(typeof(SkillType), str));
                    break;
                case "WeaponType":
                    t.GetType().GetProperty(propertyName).SetValue(t, Enum.Parse(typeof(WeaponType), str));
                    break;
            }
            return t;
        }
    
    
        //获取Excal中的第二行属性类型
        private static string[] GetTypeName(DataSet dataSet,int tableIndex)
        {
            string[] typeName = new string[dataSet.Tables[0].Columns.Count];
          
            for (int i = 0; i < dataSet.Tables[tableIndex].Columns.Count; i++)
            {
                typeName[i] = dataSet.Tables[tableIndex].Rows[1][i].ToString();
                
            }
            return typeName;
        }
        //获取Excal中的第一行属性名
        private static string[] GetProName(DataSet dataSet,int tableIndex)
        {
            string[] proertyName = new string[dataSet.Tables[tableIndex].Columns.Count];
          
            for (int i = 0; i < dataSet.Tables[tableIndex].Columns.Count; i++)
            {
                proertyName[i] = dataSet.Tables[tableIndex].Rows[0][i].ToString();
            }
            return proertyName;
        }
        
  • 相关阅读:
    android笔记5——同一个Activity中Fragment的切换
    JavaScript 刚開始学习的人应知的 24 条最佳实践
    位运算符之异或的化腐朽为奇妙
    unity常见问题之20题
    汉澳sinox不受openssl心血漏洞影响并分析修复其漏洞代码
    基于canvas和Web Audio的音频播放器
    poj2595(凸包)
    HDU 1257 最少拦截系统(dp)
    【iOS开发-33】学习手动内存管理临时抛弃ARC以及retain/assign知识——iOSproject师面试必考内容
    万能狗! 程序猿屌丝独自创业之路(一)
  • 原文地址:https://www.cnblogs.com/DazeJiang/p/14349679.html
Copyright © 2011-2022 走看看