Пример #1
0
        private object tryConvertOADateTime(double value, ushort XFormat)
        {
            XlsBiffFormatString str;
            ushort key = 0;

            if ((XFormat < 0) || (XFormat >= this.m_globals.ExtendedFormats.Count))
            {
                key = XFormat;
            }
            else
            {
                XlsBiffRecord  record = this.m_globals.ExtendedFormats[XFormat];
                BIFFRECORDTYPE iD     = record.ID;
                if (iD == BIFFRECORDTYPE.XF_V2)
                {
                    key = (ushort)(record.ReadByte(2) & 0x3f);
                }
                else if (iD == BIFFRECORDTYPE.XF_V3)
                {
                    if ((record.ReadByte(3) & 4) == 0)
                    {
                        return(value);
                    }
                    key = record.ReadByte(1);
                }
                else if (iD == BIFFRECORDTYPE.XF_V4)
                {
                    if ((record.ReadByte(5) & 4) == 0)
                    {
                        return(value);
                    }
                    key = record.ReadByte(1);
                }
                else
                {
                    if ((record.ReadByte(this.m_globals.Sheets[this.m_globals.Sheets.Count - 1].IsV8 ? 9 : 7) & 4) == 0)
                    {
                        return(value);
                    }
                    key = record.ReadUInt16(2);
                }
            }
            switch (key)
            {
            case 0:
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
            case 6:
            case 7:
            case 8:
            case 9:
            case 10:
            case 11:
            case 12:
            case 13:
            case 0x25:
            case 0x26:
            case 0x27:
            case 40:
            case 0x29:
            case 0x2a:
            case 0x2b:
            case 0x2c:
            case 0x30:
                return(value);

            case 14:
            case 15:
            case 0x10:
            case 0x11:
            case 0x12:
            case 0x13:
            case 20:
            case 0x15:
            case 0x16:
            case 0x2d:
            case 0x2e:
            case 0x2f:
                return(Helpers.ConvertFromOATime(value));

            case 0x31:
                return(value.ToString());
            }
            if (this.m_globals.Formats.TryGetValue(key, out str))
            {
                string       str2   = str.Value;
                FormatReader reader = new FormatReader {
                    FormatString = str2
                };
                if (reader.IsDateFormatString())
                {
                    return(Helpers.ConvertFromOATime(value));
                }
            }
            return(value);
        }
Пример #2
0
        private object tryConvertOADateTime(double value, ushort XFormat)
        {
            ushort format = 0;

            if (XFormat < m_globals.ExtendedFormats.Count)
            {
                XlsBiffRecord rec = m_globals.ExtendedFormats[XFormat];
                switch (rec.ID)
                {
                case BIFFRECORDTYPE.XF_V2:
                    format = (ushort)(rec.ReadByte(2) & 0x3F);
                    break;

                case BIFFRECORDTYPE.XF_V3:
                    if ((rec.ReadByte(3) & 4) == 0)
                    {
                        return(value);
                    }
                    format = rec.ReadByte(1);
                    break;

                case BIFFRECORDTYPE.XF_V4:
                    if ((rec.ReadByte(5) & 4) == 0)
                    {
                        return(value);
                    }
                    format = rec.ReadByte(1);
                    break;

                default:
                    if ((rec.ReadByte(m_globals.Sheets[m_globals.Sheets.Count - 1].IsV8 ? 9 : 7) & 4) == 0)
                    {
                        return(value);
                    }

                    format = rec.ReadUInt16(2);
                    break;
                }
            }
            else
            {
                format = XFormat;
            }


            switch (format)
            {
            // numeric built in formats
            case 0:     //"General";
            case 1:     //"0";
            case 2:     //"0.00";
            case 3:     //"#,##0";
            case 4:     //"#,##0.00";
            case 5:     //"\"$\"#,##0_);(\"$\"#,##0)";
            case 6:     //"\"$\"#,##0_);[Red](\"$\"#,##0)";
            case 7:     //"\"$\"#,##0.00_);(\"$\"#,##0.00)";
            case 8:     //"\"$\"#,##0.00_);[Red](\"$\"#,##0.00)";
            case 9:     //"0%";
            case 10:    //"0.00%";
            case 11:    //"0.00E+00";
            case 12:    //"# ?/?";
            case 13:    //"# ??/??";
            case 0x30:  // "##0.0E+0";

            case 0x25:  // "_(#,##0_);(#,##0)";
            case 0x26:  // "_(#,##0_);[Red](#,##0)";
            case 0x27:  // "_(#,##0.00_);(#,##0.00)";
            case 40:    // "_(#,##0.00_);[Red](#,##0.00)";
            case 0x29:  // "_(\"$\"* #,##0_);_(\"$\"* (#,##0);_(\"$\"* \"-\"_);_(@_)";
            case 0x2a:  // "_(\"$\"* #,##0_);_(\"$\"* (#,##0);_(\"$\"* \"-\"_);_(@_)";
            case 0x2b:  // "_(\"$\"* #,##0.00_);_(\"$\"* (#,##0.00);_(\"$\"* \"-\"??_);_(@_)";
            case 0x2c:  // "_(* #,##0.00_);_(* (#,##0.00);_(* \"-\"??_);_(@_)";
                return(value);

            // date formats
            case 14:     //this.GetDefaultDateFormat();
            case 15:     //"D-MM-YY";
            case 0x10:   // "D-MMM";
            case 0x11:   // "MMM-YY";
            case 0x12:   // "h:mm AM/PM";
            case 0x13:   // "h:mm:ss AM/PM";
            case 20:     // "h:mm";
            case 0x15:   // "h:mm:ss";
            case 0x16:   // string.Format("{0} {1}", this.GetDefaultDateFormat(), this.GetDefaultTimeFormat());

            case 0x2d:   // "mm:ss";
            case 0x2e:   // "[h]:mm:ss";
            case 0x2f:   // "mm:ss.0";
                return(value.ConvertFromOATime());

            case 0x31:     // "@";
                return(value.ToString(CultureInfo.InvariantCulture));

            default:
                XlsBiffFormatString fmtString;
                if (m_globals.Formats.TryGetValue(format, out fmtString))
                {
                    string fmt          = fmtString.Value;
                    var    formatReader = new FormatReader {
                        FormatString = fmt
                    };
                    if (formatReader.IsDateFormatString())
                    {
                        return(value.ConvertFromOATime());
                    }
                }
                return(value);
            }
        }