Esempio n. 1
0
        //private static POILogger log = POILogFactory.GetLogger(typeof(HSSFSheet));

        /// <summary>
        /// Creates new HSSFSheet - called by HSSFWorkbook to create a _sheet from
        /// scratch. You should not be calling this from application code (its protected anyhow).
        /// </summary>
        /// <param name="workbook">The HSSF Workbook object associated with the _sheet.</param>
        /// <see cref="NPOI.HSSF.UserModel.HSSFWorkbook.CreateSheet()"/>
        public HSSFSheet(HSSFWorkbook workbook)
        {
            _sheet = InternalSheet.CreateSheet();
            rows = new Dictionary<int, NPOI.SS.UserModel.IRow>();
            this._workbook = workbook;
            this.book = workbook.Workbook;
        }
Esempio n. 2
0
        /**
         * @return the value calculated for the position of the first DBCELL record for this sheet.
         * That value is1 found on the IndexRecord.
         */
        private static int GetDbCellRecordPos(InternalSheet sheet)
        {

            MyIndexRecordListener myIndexListener = new MyIndexRecordListener();
            sheet.VisitContainedRecords(myIndexListener, 0);
            IndexRecord indexRecord = myIndexListener.GetIndexRecord();
            int dbCellRecordPos = indexRecord.GetDbcellAt(0);
            return dbCellRecordPos;
        }
Esempio n. 3
0
 private static Record[] GetSheetRecords(InternalSheet s, int offset)
 {
     RecordInspector.RecordCollector rc = new RecordInspector.RecordCollector();
     s.VisitContainedRecords(rc, offset);
     return rc.Records;
 }
Esempio n. 4
0
        /// <summary>
        /// used internally to Set the properties given a Sheet object
        /// </summary>
        /// <param name="sheet">The _sheet.</param>
        private void SetPropertiesFromSheet(InternalSheet sheet)
        {

            RowRecord row = sheet.NextRow;
            bool rowRecordsAlreadyPresent = row != null;

            while (row != null)
            {
                CreateRowFromRecord(row);

                row = sheet.NextRow;
            }

            CellValueRecordInterface[] cvals = sheet.GetValueRecords();
            long timestart = DateTime.Now.Millisecond;

            //if (log.Check(POILogger.DEBUG))
            //    log.Log(DEBUG, "Time at start of cell creating in HSSF _sheet = ",
            //        timestart);
            HSSFRow lastrow = null;

            // Add every cell to its row
            for (int i = 0; i < cvals.Length; i++)
            {
                CellValueRecordInterface cval = cvals[i];
                long cellstart = DateTime.Now.Millisecond;
                HSSFRow hrow = lastrow;

                if ((lastrow == null) || (lastrow.RowNum != cval.Row))
                {
                    hrow = (HSSFRow)GetRow(cval.Row);
                    if (hrow == null)
                    {
                        // Some tools (like Perl module SpReadsheet::WriteExcel - bug 41187) skip the RowRecords 
                        // Excel, OpenOffice.org and GoogleDocs are all OK with this, so POI should be too.
                        if (rowRecordsAlreadyPresent)
                        {
                            // if at least one row record is present, all should be present.
                            throw new Exception("Unexpected missing row when some rows already present, the file is wrong");
                        }
                        // Create the row record on the fly now.
                        RowRecord rowRec = new RowRecord(cval.Row);
                        _sheet.AddRow(rowRec);
                        hrow = CreateRowFromRecord(rowRec);
                    }
                }
                if (hrow != null)
                {
                    lastrow = hrow;
                    //if (log.Check(POILogger.DEBUG))
                    //{
                    //    if (cval is Record)
                    //    {
                    //        log.log(DEBUG, "record id = " + Integer.toHexString(((Record)cval).getSid()));
                    //    }
                    //    else
                    //    {
                    //        log.log(DEBUG, "record = " + cval);
                    //    }
                    //}

                    hrow.CreateCellFromRecord(cval);

                    //if (log.Check(POILogger.DEBUG))
                    //    log.Log(DEBUG, "record took ",DateTime.Now.Millisecond - cellstart);
                }
                else
                {
                    cval = null;
                }
            }
            //if (log.Check(POILogger.DEBUG))
            //    log.Log(DEBUG, "total _sheet cell creation took ",
            //        DateTime.Now.Millisecond - timestart);
        }
