Пример #1
0
        /// <summary> Gets the name of the external sheet specified by the index
        ///
        /// </summary>
        /// <param name="index">the external sheet index
        /// </param>
        /// <returns> the name of the external sheet
        /// </returns>
        public virtual string getLastExternalSheetName(int index)
        {
            // For biff7, the whole external reference thing works differently
            // Hopefully for our purposes sheet references will all be local
            if (workbookBof.isBiff7())
            {
                BoundsheetRecord br = (BoundsheetRecord)boundsheets[index];

                return(br.Name);
            }

            int           supbookIndex = externSheet.getSupbookIndex(index);
            SupbookRecord sr           = (SupbookRecord)supbooks[supbookIndex];

            int lastTab = externSheet.getLastTabIndex(index);

            if (sr.Type == SupbookRecord.INTERNAL)
            {
                // It's an internal reference - get the name from the boundsheets list
                BoundsheetRecord br = (BoundsheetRecord)boundsheets[lastTab];

                return(br.Name);
            }
            else if (sr.Type == SupbookRecord.EXTERNAL)
            {
                // External reference - get the sheet name from the supbook record
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                sb.Append('[');
                sb.Append(sr.FileName);
                sb.Append(']');
                sb.Append(sr.getSheetName(lastTab));
                return(sb.ToString());
            }

            // An unknown supbook - return unkown
            return("[UNKNOWN]");
        }
