Magicodes.IE
Magicodes.IE copied to clipboard
Excel导入对日期类型字段验证有误
版本
Magicodes.IE.Excel 2.5.6.3
导入数据

Dto定义
public class TestDto
{
[ImporterHeader(Name ="创建时间")]
public DateTime CreatedOn { get; set; }
[ImporterHeader(Name = "修改时间")]
public DateTime ModifiedOn { get; set; }
}
获取到的数据

问题描述
后台创建时间和修改时间分别定义了DateTime类型,为了验证是否能检查数据格式,在导入文件中填入了非日期格式的数字1和2,结果后台自动转换成了1989-12-31和1900-1-1,这样就没起到我想要的验证数据格式的目的。 既然字段定义了DateTime类型,那么导入文件里对应的值如果不是这个类型,是不是应该报错(hasError=true)?
Thank you for your feedback, we will follow it.
日期类型转换支持数值、字符串、TimeSpan,具体见ConvertUtil类的GetTypedCellValue
if (toType == typeof(DateTime))
{
if (value is double)
return (T)(object)(DateTime.FromOADate((double)value));
if (fromType == typeof(TimeSpan))
return ((T)(object)(new DateTime(((TimeSpan)value).Ticks)));
if (fromType == typeof(string))
return (T)(object)DateTime.Parse(value.ToString());
}
目前考虑是否添加特性以启用严格的日期格式。