读取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;
    }
    
原文地址:https://www.cnblogs.com/DazeJiang/p/14349679.html