Exemple #1
0
        public void TestAbnormalPivotTableRecords_bug46280()
        {
            int SXVIEW_SID = ViewDefinitionRecord.sid;

            Record[] inRecs =
            {
                new RowRecord(0),
                new NumberRecord(),
                // normally MSODRAWING(0x00EC) would come here before SXVIEW
                new UnknownRecord(SXVIEW_SID,System.Text.Encoding.UTF8.GetBytes("dummydata (SXVIEW: View DefInition)")),
                new WindowTwoRecord(),
            };
            RecordStream    rs  = new RecordStream(Arrays.AsList(inRecs), 0);
            RowBlocksReader rbr = new RowBlocksReader(rs);

            if (rs.PeekNextClass() == typeof(WindowTwoRecord))
            {
                // Should have stopped at the SXVIEW record
                throw new AssertFailedException("Identified bug 46280b");
            }
            RecordStream rbStream = rbr.PlainRecordStream;

            Assert.AreEqual(inRecs[0], rbStream.GetNext());
            Assert.AreEqual(inRecs[1], rbStream.GetNext());
            Assert.IsFalse(rbStream.HasNext());
            Assert.IsTrue(rs.HasNext());
            Assert.AreEqual(inRecs[2], rs.GetNext());
            Assert.AreEqual(inRecs[3], rs.GetNext());
        }
        public void TestAbnormalPivotTableRecords_bug46280()
        {
            int SXVIEW_SID = ViewDefinitionRecord.sid;
            Record[] inRecs = {
			new RowRecord(0),
			new NumberRecord(),
			// normally MSODRAWING(0x00EC) would come here before SXVIEW
			new UnknownRecord(SXVIEW_SID, System.Text.Encoding.UTF8.GetBytes("dummydata (SXVIEW: View DefInition)")),
			new WindowTwoRecord(),
		};
            RecordStream rs = new RecordStream(Arrays.AsList(inRecs), 0);
            RowBlocksReader rbr = new RowBlocksReader(rs);
            if (rs.PeekNextClass() == typeof(WindowTwoRecord))
            {
                // Should have stopped at the SXVIEW record
                throw new AssertionException("Identified bug 46280b");
            }
            RecordStream rbStream = rbr.PlainRecordStream;
            Assert.AreEqual(inRecs[0], rbStream.GetNext());
            Assert.AreEqual(inRecs[1], rbStream.GetNext());
            Assert.IsFalse(rbStream.HasNext());
            Assert.IsTrue(rs.HasNext());
            Assert.AreEqual(inRecs[2], rs.GetNext());
            Assert.AreEqual(inRecs[3], rs.GetNext());
        }
        /**
         * Also collects any loose MergeCellRecords and puts them in the supplied
         * mergedCellsTable
         */
        public RowBlocksReader(RecordStream rs)
        {
            ArrayList plainRecords = new ArrayList();
            ArrayList shFrmRecords = new ArrayList();
            ArrayList arrayRecords = new ArrayList();
            ArrayList tableRecords = new ArrayList();
            ArrayList mergeCellRecords = new ArrayList();

            while (!RecordOrderer.IsEndOfRowBlock(rs.PeekNextSid()))
            {
                // End of row/cell records for the current sheet
                // Note - It is important that this code does not inadvertently add any sheet 
                // records from a subsequent sheet.  For example, if SharedFormulaRecords 
                // are taken from the wrong sheet, this could cause bug 44449.
                if (!rs.HasNext())
                {
                    throw new InvalidOperationException("Failed to find end of row/cell records");

                }
                Record rec = rs.GetNext();
                IList dest;
                switch (rec.Sid)
                {
                    case MergeCellsRecord.sid: 
                        dest = mergeCellRecords; 
                        break;
                    case SharedFormulaRecord.sid: 
                        dest = shFrmRecords; 
                        break;
                    case ArrayRecord.sid: 
                        dest = arrayRecords; 
                        break;
                    case TableRecord.sid: 
                        dest = tableRecords; 
                        break;
                    default: dest = plainRecords;
                        break;
                }
                dest.Add(rec);
            }
            SharedFormulaRecord[] sharedFormulaRecs = new SharedFormulaRecord[shFrmRecords.Count];
            ArrayRecord[] arrayRecs = new ArrayRecord[arrayRecords.Count];
            TableRecord[] tableRecs = new TableRecord[tableRecords.Count];
            sharedFormulaRecs = (SharedFormulaRecord[])shFrmRecords.ToArray(typeof(SharedFormulaRecord));
            arrayRecs = (ArrayRecord[])arrayRecords.ToArray(typeof(ArrayRecord));
            tableRecs = (TableRecord[])tableRecords.ToArray(typeof(TableRecord));

            _plainRecords = plainRecords;
            _sfm = SharedValueManager.Create(sharedFormulaRecs, arrayRecs, tableRecs);
            _mergedCellsRecords = new MergeCellsRecord[mergeCellRecords.Count];
            _mergedCellsRecords = (MergeCellsRecord[])mergeCellRecords.ToArray(typeof(MergeCellsRecord));
        }
        /**
         * @param rs record stream with all {@link SharedFormulaRecord}
         * {@link ArrayRecord}, {@link TableRecord} {@link MergeCellsRecord} Records removed
         */
        public RowRecordsAggregate(RecordStream rs, SharedValueManager svm)
            : this(svm)
        {
            while (rs.HasNext())
            {
                Record rec = rs.GetNext();
                switch (rec.Sid)
                {
                    case RowRecord.sid:
                        InsertRow((RowRecord)rec);
                        continue;
                    case DBCellRecord.sid:
                        // end of 'Row Block'.  Should only occur after cell records
                        // ignore DBCELL records because POI generates them upon re-serialization
                        continue;
                }
                if (rec is UnknownRecord)
                {
                    // might need to keep track of where exactly these belong
                    AddUnknownRecord((UnknownRecord)rec);

                    while (rs.PeekNextSid() == ContinueRecord.sid)
                    {
                        AddUnknownRecord(rs.GetNext());
                    }
                    continue;
                }
               			if (rec is MulBlankRecord) {
                    _valuesAgg.AddMultipleBlanks((MulBlankRecord) rec);
                    continue;
                }

                if (!(rec is CellValueRecordInterface))
                {
                    throw new InvalidOperationException("Unexpected record type (" + rec.GetType().Name + ")");
                }
                _valuesAgg.Construct((CellValueRecordInterface)rec, rs, svm);
            }
        }
