Пример #1
0
        private void readWorkBookGlobals()
        {
            XlsBiffRecord record;
            bool          flag;

            try
            {
                this.m_hdr = XlsHeader.ReadHeader(this.m_file);
            }
            catch (HeaderException exception)
            {
                this.fail(exception.Message);
                return;
            }
            catch (FormatException exception2)
            {
                this.fail(exception2.Message);
                return;
            }
            XlsRootDirectory  rootDir = new XlsRootDirectory(this.m_hdr);
            XlsDirectoryEntry entry   = rootDir.FindEntry("Workbook") ?? rootDir.FindEntry("Book");

            if (entry != null)
            {
                if (entry.EntryType != STGTY.STGTY_STREAM)
                {
                    this.fail("Error: Workbook directory entry is not a Stream.");
                    return;
                }
                this.m_stream  = new XlsBiffStream(this.m_hdr, entry.StreamFirstSector, entry.IsEntryMiniStream, rootDir);
                this.m_globals = new XlsWorkbookGlobals();
                this.m_stream.Seek(0, SeekOrigin.Begin);
                XlsBiffBOF fbof = this.m_stream.Read() as XlsBiffBOF;
                if ((fbof == null) || (fbof.Type != BIFFTYPE.WorkbookGlobals))
                {
                    this.fail("Error reading Workbook Globals - Stream has invalid data.");
                    return;
                }
                flag           = false;
                this.m_version = fbof.Version;
                this.m_sheets  = new List <XlsWorksheet>();
            }
            else
            {
                this.fail("Error: Neither stream 'Workbook' nor 'Book' was found in file.");
                return;
            }
            while ((record = this.m_stream.Read()) != null)
            {
                BIFFRECORDTYPE iD = record.ID;
                if (iD <= BIFFRECORDTYPE.COUNTRY)
                {
                    switch (iD)
                    {
                    case BIFFRECORDTYPE.FORMAT_V23:
                    {
                        XlsBiffFormatString str = (XlsBiffFormatString)record;
                        str.UseEncoding = this.m_encoding;
                        this.m_globals.Formats.Add((ushort)this.m_globals.Formats.Count, str);
                        break;
                    }

                    case BIFFRECORDTYPE.FONT:
                        goto Label_031C;

                    case BIFFRECORDTYPE.EOF:
                        goto Label_03F3;

                    case BIFFRECORDTYPE.CODEPAGE:
                        goto Label_02E4;

                    case BIFFRECORDTYPE.XF_V2:
                        goto Label_0395;

                    case BIFFRECORDTYPE.CONTINUE:
                        goto Label_03C0;

                    case BIFFRECORDTYPE.BOUNDSHEET:
                    {
                        XlsBiffBoundSheet refSheet = (XlsBiffBoundSheet)record;
                        if (refSheet.Type == XlsBiffBoundSheet.SheetType.Worksheet)
                        {
                            refSheet.IsV8        = this.isV8();
                            refSheet.UseEncoding = this.m_encoding;
                            this.m_sheets.Add(new XlsWorksheet(this.m_globals.Sheets.Count, refSheet));
                            this.m_globals.Sheets.Add(refSheet);
                        }
                        break;
                    }

                    case BIFFRECORDTYPE.COUNTRY:
                        this.m_globals.Country = record;
                        break;
                    }
                }
                else if (iD <= BIFFRECORDTYPE.EXTSST)
                {
                    switch (iD)
                    {
                    case BIFFRECORDTYPE.SST:
                        this.m_globals.SST = (XlsBiffSST)record;
                        flag = true;
                        break;

                    case BIFFRECORDTYPE.EXTSST:
                        this.m_globals.ExtSST = record;
                        flag = false;
                        break;

                    case BIFFRECORDTYPE.XF:
                        goto Label_0395;

                    case BIFFRECORDTYPE.INTERFACEHDR:
                        this.m_globals.InterfaceHdr = (XlsBiffInterfaceHdr)record;
                        break;

                    case BIFFRECORDTYPE.MMS:
                        goto Label_02C0;
                    }
                }
                else if (iD <= BIFFRECORDTYPE.FONT_V34)
                {
                    if ((iD != BIFFRECORDTYPE.PROT4REVPASSWORD) && (iD == BIFFRECORDTYPE.FONT_V34))
                    {
                        goto Label_031C;
                    }
                }
                else
                {
                    switch (iD)
                    {
                    case BIFFRECORDTYPE.XF_V3:
                    case BIFFRECORDTYPE.XF_V4:
                        goto Label_0395;

                    case BIFFRECORDTYPE.FORMAT:
                    {
                        XlsBiffFormatString str2 = (XlsBiffFormatString)record;
                        this.m_globals.Formats.Add(str2.Index, str2);
                        break;
                    }
                    }
                }
                continue;
Label_02C0:
                this.m_globals.MMS = record;
                continue;
Label_02E4:
                this.m_globals.CodePage = (XlsBiffSimpleValueRecord)record;
                try
                {
                    this.m_encoding = Encoding.GetEncoding(this.m_globals.CodePage.Value);
                }
                catch (ArgumentException)
                {
                }
                continue;
Label_031C:
                this.m_globals.Fonts.Add(record);
                continue;
Label_0395:
                this.m_globals.ExtendedFormats.Add(record);
                continue;
Label_03C0:
                if (flag)
                {
                    XlsBiffContinue fragment = (XlsBiffContinue)record;
                    this.m_globals.SST.Append(fragment);
                }
                continue;
Label_03F3:
                if (this.m_globals.SST != null)
                {
                    this.m_globals.SST.ReadStrings();
                }
                return;
            }
        }