Esempio n. 5
0
 /// <summary>
 /// Creates an HSSFSheet representing the given Sheet object.  Should only be
 /// called by HSSFWorkbook when reading in an exisiting file.
 /// </summary>
 /// <param name="workbook">The HSSF Workbook object associated with the _sheet.</param>
 /// <param name="sheet">lowlevel Sheet object this _sheet will represent</param>
 /// <see cref="NPOI.HSSF.UserModel.HSSFWorkbook(NPOI.POIFS.FileSystem.DirectoryNode, bool)"/>
 public HSSFSheet(HSSFWorkbook workbook, InternalSheet sheet)
 {
     this._sheet = sheet;
     rows = new Dictionary<int, NPOI.SS.UserModel.IRow>();
     this._workbook = workbook;
     this.book = _workbook.Workbook;
     SetPropertiesFromSheet(_sheet);
 }
Esempio n. 6
0
            /**
     * Check if the cloned sheet has drawings. If yes, then allocate a new drawing group ID and
     * re-generate shape IDs
     *
     * @param sheet the cloned sheet
     */
        public void CloneDrawings(InternalSheet sheet)
        {

            FindDrawingGroup();

            if (drawingManager == null)
            {
                //this workbook does not have drawings
                return;
            }

            //check if the cloned sheet has drawings
            int aggLoc = sheet.AggregateDrawingRecords(drawingManager, false);
            if (aggLoc != -1)
            {
                EscherAggregate agg = (EscherAggregate)sheet.FindFirstRecordBySid(EscherAggregate.sid);
                EscherContainerRecord escherContainer = agg.GetEscherContainer();
                if (escherContainer == null)
                {
                    return;
                }

                EscherDggRecord dgg = drawingManager.GetDgg();

                //register a new drawing group for the cloned sheet
                int dgId = drawingManager.FindNewDrawingGroupId();
                dgg.AddCluster(dgId, 0);
                dgg.DrawingsSaved = dgg.DrawingsSaved + 1;

                EscherDgRecord dg = null;
                for (IEnumerator it = escherContainer.ChildRecords.GetEnumerator(); it.MoveNext(); )
                {
                    Object er = it.Current;
                    if (er is EscherDgRecord)
                    {
                        dg = (EscherDgRecord)er;
                        //update id of the drawing in the cloned sheet
                        dg.Options = ((short)(dgId << 4));
                    }
                    else if (er is EscherContainerRecord)
                    {
                        //recursively find shape records and re-generate shapeId
                        ArrayList spRecords = new ArrayList();
                        EscherContainerRecord cp = (EscherContainerRecord)er;
                        for (IEnumerator spIt = cp.ChildRecords.GetEnumerator(); spIt.MoveNext(); )
                        {
                            EscherContainerRecord shapeContainer = (EscherContainerRecord)spIt.Current;

                            foreach (EscherRecord shapeChildRecord in shapeContainer.ChildRecords)
                            {
                                int recordId = shapeChildRecord.RecordId;
                                if (recordId == EscherSpRecord.RECORD_ID)
                                {
                                    EscherSpRecord sp = (EscherSpRecord)shapeChildRecord;
                                    int shapeId = drawingManager.AllocateShapeId((short)dgId, dg);
                                    //allocateShapeId increments the number of shapes. roll back to the previous value
                                    dg.NumShapes = (dg.NumShapes - 1);
                                    sp.ShapeId = (shapeId);
                                }
                                else if (recordId == EscherOptRecord.RECORD_ID)
                                {
                                    EscherOptRecord opt = (EscherOptRecord)shapeChildRecord;
                                    EscherSimpleProperty prop = (EscherSimpleProperty)opt.Lookup(
                                            EscherProperties.BLIP__BLIPTODISPLAY);
                                    if (prop != null)
                                    {
                                        int pictureIndex = prop.PropertyValue;
                                        // increment reference count for pictures
                                        EscherBSERecord bse = GetBSERecord(pictureIndex);
                                        bse.Ref = bse.Ref + 1;
                                    }

                                }

                            }
                        }
                    }
                }

            }
        }
