Ejemplo n.º 1
0
        /// <summary>
        /// 传入的DataTable和model的列比对 返回model中存在的列的集合
        /// </summary>
        /// <typeparam name="Model"></typeparam>
        /// <param name="dt">行数据源</param>
        /// <param name="model">传入的Model</param>
        /// <returns> 返回model中存在的列的集合</returns>
        public static List <string> ExistList <Model>(DataTable dt, Model model)
        {
            try
            {
                DataColumnCollection columns = dt.Columns;

                Type           ModelType     = typeof(Model);
                PropertyInfo[] ModelTypeProp = ModelType.GetProperties();
                //取出当前传入的Model的字段
                List <string> NameList = new List <string>();
                foreach (PropertyInfo item in ModelTypeProp)
                {
                    ExcelColumAttribute attribute = item.GetCustomAttributes(false).Where(o => o.GetType() == typeof(ExcelColumAttribute)).FirstOrDefault() as ExcelColumAttribute;
                    if (attribute != null)
                    {
                        NameList.Add(attribute.ColumName);
                    }
                }
                //取出数据库有的字段(数据库的字段必须和Model的字段一直 大小写都要一致)
                List <string> Exist = new List <string>();
                foreach (DataColumn item in columns)
                {
                    string ColumName = item.ToString();
                    if (NameList.Contains(ColumName))
                    {
                        Exist.Add(ColumName);
                    }
                }
                return(Exist);
            }
            catch (Exception ex)
            {
                while (ex.InnerException != null)
                {
                    ex = ex.InnerException;
                }
                throw new Exception(ex.Message);
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// 返回一个传入的Model
 /// </summary>
 /// <typeparam name="Model"></typeparam>
 /// <param name="dt">行数据源</param>
 /// <param name="model">传入的Model</param>
 /// <param name="Exist">当前Model中的值</param>
 /// <returns>将行数据转化成Model返回</returns>
 public static Model AddModel <Model>(DataRow dt, Model model, List <string> Exist)
 {
     try
     {
         Type           ModelType     = typeof(Model);
         PropertyInfo[] ModelTypeProp = ModelType.GetProperties();
         //循环给我们要附加的列进行赋值
         if (Exist.Count > 0)
         {
             foreach (PropertyInfo propertyInfo in ModelTypeProp)
             {
                 ExcelColumAttribute attribute = propertyInfo.GetCustomAttributes(false).Where(o => o.GetType() == typeof(ExcelColumAttribute)).FirstOrDefault() as ExcelColumAttribute;
                 if (attribute != null)
                 {
                     if (Exist.Contains(attribute.ColumName.Trim().ToLower()) && !string.IsNullOrEmpty(dt[attribute.ColumName].ToString()))
                     {
                         try
                         {
                             string tp = propertyInfo.PropertyType.Name; //获得对象的属性类型
                                                                         //处理时间格式
                             if (tp.Contains("Nullable"))
                             {
                                 Type[] TTModel  = propertyInfo.PropertyType.GetGenericArguments();
                                 string TypeName = TTModel[0].FullName.ToLower();
                                 if (TypeName.Contains("int32"))
                                 {
                                     int value = Convert.ToInt32(dt[attribute.ColumName]);
                                     propertyInfo.SetValue(model, value, null);//赋值的对象
                                 }
                                 else if (TypeName.Contains("datetime"))
                                 {
                                     DateTime value = Convert.ToDateTime(dt[attribute.ColumName]);
                                     propertyInfo.SetValue(model, value, null);//赋值的对象
                                 }
                                 else if (TypeName.Contains("double"))
                                 {
                                     double value = Convert.ToDouble(dt[attribute.ColumName]);
                                     propertyInfo.SetValue(model, value, null);//赋值的对象
                                 }
                                 else
                                 {
                                     string value = dt[attribute.ColumName].ToString();
                                     if (!string.IsNullOrEmpty(value))
                                     {
                                         propertyInfo.SetValue(model, value, null);//赋值的对象
                                     }
                                 }
                             }
                             //处理bool
                             else if (tp.Contains("Boolean"))
                             {
                                 bool value = dt[attribute.ColumName].ToString() == "真" ? true : false;
                                 propertyInfo.SetValue(model, value, null);//赋值的对象
                             }
                             else if (tp.Trim().ToLower().Contains("int"))
                             {
                                 int value = Convert.ToInt32(dt[attribute.ColumName].ToString());
                                 propertyInfo.SetValue(model, value, null);//赋值的对象
                             }
                             else if (tp.Trim().ToLower().Contains("datetime"))
                             {
                                 DateTime value = Convert.ToDateTime(dt[attribute.ColumName]);
                                 propertyInfo.SetValue(model, value, null);//赋值的对象
                             }
                             else if (tp.Trim().ToLower().Contains("double"))
                             {
                                 double value = Convert.ToDouble(dt[attribute.ColumName]);
                                 propertyInfo.SetValue(model, value, null);//赋值的对象
                             }
                             //其他格式
                             else
                             {
                                 string value = dt[attribute.ColumName].ToString();
                                 propertyInfo.SetValue(model, value, null);//赋值的对象
                             }
                         }
                         catch (Exception ex)
                         {
                             throw new Exception($"第【{row}】行,【{attribute.ColumName}】数据格式错误!");
                         }
                     }
                 }
             }
         }
         return(model);
     }
     catch (Exception ex)
     {
         while (ex.InnerException != null)
         {
             ex = ex.InnerException;
         }
         throw new Exception(ex.Message);
     }
 }