public static ExcelImportOption <T> Require <T, E>(this ExcelImportOption <T> origin, string field, Expression <Func <T, E> > prop, ExcelToEntityType type = ExcelToEntityType.Default, Func <string, object> action = null) { var option = GenOption(field, prop, type); var optionAction = option.Action; option.Action = item => { if (item.IsNullOfEmpty()) { throw new Exception($@" {field} 不可为空"); } if (optionAction == null) { return(item); } return(optionAction(item)); }; origin.FieldOption.Add(option); return(origin); }
public static ExcelImportOption <T> Add <T, E>(this ExcelImportOption <T> origin, string field, Expression <Func <T, E> > prop, ExcelToEntityType type = ExcelToEntityType.Default) { if (origin.FieldOption == null) { origin.FieldOption = new List <ExcelCellOption <T> >(); } origin.FieldOption.Add(GenOption(field, prop, type)); return(origin); }
public static ExcelCellOption <T> GenOption <T, E>(string field, Expression <Func <T, E> > prop, ExcelToEntityType type = ExcelToEntityType.Default) { Func <string, object> action = item => item; switch (type) { case ExcelToEntityType.Int32: action = item => { if (int.TryParse(item, out int value)) { return(value); } throw new Exception($@" {field} 需要是整数类型"); }; break; case ExcelToEntityType.Int64: action = item => { if (long.TryParse(item, out long value)) { return(value); } throw new Exception($@" {field} 需要是长整数类型"); }; break; case ExcelToEntityType.DateTime: action = item => { if (DateTime.TryParse(item, out DateTime value)) { return(value); } throw new Exception($@" {field} 需要是日期类型"); }; break; case ExcelToEntityType.Double: action = item => { if (double.TryParse(item, out double value)) { return(value); } throw new Exception($@" {field} 需要是浮点数类型"); }; break; case ExcelToEntityType.Default: break; default: break; } return(GenOption(field, prop, action)); }