MiniExcel icon indicating copy to clipboard operation
MiniExcel copied to clipboard

对可null类型的支持,比如(int? ) (Support for null types (e.g. Int?))

Open ping9719 opened this issue 2 years ago • 6 comments

excle中没有填写的可空类型的自动赋值为null:

image

ping9719 avatar Dec 07 '21 07:12 ping9719

我看了一下代码,修改源代码 可以实现为(modify the code) 【TypeHelper.cs】

` private static object TypeMappingImpl<T>(T v, ExcelCustomPropertyInfo pInfo, ref object newValue, object itemValue) where T : class, new() { var vs = itemValue?.ToString();

var definition = pInfo.ExcludeNullableType.GetGenericTypeDefinition();
if (definition != null && definition == typeof(Nullable<>))
{
    if (string.IsNullOrWhiteSpace(vs))
        newValue = null;
}
else
{
    if (pInfo.ExcludeNullableType == typeof(Guid))
    {
        newValue = Guid.Parse(vs);
    }
    else if (pInfo.ExcludeNullableType == typeof(DateTime))
    {
        // fix issue 257 https://github.com/shps951023/MiniExcel/issues/257
        if (itemValue is DateTime || itemValue is DateTime?)
        {
            newValue = itemValue;
            pInfo.Property.SetValue(v, newValue);
            return newValue;
        }

        if (pInfo.ExcelFormat != null)
        {
            if (DateTime.TryParseExact(vs, pInfo.ExcelFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out var _v))
            {
                newValue = _v;
            }
        }
        else if (DateTime.TryParse(vs, CultureInfo.InvariantCulture, DateTimeStyles.None, out var _v))
            newValue = _v;
        else if (DateTime.TryParseExact(vs, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out var _v2))
            newValue = _v2;
        else if (double.TryParse(vs, NumberStyles.None, CultureInfo.InvariantCulture, out var _d))
            newValue = DateTimeHelper.FromOADate(_d);
        else
            throw new InvalidCastException($"{vs} can't cast to datetime");
    }
    else if (pInfo.ExcludeNullableType == typeof(bool))
    {
        if (vs == "1")
            newValue = true;
        else if (vs == "0")
            newValue = false;
        else
            newValue = bool.Parse(vs);
    }
    else if (pInfo.Property.PropertyType == typeof(string))
    {
        newValue = XmlEncoder.DecodeString(vs);
    }
    else if (pInfo.Property.PropertyType.IsEnum)
    {
        newValue = Enum.Parse(pInfo.Property.PropertyType, vs, true);
    }
    else
    {
        // Use pInfo.ExcludeNullableType to resolve : https://github.com/shps951023/MiniExcel/issues/138
        newValue = Convert.ChangeType(itemValue, pInfo.ExcludeNullableType);
    }
}

if (pInfo.Property.SetMethod?.IsPublic == true)
    pInfo.Property.SetValue(v, newValue);
return newValue;

} `

ping9719 avatar Dec 08 '21 03:12 ping9719

@ping9719 感谢,我之前好像修正后,回复在QQ群,这边没有回复 目前测试是正常没问题,麻烦有问题再告知 image

shps951023 avatar Mar 18 '22 08:03 shps951023

如果带了空格就有问题,带空格的字段映射不到int?属性。应该怎么处理呢?

ganjiayi avatar Apr 18 '22 07:04 ganjiayi

请问测试这个功能的版本在哪里呢,我也想测试一下可空类型

elvisw avatar Apr 25 '22 02:04 elvisw

我忘记我当时是什么版本了 你可以用最新版试试

------------------ 原始邮件 ------------------ 发件人: "Elvis @.>; 发送时间: 2022年4月25日(星期一) 上午10:50 收件人: @.>; 抄送: @.>; @.>; 主题: Re: [MiniExcel/MiniExcel] 对可null类型的支持,比如(int? ) (Support for null types (e.g. Int?)) (Issue #310)

请问测试这个功能的版本在哪里呢,我也想测试一下可空类型

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.Message ID: @.***>

ping9719 avatar Apr 25 '22 02:04 ping9719

@ping9719 目前的最新版还不支持可空类型

elvisw avatar Apr 25 '22 13:04 elvisw