Exemple #5
0
        public InternalChart(RecordStream rs)
        {
            _plsRecords = new List<PLSAggregate>();
            records = new List<RecordBase>(128);

            if (rs.PeekNextSid() != BOFRecord.sid)
            {
                throw new Exception("BOF record expected");
            }
            BOFRecord bof = (BOFRecord)rs.GetNext();
            if (bof.Type != BOFRecord.TYPE_CHART)
            {
                throw new RuntimeException("Bad BOF record type");
            }

            records.Add(bof);
            while (rs.HasNext())
            {
                int recSid = rs.PeekNextSid();

                Record.Record rec = rs.GetNext();
                if (recSid == EOFRecord.sid)
                {
                    records.Add(rec);
                    break;
                }

                if (recSid == ChartRecord.sid)
                {

                    continue;
                }

                if (recSid == ChartFRTInfoRecord.sid)
                {
                    _chartFrtInfo = (ChartFRTInfoRecord)rec;
                }
                else if (recSid == HeaderRecord.sid)
                {
                    header = (HeaderRecord)rec;
                }
                else if (recSid == FooterRecord.sid)
                {
                    footer = (FooterRecord)rec;
                }
                else if (recSid == HCenterRecord.sid)
                {
                    _hCenter = (HCenterRecord)rec;
                }
                else if (recSid == VCenterRecord.sid)
                {
                    _vCenter = (VCenterRecord)rec;
                }
                else if (recSid == LeftMarginRecord.sid)
                {
                    _leftMargin = (LeftMarginRecord)rec;
                }
                else if (recSid == RightMarginRecord.sid)
                {
                    _rightMargin = (RightMarginRecord)rec;
                }
                else if (recSid == TopMarginRecord.sid)
                {
                    _topMargin = (TopMarginRecord)rec;
                }
                else if (recSid == BottomMarginRecord.sid)
                {
                    _bottomMargin = (BottomMarginRecord)rec;
                }
                else if (recSid == UnknownRecord.PLS_004D) // PLS
                {
                    PLSAggregate pls = new PLSAggregate(rs);
                    PLSAggregateVisitor rv = new PLSAggregateVisitor(records);
                    pls.VisitContainedRecords(rv);
                    _plsRecords.Add(pls);

                    continue;
                }
                else if (recSid == PrintSetupRecord.sid)
                {
                    printSetup = (PrintSetupRecord)rec;
                }
                else if (recSid == PrintSizeRecord.sid)
                {
                    _printSize = (PrintSizeRecord)rec;
                }
                else if (recSid == HeaderFooterRecord.sid)
                {
                    HeaderFooterRecord hf = (HeaderFooterRecord)rec;
                    if (hf.IsCurrentSheet)
                        _headerFooter = hf;
                    else
                        _sviewHeaderFooters.Add(hf);
                }
                else if (recSid == ProtectRecord.sid)
                {
                    _protect = (ProtectRecord)rec;
                }
                records.Add(rec);
            }
            
        }
        /**
         * Also collects any loose MergeCellRecords and puts them in the supplied
         * mergedCellsTable
         */
        public RowBlocksReader(RecordStream rs)
        {
            ArrayList            plainRecords     = new ArrayList();
            ArrayList            shFrmRecords     = new ArrayList();
            ArrayList            arrayRecords     = new ArrayList();
            ArrayList            tableRecords     = new ArrayList();
            ArrayList            mergeCellRecords = new ArrayList();
            List <CellReference> firstCellRefs    = new List <CellReference>();
            Record prevRec = null;

            while (!RecordOrderer.IsEndOfRowBlock(rs.PeekNextSid()))
            {
                // End of row/cell records for the current sheet
                // Note - It is important that this code does not inadvertently add any sheet
                // records from a subsequent sheet.  For example, if SharedFormulaRecords
                // are taken from the wrong sheet, this could cause bug 44449.
                if (!rs.HasNext())
                {
                    throw new InvalidOperationException("Failed to find end of row/cell records");
                }
                Record    rec = rs.GetNext();
                ArrayList dest;
                switch (rec.Sid)
                {
                case MergeCellsRecord.sid:
                    dest = mergeCellRecords;
                    break;

                case SharedFormulaRecord.sid:
                    dest = shFrmRecords;
                    if (!(prevRec is FormulaRecord))
                    {
                        throw new Exception("Shared formula record should follow a FormulaRecord");
                    }
                    FormulaRecord fr = (FormulaRecord)prevRec;
                    firstCellRefs.Add(new CellReference(fr.Row, fr.Column));

                    break;

                case ArrayRecord.sid:
                    dest = arrayRecords;
                    break;

                case TableRecord.sid:
                    dest = tableRecords;
                    break;

                default: dest = plainRecords;
                    break;
                }
                dest.Add(rec);
                prevRec = rec;
            }
            SharedFormulaRecord[] sharedFormulaRecs = new SharedFormulaRecord[shFrmRecords.Count];
            List <ArrayRecord>    arrayRecs         = new List <ArrayRecord>(arrayRecords.Count);
            List <TableRecord>    tableRecs         = new List <TableRecord>(tableRecords.Count);

            sharedFormulaRecs = (SharedFormulaRecord[])shFrmRecords.ToArray(typeof(SharedFormulaRecord));

            CellReference[] firstCells = new CellReference[firstCellRefs.Count];
            firstCells = firstCellRefs.ToArray();
            arrayRecs  = new List <ArrayRecord>((ArrayRecord[])arrayRecords.ToArray(typeof(ArrayRecord)));
            tableRecs  = new List <TableRecord>((TableRecord[])tableRecords.ToArray(typeof(TableRecord)));

            _plainRecords       = plainRecords;
            _sfm                = SharedValueManager.Create(sharedFormulaRecs, firstCells, arrayRecs, tableRecs);
            _mergedCellsRecords = new MergeCellsRecord[mergeCellRecords.Count];
            _mergedCellsRecords = (MergeCellsRecord[])mergeCellRecords.ToArray(typeof(MergeCellsRecord));
        }
        /**
         * @param rs record stream with all {@link SharedFormulaRecord}
         * {@link ArrayRecord}, {@link TableRecord} {@link MergeCellsRecord} Records removed
         */
        public RowRecordsAggregate(RecordStream rs, SharedValueManager svm)
            : this(svm)
        {
            while (rs.HasNext())
            {
                Record rec = rs.GetNext();
                switch (rec.Sid)
                {
                    case RowRecord.sid:
                        InsertRow((RowRecord)rec);
                        continue;
                    case DConRefRecord.sid:
                        AddUnknownRecord(rec);
                        continue;
                    case DBCellRecord.sid:
                        // end of 'Row Block'.  Should only occur after cell records
                        // ignore DBCELL records because POI generates them upon re-serialization
                        continue;
                }
                if (rec is UnknownRecord)
                {
                    // might need to keep track of where exactly these belong
                    AddUnknownRecord((UnknownRecord)rec);

                    while (rs.PeekNextSid() == ContinueRecord.sid)
                    {
                        AddUnknownRecord(rs.GetNext());
                    }
                    continue;
                }
       			if (rec is MulBlankRecord) {
    			    _valuesAgg.AddMultipleBlanks((MulBlankRecord) rec);
				    continue;
			    }

                if (!(rec is CellValueRecordInterface))
                {
                    //TODO: correct it, SeriesIndexRecord will appear in a separate chart sheet that contains a single chart
                    // rule SERIESDATA = Dimensions 3(SIIndex *(Number / BoolErr / Blank / Label))
                    if (rec.Sid == SeriesIndexRecord.sid)
                    {
                        AddUnknownRecord(rec);
                        continue;
                    }
                    throw new InvalidOperationException("Unexpected record type (" + rec.GetType().Name + ")");

                }
                _valuesAgg.Construct((CellValueRecordInterface)rec, rs, svm);
            }
        }
