Esempio n. 1
0
 /// <summary>
 /// 将Row实例化为T
 /// </summary>
 /// <typeparam name="T">需要实例化的类</typeparam>
 /// <param name="row"></param>
 /// <returns></returns>
 public static T RowToModel <T>(IRow row)
 {
     if (row != null)
     {
         Type model    = typeof(T);
         T    instance = (T)Activator.CreateInstance(model);
         foreach (var mprop in model.GetProperties())
         {
             if (mprop.IsDefined(typeof(ColIndexAttribute)))
             {
                 ColIndexAttribute ciAttr = mprop.GetCustomAttribute(typeof(ColIndexAttribute)) as ColIndexAttribute;
                 ICell             cell   = row.GetCell(ciAttr.Index);
                 mprop.SetValue(instance, ConvertCell(cell, mprop.PropertyType));
                 //mprop.SetValue(instance, row.GetCell(ciAttr.Index));
             }
             else
             {
                 throw new Exception(string.Format("类型{0}属性{1}未定义ColIndex", model.Name, mprop.Name));
             }
         }
         return(instance);
     }
     else
     {
         return(default(T));
     }
 }
Esempio n. 2
0
        /// <summary>
        /// 使用反射将T类型对象转换为IRow
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj">需要转换为IRow的对象</param>
        /// <param name="row">转换完成后的IRow</param>
        public static void ObjectToRow <T>(T obj, IRow row)
        {
            Type t = typeof(T);

            foreach (var item in t.GetProperties())
            {
                if (item.IsDefined(typeof(ColIndexAttribute)))
                {
                    ColIndexAttribute ciAttr = item.GetCustomAttribute <ColIndexAttribute>();
                    ICell             cell   = row.CreateCell(ciAttr.Index);
                    if (item.IsDefined(typeof(ColTypeAttribute)))
                    {
                        ColTypeAttribute ctAttr = item.GetCustomAttribute <ColTypeAttribute>();
                        switch (ctAttr.ColType)
                        {
                        case ColType.T_STR:
                            cell.SetCellType(CellType.String);
                            cell.SetCellValue((string)item.GetValue(obj));
                            break;

                        case ColType.T_BOOL:
                            cell.SetCellType(CellType.Boolean);
                            cell.SetCellValue((bool)item.GetValue(obj));
                            break;

                        case ColType.T_DATE:
                            cell.SetCellType(CellType.Numeric);
                            cell.SetCellValue((DateTime)item.GetValue(obj));
                            break;

                        case ColType.T_NUM:
                            cell.SetCellType(CellType.Numeric);
                            cell.SetCellValue(Convert.ToDouble(item.GetValue(obj)));
                            break;

                        default:
                            break;
                        }
                    }
                    else
                    {
                        cell.SetCellType(CellType.String);
                        cell.SetCellValue((item.GetValue(obj)).ToString());
                    }
                }
            }
        }
Esempio n. 3
0
 /// <summary>
 /// 根据类型设定标题行
 /// </summary>
 /// <param name="t"></param>
 /// <param name="row"></param>
 public static void TypeToHeadRow(Type t, IRow row)
 {
     foreach (var item in t.GetProperties())
     {
         if (item.IsDefined(typeof(ColIndexAttribute)))
         {
             ColIndexAttribute ciAttr = item.GetCustomAttribute <ColIndexAttribute>();
             ICell             cell   = row.CreateCell(ciAttr.Index);
             cell.SetCellType(CellType.String);
             if (item.IsDefined(typeof(ColNameAttribute)))
             {
                 ColNameAttribute cnAttr = item.GetCustomAttribute <ColNameAttribute>();
                 cell.SetCellValue(cnAttr.ColName);
             }
             else
             {
                 cell.SetCellValue(item.Name);
             }
         }
     }
 }