Пример #1
0
        public static XLDate operator -(XLDate d, TimeSpan t)
        {
            XLDate date = new XLDate(d);

            d.AddMilliseconds(-t.TotalMilliseconds);
            return(date);
        }
Пример #2
0
 public XLDate(XLDate xlDate)
 {
     _xlDate = xlDate._xlDate;
 }
Пример #3
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(ExcelDna.Integration.ExcelEmpty) || fromType == typeof(ExcelDna.Integration.ExcelError) || fromType == typeof(ExcelDna.Integration.ExcelMissing))
            {
                return(GetDefault(toType));
            }

            if (fromType == typeof(ExcelReference))
            {
                ExcelReference 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));
            }
            else 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));
            }
            else 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));
            }
            else if (toType.IsEnum)
            {
                try
                {
                    return(Enum.Parse(toType, vt.ToString(), true));
                }
                catch (Exception)
                {
                    return(GetDefault(toType));
                }
            }
            else
            {
                return(Convert.ChangeType(vt, toType));
            }
        }