Exemple #8
0
        /// <summary>
        /// Initializes a new instance of the <see cref="InternalSheet"/> class.
        /// </summary>
        /// <param name="rs">The stream.</param>
        private InternalSheet(RecordStream rs)
        {
            _mergedCellsTable = new MergedCellsTable();
            RowRecordsAggregate rra = null;

            records = new List<RecordBase>(128);
            // TODO - take chart streams off into separate java objects
            //int bofEofNestingLevel = 0;  // nesting level can only get to 2 (when charts are present)
            int dimsloc = -1;

            if (rs.PeekNextSid() != BOFRecord.sid)
            {
                throw new Exception("BOF record expected");
            }
            BOFRecord bof = (BOFRecord)rs.GetNext();
            if (bof.Type != BOFRecord.TYPE_WORKSHEET)
            {
                // TODO - fix junit tests throw new RuntimeException("Bad BOF record type");
            }
            records.Add(bof);
            while (rs.HasNext())
            {
                int recSid = rs.PeekNextSid();

                if (recSid == CFHeaderRecord.sid)
                {
                    condFormatting = new ConditionalFormattingTable(rs);
                    records.Add(condFormatting);
                    continue;
                }

                if (recSid == ColumnInfoRecord.sid)
                {
                    _columnInfos = new ColumnInfoRecordsAggregate(rs);
                    records.Add(_columnInfos);
                    continue;
                }
                if (recSid == DVALRecord.sid)
                {
                    _dataValidityTable = new DataValidityTable(rs);
                    records.Add(_dataValidityTable);
                    continue;
                }

                if (RecordOrderer.IsRowBlockRecord(recSid))
                {
                    //only Add the aggregate once
                    if (rra != null)
                    {
                        throw new InvalidOperationException("row/cell records found in the wrong place");
                    }
                    RowBlocksReader rbr = new RowBlocksReader(rs);
                    _mergedCellsTable.AddRecords(rbr.LooseMergedCells);
                    rra = new RowRecordsAggregate(rbr.PlainRecordStream, rbr.SharedFormulaManager);
                    records.Add(rra); //only Add the aggregate once
                    continue;
                }

                if (CustomViewSettingsRecordAggregate.IsBeginRecord(recSid))
                {
                    // This happens three times in test sample file "29982.xls"
                    // Also several times in bugzilla samples 46840-23373 and 46840-23374
                    records.Add(new CustomViewSettingsRecordAggregate(rs));
                    continue;                    
                }

                if (PageSettingsBlock.IsComponentRecord(recSid))
                {
                    if (_psBlock == null)
                    {
                        // first PSB record encountered - read all of them:
                        _psBlock = new PageSettingsBlock(rs);
                        records.Add(_psBlock);
                    }
                    else
                    {
                        // one or more PSB records found after some intervening non-PSB records
                        _psBlock.AddLateRecords(rs);
                    }
                    // YK: in some cases records can be moved to the preceding
                    // CustomViewSettingsRecordAggregate blocks
                    _psBlock.PositionRecords(records);
                    continue;
                }
                if (WorksheetProtectionBlock.IsComponentRecord(recSid))
                {
                    _protectionBlock.AddRecords(rs);
                    continue;
                }
                if (recSid == MergeCellsRecord.sid)
                {
                    // when the MergedCellsTable is found in the right place, we expect those records to be contiguous
                    _mergedCellsTable.Read(rs);
                    continue;
                }
                
                if (recSid == BOFRecord.sid)
                {
                    ChartSubstreamRecordAggregate chartAgg = new ChartSubstreamRecordAggregate(rs);
                    //ChartSheetAggregate chartAgg = new ChartSheetAggregate(rs, null);
                    //if (false)
                    //{
                    // TODO - would like to keep the chart aggregate packed, but one unit test needs attention
                    //    records.Add(chartAgg);
                    //}
                    //else
                    //{
                    SpillAggregate(chartAgg, records);
                    //}
                    continue;
                }
                Record rec = rs.GetNext();
                if (recSid == IndexRecord.sid)
                {
                    // ignore INDEX record because it is only needed by Excel,
                    // and POI always re-calculates its contents
                    continue;
                }

                
                if (recSid == UncalcedRecord.sid)
                {
                    // don't Add UncalcedRecord to the list
                    _isUncalced = true; // this flag is enough
                    continue;
                }

                if (recSid == FeatRecord.sid ||
                    recSid == FeatHdrRecord.sid)
                {
                    records.Add(rec);
                    continue;
                }
                if (recSid == EOFRecord.sid)
                {
                    records.Add(rec);
                    break;
                }
                if (recSid == DimensionsRecord.sid)
                {
                    // Make a columns aggregate if one hasn't Ready been created.
                    if (_columnInfos == null)
                    {
                        _columnInfos = new ColumnInfoRecordsAggregate();
                        records.Add(_columnInfos);
                    }

                    _dimensions = (DimensionsRecord)rec;
                    dimsloc = records.Count;
                }
                else if (recSid == DefaultColWidthRecord.sid)
                {
                    defaultcolwidth = (DefaultColWidthRecord)rec;
                }
                else if (recSid == DefaultRowHeightRecord.sid)
                {
                    defaultrowheight = (DefaultRowHeightRecord)rec;
                }
                else if (recSid == PrintGridlinesRecord.sid)
                {
                    printGridlines = (PrintGridlinesRecord)rec;
                }
                else if (recSid == GridsetRecord.sid)
                {
                    gridset = (GridsetRecord)rec;
                }
                else if (recSid == SelectionRecord.sid)
                {
                    selection = (SelectionRecord)rec;
                }
                else if (recSid == WindowTwoRecord.sid)
                {
                    windowTwo = (WindowTwoRecord)rec;
                }
                else if (recSid == SheetExtRecord.sid)
                {
                    sheetext = (SheetExtRecord)rec;
                }
                else if (recSid == GutsRecord.sid)
                {
                    _gutsRecord = (GutsRecord)rec;
                }

                records.Add(rec);
            }
            if (windowTwo == null)
            {
                throw new InvalidOperationException("WINDOW2 was not found");
            }
            if (_dimensions == null)
            {
                // Excel seems to always write the DIMENSION record, but tolerates when it is not present
                // in all cases Excel (2007) adds the missing DIMENSION record
                if (rra == null)
                {
                    // bug 46206 alludes to files which skip the DIMENSION record
                    // when there are no row/cell records.
                    // Not clear which application wrote these files.
                    rra = new RowRecordsAggregate();
                }
                else
                {
                    //log.log(POILogger.WARN, "DIMENSION record not found even though row/cells present");
                    // Not sure if any tools write files like this, but Excel reads them OK
                }
                dimsloc = FindFirstRecordLocBySid(WindowTwoRecord.sid);
                _dimensions = rra.CreateDimensions();
                records.Insert(dimsloc, _dimensions);                
            }
            if (rra == null)
            {
                rra = new RowRecordsAggregate();
                records.Insert(dimsloc + 1, rra);
            }
            _rowsAggregate = rra;
            // put merged cells table in the right place (regardless of where the first MergedCellsRecord was found */
            RecordOrderer.AddNewSheetRecord(records, _mergedCellsTable);
            RecordOrderer.AddNewSheetRecord(records, _protectionBlock);
            //if (log.Check(POILogger.DEBUG))
            //    log.Log(POILogger.DEBUG, "sheet createSheet (existing file) exited");

        }
