//数据验证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="导入数据第"+(row+1)+"行"+dt.Columns[column].ColumnName+"数据格式有误!"; 70 throw e; 71 } 72 } 73 lists.Add(t); 74 } 75 } 76 public static Boolean IsColumnNamesEqual(string keys, DataTable dt) 77 { 78 var columnNames = (from DataColumn column in dt.Columns select column.ColumnName).ToList(); 79 string dtColumnNames = string.Join(",", columnNames); 80 return keys == dtColumnNames; 81 } 82 public static Boolean IsColumnsEqual(string keys, DataTable dt) { 83 string[] splitKeys = keys.Split(‘,‘); 84 return splitKeys.Count() == dt.Columns.Count; 85 } 86 public static DataTable ExcelImport(string fileName) { 87 var book = new Workbook(fileName); 88 worksheet sheet = book.Worksheets[0]; 89 Cells cells = sheet.Cells; 90 DataTable dt = cells.ExportDataTableAsString(0, 0, cells.MaxDataRow + 1, cells.MaxDataColomn + 1, true); 91 return dt; 92 } 93 }
时间: 2024-10-13 17:29:24