Esempio n. 1
0
        public static object ConvertTo(this object vt, Type toType)
        {
            if (vt == null)
            {
                return(GetDefault(toType));
            }

            Type fromType = vt.GetType();

            if (fromType == typeof(DBNull))
            {
                return(GetDefault(toType));
            }

            if (fromType == typeof(ExcelEmpty) || fromType == typeof(ExcelError) || fromType == typeof(ExcelMissing))
            {
                return(GetDefault(toType));
            }

            if (fromType == typeof(ExcelReference))
            {
                var    r   = (ExcelReference)vt;
                object val = r.GetValue();
                return(ConvertTo(val, toType));
            }

            //acount for nullable types
            toType = Nullable.GetUnderlyingType(toType) ?? toType;

            if (toType == typeof(DateTime))
            {
                DateTime dt = DateTime.FromOADate(0.0);
                if (fromType == typeof(DateTime))
                {
                    dt = (DateTime)vt;
                }
                else if (fromType == typeof(double))
                {
                    dt = DateTime.FromOADate((double)vt);
                }
                else if (fromType == typeof(string))
                {
                    DateTime result;
                    if (DateTime.TryParse((string)vt, out result))
                    {
                        dt = result;
                    }
                }
                return(Convert.ChangeType(dt, toType));
            }
            if (toType == typeof(XlDate))
            {
                XlDate dt = 0.0;
                if (fromType == typeof(DateTime))
                {
                    dt = (DateTime)vt;
                }
                else if (fromType == typeof(double))
                {
                    dt = (double)vt;
                }
                else if (fromType == typeof(string))
                {
                    DateTime result;
                    if (DateTime.TryParse((string)vt, out result))
                    {
                        dt = result;
                    }
                    else
                    {
                        dt = 0.0;
                    }
                }
                else
                {
                    dt = (double)Convert.ChangeType(vt, typeof(double));
                }
                return(Convert.ChangeType(dt, toType));
            }
            if (toType == typeof(double))
            {
                double dt = 0.0;
                if (fromType == typeof(double))
                {
                    dt = (double)vt;
                }
                else if (fromType == typeof(DateTime))
                {
                    dt = ((DateTime)vt).ToOADate();
                }
                else if (fromType == typeof(string))
                {
                    double.TryParse((string)vt, out dt);
                }
                else
                {
                    dt = (double)Convert.ChangeType(vt, typeof(double));
                }
                return(Convert.ChangeType(dt, toType));
            }
            if (toType.IsEnum)
            {
                try{
                    return(Enum.Parse(toType, vt.ToString(), true));
                } catch (Exception) {
                    return(GetDefault(toType));
                }
            }
            return(Convert.ChangeType(vt, toType));
        }
Esempio n. 2
0
 public XlDate(XlDate xlDate)
 {
     _xlDate   = xlDate._xlDate;
     _dateTime = xlDate._dateTime;
 }
Esempio n. 3
0
        /// <summary>
        /// 类型转换
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="vt"></param>
        /// <returns></returns>
        public static T ConvertTo <T>(this object vt)
        {
            if (vt.IsNull())
            {
                return(default(T));
            }

            Type toType   = typeof(T);
            Type fromType = vt.GetType();

            var reference = vt as ExcelReference;

            if (reference != null)
            {
                object val = reference.GetValue();
                return(ConvertTo <T>(val));
            }

            //acount for nullable types
            toType = Nullable.GetUnderlyingType(toType) ?? toType;

            if (toType == typeof(DateTime))
            {
                DateTime dt = DateTime.FromOADate(0.0);
                if (fromType == typeof(DateTime))
                {
                    dt = (DateTime)vt;
                }
                else if (fromType == typeof(double))
                {
                    dt = DateTime.FromOADate((double)vt);
                }
                else if (fromType == typeof(string))
                {
                    if (DateTime.TryParse((string)vt, out var result))
                    {
                        dt = result;
                    }
                }
                //note this will work also if T is nullable
                return((T)Convert.ChangeType(dt, toType));
            }
            if (toType == typeof(XlDate))
            {
                XlDate dt = 0.0;
                if (fromType == typeof(DateTime))
                {
                    dt = (DateTime)vt;
                }
                else if (fromType == typeof(double))
                {
                    dt = (double)vt;
                }
                else if (fromType == typeof(string))
                {
                    if (DateTime.TryParse((string)vt, out var result))
                    {
                        dt = result;
                    }
                    else
                    {
                        dt = 0.0;
                    }
                }
                else
                {
                    dt = (double)Convert.ChangeType(vt, typeof(double));
                }
                return((T)Convert.ChangeType(dt, toType));
            }
            if (toType == typeof(double))
            {
                double dt = 0.0;
                if (fromType == typeof(double))
                {
                    dt = (double)vt;
                }
                else if (fromType == typeof(DateTime))
                {
                    dt = ((DateTime)vt).ToOADate();
                }
                else if (fromType == typeof(string))
                {
                    double.TryParse((string)vt, out dt);
                }
                else
                {
                    dt = (double)Convert.ChangeType(vt, typeof(double));
                }
                return((T)Convert.ChangeType(dt, toType));
            }
            if (toType.IsEnum)
            {
                try{
                    return((T)Enum.Parse(typeof(T), vt.ToString(), true));
                } catch (Exception) {
                    return(default(T));
                }
            }
            return((T)Convert.ChangeType(vt, toType));
        }