Exemple #9
0
        public InternalChart(RecordStream rs)
        {
            _plsRecords = new List <PLSAggregate>();
            records     = new List <RecordBase>(128);

            if (rs.PeekNextSid() != BOFRecord.sid)
            {
                throw new Exception("BOF record expected");
            }
            BOFRecord bof = (BOFRecord)rs.GetNext();

            if (bof.Type != BOFRecordType.Chart)
            {
                throw new RuntimeException("Bad BOF record type");
            }

            records.Add(bof);
            while (rs.HasNext())
            {
                int recSid = rs.PeekNextSid();

                Record.Record rec = rs.GetNext();
                if (recSid == EOFRecord.sid)
                {
                    records.Add(rec);
                    break;
                }

                if (recSid == ChartRecord.sid)
                {
                    continue;
                }

                if (recSid == ChartFRTInfoRecord.sid)
                {
                    _chartFrtInfo = (ChartFRTInfoRecord)rec;
                }
                else if (recSid == HeaderRecord.sid)
                {
                    header = (HeaderRecord)rec;
                }
                else if (recSid == FooterRecord.sid)
                {
                    footer = (FooterRecord)rec;
                }
                else if (recSid == HCenterRecord.sid)
                {
                    _hCenter = (HCenterRecord)rec;
                }
                else if (recSid == VCenterRecord.sid)
                {
                    _vCenter = (VCenterRecord)rec;
                }
                else if (recSid == LeftMarginRecord.sid)
                {
                    _leftMargin = (LeftMarginRecord)rec;
                }
                else if (recSid == RightMarginRecord.sid)
                {
                    _rightMargin = (RightMarginRecord)rec;
                }
                else if (recSid == TopMarginRecord.sid)
                {
                    _topMargin = (TopMarginRecord)rec;
                }
                else if (recSid == BottomMarginRecord.sid)
                {
                    _bottomMargin = (BottomMarginRecord)rec;
                }
                else if (recSid == UnknownRecord.PLS_004D) // PLS
                {
                    PLSAggregate        pls = new PLSAggregate(rs);
                    PLSAggregateVisitor rv  = new PLSAggregateVisitor(records);
                    pls.VisitContainedRecords(rv);
                    _plsRecords.Add(pls);

                    continue;
                }
                else if (recSid == PrintSetupRecord.sid)
                {
                    printSetup = (PrintSetupRecord)rec;
                }
                else if (recSid == PrintSizeRecord.sid)
                {
                    _printSize = (PrintSizeRecord)rec;
                }
                else if (recSid == HeaderFooterRecord.sid)
                {
                    HeaderFooterRecord hf = (HeaderFooterRecord)rec;
                    if (hf.IsCurrentSheet)
                    {
                        _headerFooter = hf;
                    }
                    else
                    {
                        _sviewHeaderFooters.Add(hf);
                    }
                }
                else if (recSid == ProtectRecord.sid)
                {
                    _protect = (ProtectRecord)rec;
                }
                records.Add(rec);
            }
        }