Esempio n. 7
0
        /// <summary>
        /// Cell comment Finder.
        /// Returns cell comment for the specified sheet, row and column.
        /// </summary>
        /// <param name="sheet">The sheet.</param>
        /// <param name="row">The row.</param>
        /// <param name="column">The column.</param>
        /// <returns>cell comment or 
        /// <c>null</c>
        ///  if not found</returns>
        public static HSSFComment FindCellComment(InternalSheet sheet, int row, int column)
        {
            HSSFComment comment = null;
            Dictionary<int, TextObjectRecord> noteTxo = new Dictionary<int, TextObjectRecord>(); //map shapeId and TextObjectRecord
            int i = 0;
            for (IEnumerator it = sheet.Records.GetEnumerator(); it.MoveNext(); )
            {
                RecordBase rec = (RecordBase)it.Current;
                if (rec is NoteRecord)
                {
                    NoteRecord note = (NoteRecord)rec;
                    if (note.Row == row && note.Column == column)
                    {
                        if (i < noteTxo.Count)
                        {
                            TextObjectRecord txo = (TextObjectRecord)noteTxo[note.ShapeId];
                            comment = new HSSFComment(note, txo);
                            comment.Row = note.Row;
                            comment.Column = note.Column;
                            comment.Author = note.Author;
                            comment.Visible = (note.Flags == NoteRecord.NOTE_VISIBLE);
                            comment.String = txo.Str;
                            break;
                        }
                    }
                }
                else if (rec is ObjRecord)
                {
                    ObjRecord obj = (ObjRecord)rec;
                    SubRecord sub = obj.SubRecords[0];
                    if (sub is CommonObjectDataSubRecord)
                    {
                        CommonObjectDataSubRecord cmo = (CommonObjectDataSubRecord)sub;
                        if (cmo.ObjectType == CommonObjectType.COMMENT)
                        {
                            //Find the nearest TextObjectRecord which holds comment's text and map it to its shapeId
                            while (it.MoveNext())
                            {
                                rec = (Record)it.Current;
                                if (rec is TextObjectRecord)
                                {
                                    noteTxo.Add(cmo.ObjectId, (TextObjectRecord)rec);
                                    break;
                                }
                            }

                        }
                    }
                }
            }
            return comment;
        }
Esempio n. 8
0
        private void CheckSheetRecords(InternalSheet sheet)
        {
            IList records = sheet.Records;
            Assert.IsTrue(records[0] is BOFRecord);
            Assert.IsTrue(records[records.Count - 1] is EOFRecord);

            CheckRecordOrder(records, sheetRecords);
            //        CheckRecordstogether(records, sheetRecords);
        }
 /* package */
 public HSSFSheetConditionalFormatting(HSSFWorkbook workbook, InternalSheet sheet)
 {
     _workbook = workbook;
     _conditionalFormattingTable = sheet.ConditionalFormattingTable;
 }
Esempio n. 10
0
        /**
         * Check if the cloned sheet has drawings. If yes, then allocate a new drawing group ID and
         * re-generate shape IDs
         *
         * @param sheet the cloned sheet
         */
        public void CloneDrawings(InternalSheet sheet)
        {
            FindDrawingGroup();

            if (drawingManager == null)
            {
                //this workbook does not have drawings
                return;
            }

            //check if the cloned sheet has drawings
            int aggLoc = sheet.AggregateDrawingRecords(drawingManager, false);
            if (aggLoc != -1)
            {
                EscherAggregate agg = (EscherAggregate)sheet.FindFirstRecordBySid(EscherAggregate.sid);
                EscherContainerRecord escherContainer = agg.GetEscherContainer();
                if (escherContainer == null)
                {
                    return;
                }

                EscherDggRecord dgg = drawingManager.GetDgg();

                //register a new drawing group for the cloned sheet
                int dgId = drawingManager.FindNewDrawingGroupId();
                dgg.AddCluster(dgId, 0);
                dgg.DrawingsSaved = dgg.DrawingsSaved + 1;

                EscherDgRecord dg = null;
                for (IEnumerator it = escherContainer.ChildRecords.GetEnumerator(); it.MoveNext(); )
                {
                    Object er = it.Current;
                    if (er is EscherDgRecord)
                    {
                        dg = (EscherDgRecord)er;
                        //update id of the drawing in the cloned sheet
                        dg.Options = ((short)(dgId << 4));
                    }
                    else if (er is EscherContainerRecord)
                    {
                        //recursively find shape records and re-generate shapeId
                        ArrayList spRecords = new ArrayList();
                        EscherContainerRecord cp = (EscherContainerRecord)er;
                        cp.GetRecordsById(EscherSpRecord.RECORD_ID, ref spRecords);
                        for (IEnumerator spIt = spRecords.GetEnumerator(); spIt.MoveNext(); )
                        {
                            EscherSpRecord sp = (EscherSpRecord)spIt.Current;
                            int shapeId = drawingManager.AllocateShapeId((short)dgId, dg);
                            //allocateShapeId increments the number of shapes. roll back to the previous value
                            dg.NumShapes = (dg.NumShapes - 1);
                            sp.ShapeId = (shapeId);
                        }
                    }
                }

            }
        }