Пример #2
0
        public static XlsBiffRecord GetRecord(byte[] bytes, uint offset)
        {
            BIFFRECORDTYPE biffrecordtype = (BIFFRECORDTYPE)BitConverter.ToUInt16(bytes, (int)offset);

            if (biffrecordtype <= BIFFRECORDTYPE.MULBLANK)
            {
                switch (biffrecordtype)
                {
                case BIFFRECORDTYPE.BLANK_OLD:
                case BIFFRECORDTYPE.BOOLERR_OLD:
                    goto Label_01FA;

                case BIFFRECORDTYPE.INTEGER_OLD:
                    goto Label_021A;

                case BIFFRECORDTYPE.NUMBER_OLD:
                    goto Label_0222;

                case BIFFRECORDTYPE.LABEL_OLD:
                    goto Label_020A;

                case BIFFRECORDTYPE.FORMULA_OLD:
                    goto Label_023A;

                case BIFFRECORDTYPE.BOF_V2:
                    goto Label_01C2;

                case BIFFRECORDTYPE.EOF:
                    return(new XlsBiffEOF(bytes, offset));

                case BIFFRECORDTYPE.FORMAT_V23:
                    goto Label_0242;

                case BIFFRECORDTYPE.CONTINUE:
                    return(new XlsBiffContinue(bytes, offset));

                case BIFFRECORDTYPE.WINDOW1:
                    return(new XlsBiffWindow1(bytes, offset));

                case BIFFRECORDTYPE.BACKUP:
                    return(new XlsBiffSimpleValueRecord(bytes, offset));

                case BIFFRECORDTYPE.CODEPAGE:
                    return(new XlsBiffSimpleValueRecord(bytes, offset));

                case BIFFRECORDTYPE.RECORD1904:
                    return(new XlsBiffSimpleValueRecord(bytes, offset));

                case BIFFRECORDTYPE.UNCALCED:
                    return(new XlsBiffUncalced(bytes, offset));

                case BIFFRECORDTYPE.BOUNDSHEET:
                    return(new XlsBiffBoundSheet(bytes, offset));

                case BIFFRECORDTYPE.MULRK:
                    return(new XlsBiffMulRKCell(bytes, offset));

                case BIFFRECORDTYPE.MULBLANK:
                    return(new XlsBiffMulBlankCell(bytes, offset));

                case BIFFRECORDTYPE.FNGROUPCOUNT:
                    return(new XlsBiffSimpleValueRecord(bytes, offset));

                case BIFFRECORDTYPE.HIDEOBJ:
                    return(new XlsBiffSimpleValueRecord(bytes, offset));
                }
                goto Label_02B2;
            }
            if (biffrecordtype <= BIFFRECORDTYPE.INDEX)
            {
                switch (biffrecordtype)
                {
                case BIFFRECORDTYPE.RSTRING:
                case BIFFRECORDTYPE.LABEL:
                    goto Label_020A;

                case BIFFRECORDTYPE.DBCELL:
                    return(new XlsBiffDbCell(bytes, offset));

                case BIFFRECORDTYPE.BOOKBOOL:
                    return(new XlsBiffSimpleValueRecord(bytes, offset));

                case BIFFRECORDTYPE.INTERFACEHDR:
                    return(new XlsBiffInterfaceHdr(bytes, offset));

                case BIFFRECORDTYPE.SST:
                    return(new XlsBiffSST(bytes, offset));

                case BIFFRECORDTYPE.LABELSST:
                    return(new XlsBiffLabelSSTCell(bytes, offset));

                case BIFFRECORDTYPE.USESELFS:
                    return(new XlsBiffSimpleValueRecord(bytes, offset));

                case BIFFRECORDTYPE.DIMENSIONS:
                    return(new XlsBiffDimensions(bytes, offset));

                case BIFFRECORDTYPE.BLANK:
                case BIFFRECORDTYPE.BOOLERR:
                    goto Label_01FA;

                case BIFFRECORDTYPE.INTEGER:
                    goto Label_021A;

                case BIFFRECORDTYPE.NUMBER:
                    goto Label_0222;

                case BIFFRECORDTYPE.STRING:
                    return(new XlsBiffFormulaString(bytes, offset));

                case BIFFRECORDTYPE.ROW:
                    return(new XlsBiffRow(bytes, offset));

                case BIFFRECORDTYPE.BOF_V3:
                    goto Label_01C2;

                case BIFFRECORDTYPE.INDEX:
                    return(new XlsBiffIndex(bytes, offset));
                }
                goto Label_02B2;
            }
            if (biffrecordtype <= BIFFRECORDTYPE.FORMULA)
            {
                switch (biffrecordtype)
                {
                case BIFFRECORDTYPE.RK:
                    return(new XlsBiffRKCell(bytes, offset));

                case BIFFRECORDTYPE.FORMULA:
                    goto Label_023A;
                }
                goto Label_02B2;
            }
            if (biffrecordtype != BIFFRECORDTYPE.BOF_V4)
            {
                if (biffrecordtype == BIFFRECORDTYPE.FORMAT)
                {
                    goto Label_0242;
                }
                if (biffrecordtype != BIFFRECORDTYPE.BOF)
                {
                    goto Label_02B2;
                }
            }
Label_01C2:
            return(new XlsBiffBOF(bytes, offset));

Label_01FA:
            return(new XlsBiffBlankCell(bytes, offset));

Label_020A:
            return(new XlsBiffLabelCell(bytes, offset));

Label_021A:
            return(new XlsBiffIntegerCell(bytes, offset));

Label_0222:
            return(new XlsBiffNumberCell(bytes, offset));

Label_023A:
            return(new XlsBiffFormulaCell(bytes, offset));

Label_0242:
            return(new XlsBiffFormatString(bytes, offset));

Label_02B2:
            return(new XlsBiffRecord(bytes, offset));
        }
Пример #3
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);
        }