Exemple #10
0
            /**
     * given a POI POIFSFileSystem object, and a specific directory
     *  within it, read in its Workbook and populate the high and
     *  low level models.  If you're reading in a workbook...start here.
     *
     * @param directory the POI filesystem directory to process from
     * @param preserveNodes whether to preseve other nodes, such as
     *        macros.  This takes more memory, so only say yes if you
     *        need to. If set, will store all of the POIFSFileSystem
     *        in memory
     * @see org.apache.poi.poifs.filesystem.POIFSFileSystem
     * @exception IOException if the stream cannot be read
     */
        public HSSFWorkbook(DirectoryNode directory, bool preserveNodes):base(directory)
        {

            String workbookName = GetWorkbookDirEntryName(directory);

            this.preserveNodes = preserveNodes;

            // If we're not preserving nodes, don't track the
            //  POIFS any more
            if (!preserveNodes)
            {
                this.directory = null;
            }

            _sheets = new List<HSSFSheet>(INITIAL_CAPACITY);
            names = new List<HSSFName>(INITIAL_CAPACITY);

            // Grab the data from the workbook stream, however
            //  it happens to be spelled.
            Stream stream = directory.CreatePOIFSDocumentReader(workbookName);


            List<Record> records = RecordFactory.CreateRecords(stream);

            workbook = InternalWorkbook.CreateWorkbook(records);
            SetPropertiesFromWorkbook(workbook);
            int recOffset = workbook.NumRecords;

            // Convert all LabelRecord records to LabelSSTRecord
            ConvertLabelRecords(records, recOffset);
            RecordStream rs = new RecordStream(records, recOffset);
            while (rs.HasNext())
            {
                InternalSheet sheet = InternalSheet.CreateSheet(rs);
                _sheets.Add(new HSSFSheet(this, sheet));
            }

            for (int i = 0; i < workbook.NumNames; ++i)
            {
                NameRecord nameRecord = workbook.GetNameRecord(i);
                HSSFName name = new HSSFName(this, workbook.GetNameRecord(i), workbook.GetNameCommentRecord(nameRecord));
                names.Add(name);
            }
        }
        /**
         * Also collects any loose MergeCellRecords and puts them in the supplied
         * mergedCellsTable
         */
        public RowBlocksReader(RecordStream rs)
        {
            ArrayList plainRecords = new ArrayList();
            ArrayList shFrmRecords = new ArrayList();
            ArrayList arrayRecords = new ArrayList();
            ArrayList tableRecords = new ArrayList();
            ArrayList mergeCellRecords = new ArrayList();
            List<CellReference> firstCellRefs = new List<CellReference>();
            Record prevRec = null;

            while (!RecordOrderer.IsEndOfRowBlock(rs.PeekNextSid()))
            {
                // End of row/cell records for the current sheet
                // Note - It is important that this code does not inadvertently add any sheet 
                // records from a subsequent sheet.  For example, if SharedFormulaRecords 
                // are taken from the wrong sheet, this could cause bug 44449.
                if (!rs.HasNext())
                {
                    throw new InvalidOperationException("Failed to find end of row/cell records");

                }
                Record rec = rs.GetNext();
                ArrayList dest;
                switch (rec.Sid)
                {
                    case MergeCellsRecord.sid:
                        dest = mergeCellRecords;
                        break;
                    case SharedFormulaRecord.sid:
                        dest = shFrmRecords;
                        if (!(prevRec is FormulaRecord))
                        {
                            throw new Exception("Shared formula record should follow a FormulaRecord");
                        }
                        FormulaRecord fr = (FormulaRecord)prevRec;
                        firstCellRefs.Add(new CellReference(fr.Row, fr.Column));

                        break;
                    case ArrayRecord.sid:
                        dest = arrayRecords;
                        break;
                    case TableRecord.sid:
                        dest = tableRecords;
                        break;
                    default: dest = plainRecords;
                        break;
                }
                dest.Add(rec);
                prevRec = rec;
            }
            SharedFormulaRecord[] sharedFormulaRecs = new SharedFormulaRecord[shFrmRecords.Count];
            List<ArrayRecord> arrayRecs = new List<ArrayRecord>(arrayRecords.Count);
            List<TableRecord> tableRecs = new List<TableRecord>(tableRecords.Count);
            sharedFormulaRecs = (SharedFormulaRecord[])shFrmRecords.ToArray(typeof(SharedFormulaRecord));

            CellReference[] firstCells = new CellReference[firstCellRefs.Count];
            firstCells=firstCellRefs.ToArray();
            arrayRecs = new List<ArrayRecord>((ArrayRecord[])arrayRecords.ToArray(typeof(ArrayRecord)));
            tableRecs = new List<TableRecord>((TableRecord[])tableRecords.ToArray(typeof(TableRecord)));

            _plainRecords = plainRecords;
            _sfm = SharedValueManager.Create(sharedFormulaRecs,firstCells, arrayRecs, tableRecs);
            _mergedCellsRecords = new MergeCellsRecord[mergeCellRecords.Count];
            _mergedCellsRecords = (MergeCellsRecord[])mergeCellRecords.ToArray(typeof(MergeCellsRecord));
        }
