//数据验证
Validata<T>();
//转换为集合
DataTableToList<T>();
//数据异常处理
SetValue<T>();
//文件导入
ExcelImport();
 1     public class GenericExcelToList
 2     {
 3         public static List<ValidationResult> Validata<T>(List<T> lists) {
 4             var vrs = new List<ValidationResult>();
 5             for (int i = 0; i < lists.Count; i++) {
 6                 List<ValidationResult> vr = Validata(lists[i]);
 7                 foreach (var vd in vr) {
 8                     vd.ErrorMessage = "第" + (i + 1) + "行," + vd.ErrorMessage;
 9                 }
10                 vrs.AddRange(vr);
11                 if (vr.Count > 3)
12                 {
13                     vrs = vrs.GetRange(0, 3);
14                     vrs[vrs.Count - 1].ErrorMessage += "等......";
15                     break;
16                 }
17             }
18             return vrs;
19         }
20         public static List<ValidationResult> Validata<T>(T t)
21         {
22             var vc = new ValidationContext(t, null, null);
23             var vr = new List<ValidationResult>();
24             Validator.TryValidateObject(t, vc, vr, trure);
25             return vr;
26         }
27         public static void SetValue<T>(PropertyInfo property, string value, T t) {
28             string type = property.PropertyType.Name.ToLower();
29             if (type == "nullable`1")
30                 type = property.PropertyType.GetGenericArguments()[0].Name.ToLower();
31             switch (type) { 
32                 case "int32":
33                     property.SetValue(t,Convert.ToInt32(value),null);
34                     break;
35                 case "string":
36                     property.SetValue(t,value,null);
37                     break;
38                 case "double":
39                     property.SetValue(t, Convert.ToDouble(value), null);
40                     break;
41                 case "decimal":
42                     property.SetValue(t, Convert.ToDecimal(value), null);
43                     break;
44                 case "datetime":
45                     property.SetValue(t, Convert.ToDateTime(value), null);
46                     break;
47                 default:
48                     property.SetValue(t, value, null);
49                     break;
50             }
51         }
52         public static void DataTableToList<T>(string keys, List<T> lists, DataTable dt) where T : new() {
53             string[] splitKeys = keys.Split(‘,‘);
54             PropertyInfo[] properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
55             for (var row = 0; row < dt.Rows.Count; row++) {
56                 var t = new T();
57                 for (var column = 0; column < dt.Columns.Count; column++) {
58                     try
59                     {
60                         var value = dt.Rows[row][column].ToString();
61                         if (value == "") continue;
62                         if (column > splitKeys.Count()) break;
63                         PropertyInfo property = properties.FirstOrDefault(m => m.Name == splitKeys[column]);
64                         if (property == null) continue;
65                         SetValue(property, value, t);
66                     }
67                     catch (Exception e)
68                     {
69                         e.HelpLink=