Beispiel #1
0
        public static object TypeMapping <T>(T v, ExcelColumnInfo pInfo, object newValue, object itemValue, int rowIndex, string startCell, Configuration _config) where T : class, new()
        {
            try
            {
                return(TypeMappingImpl(v, pInfo, ref newValue, itemValue, _config));
            }
            catch (Exception ex) when(ex is InvalidCastException || ex is FormatException)
            {
                var columnName    = pInfo.ExcelColumnName ?? pInfo.Property.Name;
                var startRowIndex = ReferenceHelper.ConvertCellToXY(startCell).Item2;
                var errorRow      = startRowIndex + rowIndex + 1;

                throw new ExcelInvalidCastException(columnName, errorRow, itemValue, pInfo.Property.PropertyType, $"ColumnName : {columnName}, CellRow : {errorRow}, Value : {itemValue}, it can't cast to {pInfo.Property.PropertyType.Name} type.");
            }
        }
Beispiel #2
0
        private static object TypeMappingImpl <T>(T v, ExcelColumnInfo pInfo, ref object newValue, object itemValue, Configuration _config) where T : class, new()
        {
            if (pInfo.ExcludeNullableType == typeof(Guid))
            {
                newValue = Guid.Parse(itemValue.ToString());
            }
            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);
                }

                var vs = itemValue?.ToString();
                if (pInfo.ExcelFormat != null)
                {
                    if (DateTime.TryParseExact(vs, pInfo.ExcelFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out var _v))
                    {
                        newValue = _v;
                    }
                }
                else if (DateTime.TryParse(vs, _config.Culture, 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))
            {
                var vs = itemValue.ToString();
                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(itemValue?.ToString());
            }
            else if (pInfo.Property.PropertyType.IsEnum)
            {
                newValue = Enum.Parse(pInfo.Property.PropertyType, itemValue?.ToString(), true);
            }
            else
            {
                // Use pInfo.ExcludeNullableType to resolve : https://github.com/shps951023/MiniExcel/issues/138
                newValue = Convert.ChangeType(itemValue, pInfo.ExcludeNullableType, _config.Culture);
            }

            pInfo.Property.SetValue(v, newValue);
            return(newValue);
        }