Exemple #12
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Sheet"/> class.
        /// </summary>
        /// <param name="rs">The stream.</param>
        private Sheet(RecordStream rs)
        {
            _mergedCellsTable = new MergedCellsTable();
            RowRecordsAggregate rra = null;

            records = new List<RecordBase>(128);
            // TODO - take chart streams off into separate java objects
            int bofEofNestingLevel = 0;  // nesting level can only get to 2 (when charts are present)
            int dimsloc = -1;

            while (rs.HasNext())
            {
                int recSid = rs.PeekNextSid();

                if (recSid == CFHeaderRecord.sid)
                {
                    condFormatting = new ConditionalFormattingTable(rs);
                    records.Add(condFormatting);
                    continue;
                }

                if (recSid == ColumnInfoRecord.sid)
                {
                    _columnInfos = new ColumnInfoRecordsAggregate(rs);
                    records.Add(_columnInfos);
                    continue;
                }
                if (recSid == DVALRecord.sid)
                {
                    _dataValidityTable = new DataValidityTable(rs);
                    records.Add(_dataValidityTable);
                    continue;
                }

                if (RecordOrderer.IsRowBlockRecord(recSid) && bofEofNestingLevel == 1)
                {
                    //only Add the aggregate once
                    if (rra != null)
                    {
                        throw new InvalidOperationException("row/cell records found in the wrong place");
                    }
                    RowBlocksReader rbr = new RowBlocksReader(rs);
                    _mergedCellsTable.AddRecords(rbr.LooseMergedCells);
                    rra = new RowRecordsAggregate(rbr.PlainRecordStream, rbr.SharedFormulaManager);
                    records.Add(rra); //only Add the aggregate once
                    continue;
                }

                if (PageSettingsBlock.IsComponentRecord(recSid))
                {
                    PageSettingsBlock psb = new PageSettingsBlock(rs);
                    if (bofEofNestingLevel == 1)
                    {
                        if (_psBlock == null)
                        {
                            _psBlock = psb;
                        }
                        else
                        {
                            // more than one 'Page Settings Block' at nesting level 1 ?
                            // apparently this happens in about 15 test sample files
                        }
                    }
                    records.Add(psb);
                    continue;
                }

                if (recSid == MergeCellsRecord.sid)
                {
                    // when the MergedCellsTable is found in the right place, we expect those records to be contiguous
                    _mergedCellsTable.Read(rs);
                    continue;
                }

                Record rec = rs.GetNext();
                if (recSid == IndexRecord.sid)
                {
                    // ignore INDEX record because it is only needed by Excel,
                    // and POI always re-calculates its contents
                    continue;
                }


                if (recSid == UncalcedRecord.sid)
                {
                    // don't Add UncalcedRecord to the list
                    _isUncalced = true; // this flag is enough
                    continue;
                }

                if (recSid == BOFRecord.sid)
                {
                    bofEofNestingLevel++;
                    //if (log.Check(POILogger.DEBUG))
                    //    log.Log(POILogger.DEBUG, "Hit BOF record. Nesting increased to " + bofEofNestingLevel);
                }
                else if (recSid == EOFRecord.sid)
                {
                    --bofEofNestingLevel;
                    //if (log.Check(POILogger.DEBUG))
                    //    log.Log(POILogger.DEBUG, "Hit EOF record. Nesting decreased to " + bofEofNestingLevel);
                    if (bofEofNestingLevel == 0)
                    {
                        records.Add(rec);
                        break;
                    }
                }
                else if (recSid == DimensionsRecord.sid)
                {
                    // Make a columns aggregate if one hasn't Ready been created.
                    if (_columnInfos == null)
                    {
                        _columnInfos = new ColumnInfoRecordsAggregate();
                        records.Add(_columnInfos);
                    }

                    _dimensions = (DimensionsRecord)rec;
                    dimsloc = records.Count;
                }
                else if (recSid == DefaultColWidthRecord.sid)
                {
                    defaultcolwidth = (DefaultColWidthRecord)rec;
                }
                else if (recSid == DefaultRowHeightRecord.sid)
                {
                    defaultrowheight = (DefaultRowHeightRecord)rec;
                }
                else if (recSid == PrintGridlinesRecord.sid)
                {
                    printGridlines = (PrintGridlinesRecord)rec;
                }
                else if (recSid == GridsetRecord.sid)
                {
                    gridset = (GridsetRecord)rec;
                }
                else if (recSid == SelectionRecord.sid)
                {
                    selection = (SelectionRecord)rec;
                }
                else if (recSid == WindowTwoRecord.sid)
                {
                    windowTwo = (WindowTwoRecord)rec;
                }
                else if (recSid == ProtectRecord.sid)
                {
                    protect = (ProtectRecord)rec;
                }
                else if (recSid == ObjectProtectRecord.sid)
                {
                    objprotect = (ObjectProtectRecord)rec;
                }
                else if (recSid == ScenarioProtectRecord.sid)
                {
                    scenprotect = (ScenarioProtectRecord)rec;
                }
                else if (recSid == PasswordRecord.sid)
                {
                    password = (PasswordRecord)rec;
                }
                else if (recSid == SheetExtRecord.sid)
                {
                    sheetext = (SheetExtRecord)rec;
                }

                records.Add(rec);
            }
            if (windowTwo == null)
            {
                throw new InvalidOperationException("WINDOW2 was not found");
            }
            if (_dimensions == null)
            {
                // Excel seems to always write the DIMENSION record, but tolerates when it is not present
                // in all cases Excel (2007) adds the missing DIMENSION record
                if (rra == null)
                {
                    // bug 46206 alludes to files which skip the DIMENSION record
                    // when there are no row/cell records.
                    // Not clear which application wrote these files.
                    rra = new RowRecordsAggregate();
                }
                else
                {
                    //log.log(POILogger.WARN, "DIMENSION record not found even though row/cells present");
                    // Not sure if any tools write files like this, but Excel reads them OK
                }
                dimsloc = FindFirstRecordLocBySid(WindowTwoRecord.sid);
                _dimensions = rra.CreateDimensions();
                records.Insert(dimsloc, _dimensions);                
            }
            if (rra == null)
            {
                rra = new RowRecordsAggregate();
                records.Insert(dimsloc + 1, rra);
            }
            _rowsAggregate = rra;
            // put merged cells table in the right place (regardless of where the first MergedCellsRecord was found */
            RecordOrderer.AddNewSheetRecord(records, _mergedCellsTable);
            //if (log.Check(POILogger.DEBUG))
            //    log.Log(POILogger.DEBUG, "sheet createSheet (existing file) exited");

        }