Esempio n. 1
0
        private bool readWorkSheetGlobals(XlsWorksheet sheet, out XlsBiffIndex idx, out XlsBiffRow row)
        {
            idx = null;
            row = null;

            m_stream.Seek((int)sheet.DataOffset, SeekOrigin.Begin);

            XlsBiffBOF bof = m_stream.Read() as XlsBiffBOF;

            if (bof == null || bof.Type != BIFFTYPE.Worksheet)
            {
                return(false);
            }

            //DumpBiffRecords();

            XlsBiffRecord rec = m_stream.Read();

            if (rec == null)
            {
                return(false);
            }
            if (rec is XlsBiffIndex)
            {
                idx = rec as XlsBiffIndex;
            }
            else if (rec is XlsBiffUncalced)
            {
                // Sometimes this come before the index...
                idx = m_stream.Read() as XlsBiffIndex;
            }

            #region Если в текущем файле MS Biff нет индекса или он не находится в самом начале то основные данные в таблицах не находятся и выводится только минимум.
            //if (null == idx)
            //{
            //	// There is a record before the index! Chech his type and see the MS Biff Documentation
            //	return false;
            //}
            #endregion

            if (idx != null)
            {
                idx.IsV8 = isV8();
            }

            XlsBiffRecord     trec;
            XlsBiffDimensions dims = null;

            do
            {
                trec = m_stream.Read();
                if (trec.ID == BIFFRECORDTYPE.DIMENSIONS)
                {
                    dims = (XlsBiffDimensions)trec;
                    break;
                }
            } while (trec != null && trec.ID != BIFFRECORDTYPE.ROW);

            //if we are already on row record then set that as the row, otherwise step forward till we get to a row record
            if (trec.ID == BIFFRECORDTYPE.ROW)
            {
                row = (XlsBiffRow)trec;
            }

            XlsBiffRow rowRecord = null;
            while (rowRecord == null)
            {
                if (m_stream.Position >= m_stream.Size)
                {
                    break;
                }
                var thisRec = m_stream.Read();
                if (thisRec is XlsBiffEOF)
                {
                    break;
                }
                rowRecord = thisRec as XlsBiffRow;
            }

            row = rowRecord;

            m_maxCol = 256;

            if (dims != null)
            {
                dims.IsV8        = isV8();
                m_maxCol         = dims.LastColumn - 1;
                sheet.Dimensions = dims;
            }

            m_maxRow = idx == null ? (int)dims.LastRow : (int)idx.LastExistingRow;

            if (idx != null && idx.LastExistingRow <= idx.FirstExistingRow)
            {
                return(false);
            }
            else if (row == null)
            {
                return(false);
            }

            m_depth = 0;

            return(true);
        }
Esempio n. 2
0
        private void readWorkBookGlobals()
        {
            //Read Header
            try
            {
                m_hdr = XlsHeader.ReadHeader(m_file);
            }
            catch (HeaderException ex)
            {
                fail(ex.Message);
                return;
            }
            catch (FormatException ex)
            {
                fail(ex.Message);
                return;
            }

            XlsRootDirectory  dir           = new XlsRootDirectory(m_hdr);
            XlsDirectoryEntry workbookEntry = dir.FindEntry(WORKBOOK) ?? dir.FindEntry(BOOK);

            if (workbookEntry == null)
            {
                fail(Errors.ErrorStreamWorkbookNotFound); return;
            }

            if (workbookEntry.EntryType != STGTY.STGTY_STREAM)
            {
                fail(Errors.ErrorWorkbookIsNotStream); return;
            }

            m_stream = new XlsBiffStream(m_hdr, workbookEntry.StreamFirstSector, workbookEntry.IsEntryMiniStream, dir);

            m_globals = new XlsWorkbookGlobals();

            m_stream.Seek(0, SeekOrigin.Begin);

            XlsBiffRecord rec = m_stream.Read();
            XlsBiffBOF    bof = rec as XlsBiffBOF;

            if (bof == null || bof.Type != BIFFTYPE.WorkbookGlobals)
            {
                fail(Errors.ErrorWorkbookGlobalsInvalidData); return;
            }

            bool sst = false;

            m_version = bof.Version;
            m_sheets  = new List <XlsWorksheet>();

            while (null != (rec = m_stream.Read()))
            {
                switch (rec.ID)
                {
                case BIFFRECORDTYPE.INTERFACEHDR:
                    m_globals.InterfaceHdr = (XlsBiffInterfaceHdr)rec;
                    break;

                case BIFFRECORDTYPE.BOUNDSHEET:
                    XlsBiffBoundSheet sheet = (XlsBiffBoundSheet)rec;

                    if (sheet.Type != XlsBiffBoundSheet.SheetType.Worksheet)
                    {
                        break;
                    }

                    sheet.IsV8        = isV8();
                    sheet.UseEncoding = m_encoding;

                    m_sheets.Add(new XlsWorksheet(m_globals.Sheets.Count, sheet));
                    m_globals.Sheets.Add(sheet);

                    break;

                case BIFFRECORDTYPE.MMS:
                    m_globals.MMS = rec;
                    break;

                case BIFFRECORDTYPE.COUNTRY:
                    m_globals.Country = rec;
                    break;

                case BIFFRECORDTYPE.CODEPAGE:

                    m_globals.CodePage = (XlsBiffSimpleValueRecord)rec;

                    try
                    {
                        m_encoding = Encoding.GetEncoding(m_globals.CodePage.Value);
                    }
                    catch (ArgumentException)
                    {
                        // Warning - Password protection
                    }

                    break;

                case BIFFRECORDTYPE.FONT:
                case BIFFRECORDTYPE.FONT_V34:
                    m_globals.Fonts.Add(rec);
                    break;

                case BIFFRECORDTYPE.FORMAT_V23:
                {
                    var fmt = (XlsBiffFormatString)rec;
                    fmt.UseEncoding = m_encoding;
                    m_globals.Formats.Add((ushort)m_globals.Formats.Count, fmt);
                }
                break;

                case BIFFRECORDTYPE.FORMAT:
                {
                    var fmt = (XlsBiffFormatString)rec;
                    m_globals.Formats.Add(fmt.Index, fmt);
                }
                break;

                case BIFFRECORDTYPE.XF:
                case BIFFRECORDTYPE.XF_V4:
                case BIFFRECORDTYPE.XF_V3:
                case BIFFRECORDTYPE.XF_V2:
                    m_globals.ExtendedFormats.Add(rec);
                    break;

                case BIFFRECORDTYPE.SST:
                    m_globals.SST = (XlsBiffSST)rec;
                    sst           = true;
                    break;

                case BIFFRECORDTYPE.CONTINUE:
                    if (!sst)
                    {
                        break;
                    }
                    XlsBiffContinue contSST = (XlsBiffContinue)rec;
                    m_globals.SST.Append(contSST);
                    break;

                case BIFFRECORDTYPE.EXTSST:
                    m_globals.ExtSST = rec;
                    sst = false;
                    break;

                case BIFFRECORDTYPE.PROTECT:
                case BIFFRECORDTYPE.PASSWORD:
                case BIFFRECORDTYPE.PROT4REVPASSWORD:
                    //IsProtected
                    break;

                case BIFFRECORDTYPE.EOF:
                    if (m_globals.SST != null)
                    {
                        m_globals.SST.ReadStrings();
                    }
                    return;

                default:
                    continue;
                }
            }
        }