Пример #2
0
        /// <summary> Does the hard work of building up the object graph from the excel bytes
        ///
        /// </summary>
        /// <exception cref=""> BiffException
        /// </exception>
        /// <exception cref=""> PasswordException if the workbook is password protected
        /// </exception>
        protected internal override void  parse()
        {
            Record r = null;

            BOFRecord bof = new BOFRecord(excelFile.next());

            workbookBof = bof;
            bofs++;

            if (!bof.isBiff8() && !bof.isBiff7())
            {
                throw new BiffException(BiffException.unrecognizedBiffVersion);
            }

            if (!bof.isWorkbookGlobals())
            {
                throw new BiffException(BiffException.expectedGlobals);
            }
            ArrayList continueRecords = new ArrayList();

            nameTable = new ArrayList();

            // Skip to the first worksheet
            while (bofs == 1)
            {
                r = excelFile.next();

                if (r.Type == NExcel.Biff.Type.SST)
                {
                    continueRecords.Clear();
                    Record nextrec = excelFile.peek();
                    while (nextrec.Type == NExcel.Biff.Type.CONTINUE)
                    {
                        continueRecords.Add(excelFile.next());
                        nextrec = excelFile.peek();
                    }

                    // cast the array
                    System.Object[] rec     = continueRecords.ToArray();
                    Record[]        records = new Record[rec.Length];
                    Array.Copy(rec, 0, records, 0, rec.Length);

                    sharedStrings = new SSTRecord(r, records, settings);
                }
                else if (r.Type == NExcel.Biff.Type.FILEPASS)
                {
                    throw new PasswordException();
                }
                else if (r.Type == NExcel.Biff.Type.NAME)
                {
                    NameRecord nr = null;

                    if (bof.isBiff8())
                    {
                        nr = new NameRecord(r, settings, namedRecords.Count);
                    }
                    else
                    {
                        nr = new NameRecord(r, settings, namedRecords.Count, NameRecord.biff7);
                    }

                    namedRecords[nr.Name] = nr;
                    nameTable.Add(nr);
                }
                else if (r.Type == NExcel.Biff.Type.FONT)
                {
                    FontRecord fr = null;

                    if (bof.isBiff8())
                    {
                        fr = new FontRecord(r, settings);
                    }
                    else
                    {
                        fr = new FontRecord(r, settings, FontRecord.biff7);
                    }
                    fonts.addFont(fr);
                }
                else if (r.Type == NExcel.Biff.Type.PALETTE)
                {
                    NExcel.Biff.PaletteRecord palette = new NExcel.Biff.PaletteRecord(r);
                    formattingRecords.Palette = palette;
                }
                else if (r.Type == NExcel.Biff.Type.NINETEENFOUR)
                {
                    NineteenFourRecord nr = new NineteenFourRecord(r);
                    nineteenFour = nr.is1904();
                }
                else if (r.Type == NExcel.Biff.Type.FORMAT)
                {
                    FormatRecord fr = null;
                    if (bof.isBiff8())
                    {
                        fr = new FormatRecord(r, settings, FormatRecord.biff8);
                    }
                    else
                    {
                        fr = new FormatRecord(r, settings, FormatRecord.biff7);
                    }
                    try
                    {
                        formattingRecords.addFormat(fr);
                    }
                    catch (NumFormatRecordsException e)
                    {
                        // This should not happen.  Bomb out
                        //          Assert.verify(false, e.getMessage());
                        Assert.verify(false, "This should not happen. 64");
                    }
                }
                else if (r.Type == NExcel.Biff.Type.XF)
                {
                    XFRecord xfr = null;
                    if (bof.isBiff8())
                    {
                        xfr = new XFRecord(r, XFRecord.biff8);
                    }
                    else
                    {
                        xfr = new XFRecord(r, XFRecord.biff7);
                    }

                    try
                    {
                        formattingRecords.addStyle(xfr);
                    }
                    catch (NumFormatRecordsException e)
                    {
                        // This should not happen.  Bomb out
                        //          Assert.verify(false, e.getMessage());
                        Assert.verify(false, "This should not happen. 59");
                    }
                }
                else if (r.Type == NExcel.Biff.Type.BOUNDSHEET)
                {
                    BoundsheetRecord br = null;

                    if (bof.isBiff8())
                    {
                        br = new BoundsheetRecord(r);
                    }
                    else
                    {
                        br = new BoundsheetRecord(r, BoundsheetRecord.biff7);
                    }

                    if (br.isSheet() || br.Chart)
                    {
                        boundsheets.Add(br);
                    }
                }
                else if (r.Type == NExcel.Biff.Type.EXTERNSHEET)
                {
                    if (bof.isBiff8())
                    {
                        externSheet = new ExternalSheetRecord(r, settings);
                    }
                    else
                    {
                        externSheet = new ExternalSheetRecord(r, settings, ExternalSheetRecord.biff7);
                    }
                }
                else if (r.Type == NExcel.Biff.Type.CODEPAGE)
                {
                    CodepageRecord cr = new CodepageRecord(r);
                    settings.CharacterSet = cr.CharacterSet;
                }
                else if (r.Type == NExcel.Biff.Type.SUPBOOK)
                {
                    SupbookRecord sr = new SupbookRecord(r, settings);
                    supbooks.Add(sr);
                }
                else if (r.Type == NExcel.Biff.Type.PROTECT)
                {
                    ProtectRecord pr = new ProtectRecord(r);
                    wbProtected = pr.IsProtected();
                }
                else if (r.Type == NExcel.Biff.Type.MSODRAWINGGROUP)
                {
                    msoDrawingGroup = new MsoDrawingGroupRecord(r);

                    if (drawingGroup == null)
                    {
                        drawingGroup = new DrawingGroup(DrawingGroup.READ);
                    }

                    drawingGroup.add(msoDrawingGroup);

                    Record nextrec = excelFile.peek();
                    while (nextrec.Type == NExcel.Biff.Type.CONTINUE)
                    {
                        drawingGroup.add(excelFile.next());
                        nextrec = excelFile.peek();
                    }
                }
                else if (r.Type == NExcel.Biff.Type.EOF)
                {
                    bofs--;
                }
            }

            bof = null;
            if (excelFile.hasNext())
            {
                r = excelFile.next();

                if (r.Type == NExcel.Biff.Type.BOF)
                {
                    bof = new BOFRecord(r);
                }
            }

            // Only get sheets for which there is a corresponding Boundsheet record
            while (bof != null && NumberOfSheets < boundsheets.Count)
            {
                if (!bof.isBiff8() && !bof.isBiff7())
                {
                    throw new BiffException(BiffException.unrecognizedBiffVersion);
                }

                if (bof.isWorksheet())
                {
                    // Read the sheet in
                    SheetImpl s = new SheetImpl(excelFile, sharedStrings, formattingRecords, bof, workbookBof, nineteenFour, this);

                    BoundsheetRecord br = (BoundsheetRecord)boundsheets[NumberOfSheets];
                    s.setName(br.Name);
                    s.Hidden = br.isHidden();
                    addSheet(s);
                }
                else if (bof.isChart())
                {
                    // Read the sheet in
                    SheetImpl s = new SheetImpl(excelFile, sharedStrings, formattingRecords, bof, workbookBof, nineteenFour, this);

                    BoundsheetRecord br = (BoundsheetRecord)boundsheets[NumberOfSheets];
                    s.setName(br.Name);
                    s.Hidden = br.isHidden();
                    addSheet(s);
                }
                else
                {
                    logger.warn("BOF is unrecognized");


                    while (excelFile.hasNext() && r.Type != NExcel.Biff.Type.EOF)
                    {
                        r = excelFile.next();
                    }
                }

                // The next record will normally be a BOF or empty padding until
                // the end of the block is reached.  In exceptionally unlucky cases,
                // the last EOF  will coincide with a block division, so we have to
                // check there is more data to retrieve.
                // Thanks to liamg for spotting this
                bof = null;
                if (excelFile.hasNext())
                {
                    r = excelFile.next();

                    if (r.Type == NExcel.Biff.Type.BOF)
                    {
                        bof = new BOFRecord(r);
                    }
                }
            }
        }