Esempio n. 1
0
 /// <summary>
 /// This must be called to Setup the internal work book references whenever
 /// a RichTextString Is Added to a cell
 /// </summary>
 /// <param name="book">The workbook.</param>
 /// <param name="record">The record.</param>
 public void SetWorkbookReferences(InternalWorkbook book, LabelSSTRecord record)
 {
     this._book   = book;
     this._record = record;
 }
Esempio n. 2
0
        public void TestMissingCellRecords()
        {
            OpenNormal();

            // Find the cell at 0,0
            int cell00 = -1;

            for (int i = 0; i < r.Length; i++)
            {
                if (r[i] is LabelSSTRecord)
                {
                    LabelSSTRecord lr = (LabelSSTRecord)r[i];
                    if (lr.Row == 0 && lr.Column == 0)
                    {
                        cell00 = i;
                    }
                }
            }
            Assert.IsTrue(cell00 > -1);

            // We have rows 0, 1, 2, 20 and 21
            // Row 0 has 1 entry, 0
            // Row 1 has 4 entries, 0+3
            // Row 2 has 6 entries, 0+5
            // Row 20 has 5 entries, 0-5
            // Row 21 has 7 entries, 0+1+3+5+6
            // Row 22 has 12 entries, 0+3+4+11

            // Row 0
            Assert.IsFalse(r[cell00 + 0] is MissingCellDummyRecord);
            Assert.IsFalse(r[cell00 + 1] is MissingCellDummyRecord);

            // Row 1
            Assert.IsFalse(r[cell00 + 2] is MissingCellDummyRecord);
            Assert.IsTrue(r[cell00 + 3] is MissingCellDummyRecord);
            Assert.IsTrue(r[cell00 + 4] is MissingCellDummyRecord);
            Assert.IsFalse(r[cell00 + 5] is MissingCellDummyRecord);
            Assert.IsFalse(r[cell00 + 6] is MissingCellDummyRecord);

            // Row 2
            Assert.IsFalse(r[cell00 + 7] is MissingCellDummyRecord);
            Assert.IsTrue(r[cell00 + 8] is MissingCellDummyRecord);
            Assert.IsTrue(r[cell00 + 9] is MissingCellDummyRecord);
            Assert.IsTrue(r[cell00 + 10] is MissingCellDummyRecord);
            Assert.IsTrue(r[cell00 + 11] is MissingCellDummyRecord);
            Assert.IsFalse(r[cell00 + 12] is MissingCellDummyRecord);
            Assert.IsFalse(r[cell00 + 13] is MissingCellDummyRecord);

            // Row 3-19
            Assert.IsFalse(r[cell00 + 14] is MissingCellDummyRecord);
            Assert.IsFalse(r[cell00 + 15] is MissingCellDummyRecord);

            // Row 20
            Assert.IsFalse(r[cell00 + 31] is MissingCellDummyRecord);
            Assert.IsFalse(r[cell00 + 32] is MissingCellDummyRecord);
            Assert.IsFalse(r[cell00 + 33] is MissingCellDummyRecord);
            Assert.IsFalse(r[cell00 + 34] is MissingCellDummyRecord);
            Assert.IsFalse(r[cell00 + 35] is MissingCellDummyRecord);
            Assert.IsFalse(r[cell00 + 36] is MissingCellDummyRecord);

            // Row 21
            Assert.IsFalse(r[cell00 + 37] is MissingCellDummyRecord);
            Assert.IsFalse(r[cell00 + 38] is MissingCellDummyRecord);
            Assert.IsTrue(r[cell00 + 39] is MissingCellDummyRecord);
            Assert.IsFalse(r[cell00 + 40] is MissingCellDummyRecord);
            Assert.IsTrue(r[cell00 + 41] is MissingCellDummyRecord);
            Assert.IsFalse(r[cell00 + 42] is MissingCellDummyRecord);
            Assert.IsFalse(r[cell00 + 43] is MissingCellDummyRecord);
            Assert.IsFalse(r[cell00 + 44] is MissingCellDummyRecord);

            // Row 22
            Assert.IsFalse(r[cell00 + 45] is MissingCellDummyRecord);
            Assert.IsTrue(r[cell00 + 46] is MissingCellDummyRecord);
            Assert.IsTrue(r[cell00 + 47] is MissingCellDummyRecord);
            Assert.IsFalse(r[cell00 + 48] is MissingCellDummyRecord);
            Assert.IsFalse(r[cell00 + 49] is MissingCellDummyRecord);
            Assert.IsTrue(r[cell00 + 50] is MissingCellDummyRecord);
            Assert.IsTrue(r[cell00 + 51] is MissingCellDummyRecord);
            Assert.IsTrue(r[cell00 + 52] is MissingCellDummyRecord);
            Assert.IsTrue(r[cell00 + 53] is MissingCellDummyRecord);
            Assert.IsTrue(r[cell00 + 54] is MissingCellDummyRecord);
            Assert.IsTrue(r[cell00 + 55] is MissingCellDummyRecord);
            Assert.IsFalse(r[cell00 + 56] is MissingCellDummyRecord);
            Assert.IsFalse(r[cell00 + 57] is MissingCellDummyRecord);

            // Check some numbers
            MissingCellDummyRecord mc;

            mc = (MissingCellDummyRecord)r[cell00 + 3];
            Assert.AreEqual(1, mc.Row);
            Assert.AreEqual(1, mc.Column);
            mc = (MissingCellDummyRecord)r[cell00 + 4];
            Assert.AreEqual(1, mc.Row);
            Assert.AreEqual(2, mc.Column);

            mc = (MissingCellDummyRecord)r[cell00 + 8];
            Assert.AreEqual(2, mc.Row);
            Assert.AreEqual(1, mc.Column);
            mc = (MissingCellDummyRecord)r[cell00 + 9];
            Assert.AreEqual(2, mc.Row);
            Assert.AreEqual(2, mc.Column);

            mc = (MissingCellDummyRecord)r[cell00 + 55];
            Assert.AreEqual(22, mc.Row);
            Assert.AreEqual(10, mc.Column);
        }
Esempio n. 3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="HSSFRichTextString"/> class.
        /// </summary>
        /// <param name="book">The workbook.</param>
        /// <param name="record">The record.</param>
        public HSSFRichTextString(InternalWorkbook book, LabelSSTRecord record)
        {
            SetWorkbookReferences(book, record);

            this._string = book.GetSSTString(record.SSTIndex);
        }
Esempio n. 4
0
        public void TestEndOfRowRecords()
        {
            OpenNormal();

            // Find the cell at 0,0
            int cell00 = -1;

            for (int i = 0; i < r.Length; i++)
            {
                if (r[i] is LabelSSTRecord)
                {
                    LabelSSTRecord lr = (LabelSSTRecord)r[i];
                    if (lr.Row == 0 && lr.Column == 0)
                    {
                        cell00 = i;
                    }
                }
            }
            Assert.IsTrue(cell00 > -1);

            // We have rows 0, 1, 2, 20 and 21
            // Row 0 has 1 entry
            // Row 1 has 4 entries
            // Row 2 has 6 entries
            // Row 20 has 5 entries
            // Row 21 has 7 entries
            // Row 22 has 12 entries

            // Row 0
            Assert.IsFalse(r[cell00 + 0] is LastCellOfRowDummyRecord);
            Assert.IsTrue(r[cell00 + 1] is LastCellOfRowDummyRecord);
            // Row 1
            Assert.IsFalse(r[cell00 + 2] is LastCellOfRowDummyRecord);
            Assert.IsFalse(r[cell00 + 3] is LastCellOfRowDummyRecord);
            Assert.IsFalse(r[cell00 + 4] is LastCellOfRowDummyRecord);
            Assert.IsFalse(r[cell00 + 5] is LastCellOfRowDummyRecord);
            Assert.IsTrue(r[cell00 + 6] is LastCellOfRowDummyRecord);
            // Row 2
            Assert.IsFalse(r[cell00 + 7] is LastCellOfRowDummyRecord);
            Assert.IsFalse(r[cell00 + 8] is LastCellOfRowDummyRecord);
            Assert.IsFalse(r[cell00 + 9] is LastCellOfRowDummyRecord);
            Assert.IsFalse(r[cell00 + 10] is LastCellOfRowDummyRecord);
            Assert.IsFalse(r[cell00 + 11] is LastCellOfRowDummyRecord);
            Assert.IsFalse(r[cell00 + 12] is LastCellOfRowDummyRecord);
            Assert.IsTrue(r[cell00 + 13] is LastCellOfRowDummyRecord);
            // Row 3 -> 19
            Assert.IsTrue(r[cell00 + 14] is LastCellOfRowDummyRecord);
            Assert.IsTrue(r[cell00 + 15] is LastCellOfRowDummyRecord);
            Assert.IsTrue(r[cell00 + 16] is LastCellOfRowDummyRecord);
            Assert.IsTrue(r[cell00 + 17] is LastCellOfRowDummyRecord);
            Assert.IsTrue(r[cell00 + 18] is LastCellOfRowDummyRecord);
            Assert.IsTrue(r[cell00 + 19] is LastCellOfRowDummyRecord);
            Assert.IsTrue(r[cell00 + 20] is LastCellOfRowDummyRecord);
            Assert.IsTrue(r[cell00 + 21] is LastCellOfRowDummyRecord);
            Assert.IsTrue(r[cell00 + 22] is LastCellOfRowDummyRecord);
            Assert.IsTrue(r[cell00 + 23] is LastCellOfRowDummyRecord);
            Assert.IsTrue(r[cell00 + 24] is LastCellOfRowDummyRecord);
            Assert.IsTrue(r[cell00 + 25] is LastCellOfRowDummyRecord);
            Assert.IsTrue(r[cell00 + 26] is LastCellOfRowDummyRecord);
            Assert.IsTrue(r[cell00 + 27] is LastCellOfRowDummyRecord);
            Assert.IsTrue(r[cell00 + 28] is LastCellOfRowDummyRecord);
            Assert.IsTrue(r[cell00 + 29] is LastCellOfRowDummyRecord);
            Assert.IsTrue(r[cell00 + 30] is LastCellOfRowDummyRecord);
            // Row 20
            Assert.IsFalse(r[cell00 + 31] is LastCellOfRowDummyRecord);
            Assert.IsFalse(r[cell00 + 32] is LastCellOfRowDummyRecord);
            Assert.IsFalse(r[cell00 + 33] is LastCellOfRowDummyRecord);
            Assert.IsFalse(r[cell00 + 34] is LastCellOfRowDummyRecord);
            Assert.IsFalse(r[cell00 + 35] is LastCellOfRowDummyRecord);
            Assert.IsTrue(r[cell00 + 36] is LastCellOfRowDummyRecord);
            // Row 21
            Assert.IsFalse(r[cell00 + 37] is LastCellOfRowDummyRecord);
            Assert.IsFalse(r[cell00 + 38] is LastCellOfRowDummyRecord);
            Assert.IsFalse(r[cell00 + 39] is LastCellOfRowDummyRecord);
            Assert.IsFalse(r[cell00 + 40] is LastCellOfRowDummyRecord);
            Assert.IsFalse(r[cell00 + 41] is LastCellOfRowDummyRecord);
            Assert.IsFalse(r[cell00 + 42] is LastCellOfRowDummyRecord);
            Assert.IsFalse(r[cell00 + 43] is LastCellOfRowDummyRecord);
            Assert.IsTrue(r[cell00 + 44] is LastCellOfRowDummyRecord);
            // Row 22
            Assert.IsFalse(r[cell00 + 45] is LastCellOfRowDummyRecord);
            Assert.IsFalse(r[cell00 + 46] is LastCellOfRowDummyRecord);
            Assert.IsFalse(r[cell00 + 47] is LastCellOfRowDummyRecord);
            Assert.IsFalse(r[cell00 + 48] is LastCellOfRowDummyRecord);
            Assert.IsFalse(r[cell00 + 49] is LastCellOfRowDummyRecord);
            Assert.IsFalse(r[cell00 + 50] is LastCellOfRowDummyRecord);
            Assert.IsFalse(r[cell00 + 51] is LastCellOfRowDummyRecord);
            Assert.IsFalse(r[cell00 + 52] is LastCellOfRowDummyRecord);
            Assert.IsFalse(r[cell00 + 53] is LastCellOfRowDummyRecord);
            Assert.IsFalse(r[cell00 + 54] is LastCellOfRowDummyRecord);
            Assert.IsFalse(r[cell00 + 55] is LastCellOfRowDummyRecord);
            Assert.IsFalse(r[cell00 + 56] is LastCellOfRowDummyRecord);
            Assert.IsTrue(r[cell00 + 57] is LastCellOfRowDummyRecord);

            // Check the numbers of the last seen columns
            LastCellOfRowDummyRecord[] lrs = new LastCellOfRowDummyRecord[24];
            int lrscount = 0;

            for (int i = 0; i < r.Length; i++)
            {
                if (r[i] is LastCellOfRowDummyRecord)
                {
                    lrs[lrscount] = (LastCellOfRowDummyRecord)r[i];
                    lrscount++;
                }
            }

            Assert.AreEqual(0, lrs[0].LastColumnNumber);
            Assert.AreEqual(0, lrs[0].Row);

            Assert.AreEqual(3, lrs[1].LastColumnNumber);
            Assert.AreEqual(1, lrs[1].Row);

            Assert.AreEqual(5, lrs[2].LastColumnNumber);
            Assert.AreEqual(2, lrs[2].Row);

            for (int i = 3; i <= 19; i++)
            {
                Assert.AreEqual(-1, lrs[i].LastColumnNumber);
                Assert.AreEqual(i, lrs[i].Row);
            }

            Assert.AreEqual(4, lrs[20].LastColumnNumber);
            Assert.AreEqual(20, lrs[20].Row);

            Assert.AreEqual(6, lrs[21].LastColumnNumber);
            Assert.AreEqual(21, lrs[21].Row);

            Assert.AreEqual(11, lrs[22].LastColumnNumber);
            Assert.AreEqual(22, lrs[22].Row);
        }
Esempio n. 5
0
            /// <summary>
            /// Process an HSSF Record. Called when a record occurs in an HSSF file.
            /// </summary>
            /// <param name="record"></param>
            public void ProcessRecord(Record record)
            {
                String thisText = null;
                int    thisRow  = -1;

                switch (record.Sid)
                {
                case BoundSheetRecord.sid:
                    BoundSheetRecord sr = (BoundSheetRecord)record;
                    sheetNames.Add(sr.Sheetname);
                    break;

                case BOFRecord.sid:
                    BOFRecord bof = (BOFRecord)record;
                    if (bof.Type == BOFRecordType.Worksheet)
                    {
                        sheetNum++;
                        rowNum = -1;

                        if (includeSheetNames)
                        {
                            if (text.Length > 0)
                            {
                                text.Append("\n");
                            }
                            text.Append(sheetNames[sheetNum]);
                        }
                    }
                    break;

                case SSTRecord.sid:
                    sstRecord = (SSTRecord)record;
                    break;

                case FormulaRecord.sid:
                    FormulaRecord frec = (FormulaRecord)record;
                    thisRow = frec.Row;

                    if (formulasNotResults)
                    {
                        thisText = HSSFFormulaParser.ToFormulaString((HSSFWorkbook)null, frec.ParsedExpression);
                    }
                    else
                    {
                        if (frec.HasCachedResultString)
                        {
                            // Formula result is a string
                            // This is stored in the next record
                            outputNextStringValue = true;
                            nextRow = frec.Row;
                        }
                        else
                        {
                            thisText = FormatNumberDateCell(frec, frec.Value);
                        }
                    }
                    break;

                case StringRecord.sid:
                    if (outputNextStringValue)
                    {
                        // String for formula
                        StringRecord srec = (StringRecord)record;
                        thisText = srec.String;
                        thisRow  = nextRow;
                        outputNextStringValue = false;
                    }
                    break;

                case LabelRecord.sid:
                    LabelRecord lrec = (LabelRecord)record;
                    thisRow  = lrec.Row;
                    thisText = lrec.Value;
                    break;

                case LabelSSTRecord.sid:
                    LabelSSTRecord lsrec = (LabelSSTRecord)record;
                    thisRow = lsrec.Row;
                    if (sstRecord == null)
                    {
                        throw new Exception("No SST record found");
                    }
                    thisText = sstRecord.GetString(lsrec.SSTIndex).ToString();
                    break;

                case NoteRecord.sid:
                    NoteRecord nrec = (NoteRecord)record;
                    thisRow = nrec.Row;
                    // TODO: Find object to match nrec.GetShapeId()
                    break;

                case NumberRecord.sid:
                    NumberRecord numrec = (NumberRecord)record;
                    thisRow  = numrec.Row;
                    thisText = FormatNumberDateCell(numrec, numrec.Value);
                    break;

                default:
                    break;
                }

                if (thisText != null)
                {
                    if (thisRow != rowNum)
                    {
                        rowNum = thisRow;
                        if (text.Length > 0)
                        {
                            text.Append("\n");
                        }
                    }
                    else
                    {
                        text.Append("\t");
                    }
                    text.Append(thisText);
                }
            }
Esempio n. 6
0
        /// <summary>
        /// Sets the cell type. The SetValue flag indicates whether to bother about
        /// trying to preserve the current value in the new record if one is Created.
        /// The SetCellValue method will call this method with false in SetValue
        /// since it will overWrite the cell value later
        /// </summary>
        /// <param name="cellType">Type of the cell.</param>
        /// <param name="setValue">if set to <c>true</c> [set value].</param>
        /// <param name="row">The row.</param>
        /// <param name="col">The col.</param>
        /// <param name="styleIndex">Index of the style.</param>
        private void SetCellType(CellType cellType, bool setValue, int row, int col, short styleIndex)
        {
            if (cellType > CellType.Error)
            {
                throw new Exception("I have no idea what type that Is!");
            }
            switch (cellType)
            {
            case CellType.Formula:
                FormulaRecordAggregate frec = null;

                if (cellType != this.cellType)
                {
                    frec = _sheet.Sheet.RowsAggregate.CreateFormula(row, col);
                }
                else
                {
                    frec = (FormulaRecordAggregate)_record;
                }
                frec.Column = col;
                if (setValue)
                {
                    frec.FormulaRecord.Value = NumericCellValue;
                }
                frec.XFIndex = styleIndex;
                frec.Row     = row;
                _record      = frec;
                break;

            case CellType.Numeric:
                NumberRecord nrec = null;

                if (cellType != this.cellType)
                {
                    nrec = new NumberRecord();
                }
                else
                {
                    nrec = (NumberRecord)_record;
                }
                nrec.Column = col;
                if (setValue)
                {
                    nrec.Value = NumericCellValue;
                }
                nrec.XFIndex = styleIndex;
                nrec.Row     = row;
                _record      = nrec;
                break;

            case CellType.String:
                LabelSSTRecord lrec = null;

                if (cellType != this.cellType)
                {
                    lrec = new LabelSSTRecord();
                }
                else
                {
                    lrec = (LabelSSTRecord)_record;
                }
                lrec.Column  = col;
                lrec.Row     = row;
                lrec.XFIndex = styleIndex;
                if (setValue)
                {
                    String str      = ConvertCellValueToString();
                    int    sstIndex = book.Workbook.AddSSTString(new UnicodeString(str));
                    lrec.SSTIndex = (sstIndex);
                    UnicodeString us = book.Workbook.GetSSTString(sstIndex);
                    stringValue = new HSSFRichTextString();
                    stringValue.UnicodeString = us;
                }
                _record = lrec;
                break;

            case CellType.Blank:
                BlankRecord brec = null;

                if (cellType != this.cellType)
                {
                    brec = new BlankRecord();
                }
                else
                {
                    brec = (BlankRecord)_record;
                }
                brec.Column = col;

                // During construction the cellStyle may be null for a Blank cell.
                brec.XFIndex = styleIndex;
                brec.Row     = row;
                _record      = brec;
                break;

            case CellType.Boolean:
                BoolErrRecord boolRec = null;

                if (cellType != this.cellType)
                {
                    boolRec = new BoolErrRecord();
                }
                else
                {
                    boolRec = (BoolErrRecord)_record;
                }
                boolRec.Column = col;
                if (setValue)
                {
                    boolRec.SetValue(ConvertCellValueToBoolean());
                }
                boolRec.XFIndex = styleIndex;
                boolRec.Row     = row;
                _record         = boolRec;
                break;

            case CellType.Error:
                BoolErrRecord errRec = null;

                if (cellType != this.cellType)
                {
                    errRec = new BoolErrRecord();
                }
                else
                {
                    errRec = (BoolErrRecord)_record;
                }
                errRec.Column = col;
                if (setValue)
                {
                    errRec.SetValue((byte)HSSFErrorConstants.ERROR_VALUE);
                }
                errRec.XFIndex = styleIndex;
                errRec.Row     = row;
                _record        = errRec;
                break;
            }
            if (cellType != this.cellType &&
                this.cellType != CellType.Unknown)  // Special Value to indicate an Uninitialized Cell
            {
                _sheet.Sheet.ReplaceValueRecord(_record);
            }
            this.cellType = cellType;
        }
        /**
         * Reads in the contents of this sheet
         */
        public void read()
        {
            Record r = null;
            BaseSharedFormulaRecord sharedFormula = null;
            bool sharedFormulaAdded = false;

            bool cont = true;

            // Set the position within the file
            excelFile.setPos(startPosition);

            // Handles to the last drawing and obj records
            MsoDrawingRecord msoRecord = null;
            ObjRecord objRecord = null;
            bool firstMsoRecord = true;

            // Handle to the last conditional format record
            ConditionalFormat condFormat = null;

            // Handle to the autofilter records
            FilterModeRecord filterMode = null;
            AutoFilterInfoRecord autoFilterInfo = null;

            // A handle to window2 record
            Window2Record window2Record = null;

            // A handle to printgridlines record
            PrintGridLinesRecord printGridLinesRecord = null;

            // A handle to printheaders record
            PrintHeadersRecord printHeadersRecord = null;

            // Hash map of comments, indexed on objectId.  As each corresponding
            // note record is encountered, these are removed from the array
            Dictionary<uint,Comment> comments = new Dictionary<uint,Comment>();

            // A list of object ids - used for cross referencing
            ArrayList objectIds = new ArrayList();

            // A handle to a continue record read in
            ContinueRecord continueRecord = null;

            while (cont)
                {
                r = excelFile.next();
                Type type = r.getType();

                if (type == Type.UNKNOWN && r.getCode() == 0)
                    {
                    //logger.warn("Biff code zero found");

                    // Try a dimension record
                    if (r.getLength() == 0xa)
                        {
                        //logger.warn("Biff code zero found - trying a dimension record.");
                        r.setType(Type.DIMENSION);
                        }
                    else
                        {
                        //logger.warn("Biff code zero found - Ignoring.");
                        }
                    }

                if (type == Type.DIMENSION)
                    {
                    DimensionRecord dr = null;

                    if (workbookBof.isBiff8())
                        {
                        dr = new DimensionRecord(r);
                        }
                    else
                        {
                        dr = new DimensionRecord(r, DimensionRecord.biff7);
                        }
                    numRows = dr.getNumberOfRows();
                    numCols = dr.getNumberOfColumns();
                    cells = new Cell[numRows, numCols];
                    }
                else if (type == Type.LABELSST)
                    {
                    LabelSSTRecord label = new LabelSSTRecord(r,
                                                              sharedStrings,
                                                              formattingRecords,
                                                              sheet);
                    addCell(label);
                    }
                else if (type == Type.RK || type == Type.RK2)
                    {
                    RKRecord rkr = new RKRecord(r, formattingRecords, sheet);

                    if (formattingRecords.isDate(rkr.getXFIndex()))
                        {
                        DateCell dc = new DateRecord
                          (rkr, rkr.getXFIndex(), formattingRecords, nineteenFour, sheet);
                        addCell(dc);
                        }
                    else
                        {
                        addCell(rkr);
                        }
                    }
                else if (type == Type.HLINK)
                    {
                    HyperlinkRecord hr = new HyperlinkRecord(r, sheet, workbookSettings);
                    hyperlinks.Add(hr);
                    }
                else if (type == Type.MERGEDCELLS)
                    {
                    MergedCellsRecord mc = new MergedCellsRecord(r, sheet);
                    if (mergedCells == null)
                        {
                        mergedCells = mc.getRanges();
                        }
                    else
                        {
                        Range[] newMergedCells =
                          new Range[mergedCells.Length + mc.getRanges().Length];
                        System.Array.Copy(mergedCells, 0, newMergedCells, 0,
                                         mergedCells.Length);
                        System.Array.Copy(mc.getRanges(),
                                         0,
                                         newMergedCells, mergedCells.Length,
                                         mc.getRanges().Length);
                        mergedCells = newMergedCells;
                        }
                    }
                else if (type == Type.MULRK)
                    {
                    MulRKRecord mulrk = new MulRKRecord(r);

                    // Get the individual cell records from the multiple record
                    int num = mulrk.getNumberOfColumns();
                    int ixf = 0;
                    for (int i = 0; i < num; i++)
                        {
                        ixf = mulrk.getXFIndex(i);

                        NumberValue nv = new NumberValue
                          (mulrk.getRow(),
                           mulrk.getFirstColumn() + i,
                           RKHelper.getDouble(mulrk.getRKNumber(i)),
                           ixf,
                           formattingRecords,
                           sheet);

                        if (formattingRecords.isDate(ixf))
                            {
                            DateCell dc = new DateRecord(nv,
                                                         ixf,
                                                         formattingRecords,
                                                         nineteenFour,
                                                         sheet);
                            addCell(dc);
                            }
                        else
                            {
                            nv.setNumberFormat(formattingRecords.getNumberFormat(ixf));
                            addCell(nv);
                            }
                        }
                    }
                else if (type == Type.NUMBER)
                    {
                    NumberRecord nr = new NumberRecord(r, formattingRecords, sheet);

                    if (formattingRecords.isDate(nr.getXFIndex()))
                        {
                        DateCell dc = new DateRecord(nr,nr.getXFIndex(),formattingRecords,nineteenFour, sheet);
                        addCell(dc);
                        }
                    else
                        addCell(nr);
                    }
                else if (type == Type.BOOLERR)
                    {
                    BooleanRecord br = new BooleanRecord(r, formattingRecords, sheet);

                    if (br.isError())
                        {
                        ErrorRecord er = new ErrorRecord(br.getRecord(), formattingRecords,
                                                         sheet);
                        addCell(er);
                        }
                    else
                        {
                        addCell(br);
                        }
                    }
                else if (type == Type.PRINTGRIDLINES)
                    {
                    printGridLinesRecord = new PrintGridLinesRecord(r);
                    settings.setPrintGridLines(printGridLinesRecord.getPrintGridLines());
                    }
                else if (type == Type.PRINTHEADERS)
                    {
                    printHeadersRecord = new PrintHeadersRecord(r);
                    settings.setPrintHeaders(printHeadersRecord.getPrintHeaders());
                    }
                else if (type == Type.WINDOW2)
                    {
                    window2Record = null;

                    if (workbookBof.isBiff8())
                        {
                        window2Record = new Window2Record(r);
                        }
                    else
                        {
                        window2Record = new Window2Record(r, Window2Record.biff7);
                        }

                    settings.setShowGridLines(window2Record.getShowGridLines());
                    settings.setDisplayZeroValues(window2Record.getDisplayZeroValues());
                    settings.setSelected(true);
                    settings.setPageBreakPreviewMode(window2Record.isPageBreakPreview());
                    }
                else if (type == Type.PANE)
                    {
                    PaneRecord pr = new PaneRecord(r);

                    if (window2Record != null &&
                        window2Record.getFrozen())
                        {
                        settings.setVerticalFreeze(pr.getRowsVisible());
                        settings.setHorizontalFreeze(pr.getColumnsVisible());
                        }
                    }
                else if (type == Type.CONTINUE)
                    {
                    // don't know what this is for, but keep hold of it anyway
                    continueRecord = new ContinueRecord(r);
                    }
                else if (type == Type.NOTE)
                    {
                    if (!workbookSettings.getDrawingsDisabled())
                        {
                        NoteRecord nr = new NoteRecord(r);

                        // Get the comment for the object id
                        if (!comments.ContainsKey(nr.getObjectId()))
                            {
                            //logger.warn(" cannot find comment for note id " + nr.getObjectId() + "...ignoring");
                            }
                        else
                            {
                            Comment comment = comments[nr.getObjectId()];
                            comments.Remove(nr.getObjectId());

                            comment.setNote(nr);
                            drawings.Add(comment);
                            addCellComment(comment.getColumn(),
                                           comment.getRow(),
                                           comment.getText(),
                                           comment.getWidth(),
                                           comment.getHeight());
                            }
                        }
                    }
                else if (type == Type.ARRAY)
                    {
                    ;
                    }
                else if (type == Type.PROTECT)
                    {
                    ProtectRecord pr = new ProtectRecord(r);
                    settings.setProtected(pr.isProtected());
                    }
                else if (type == Type.SHAREDFORMULA)
                    {
                    if (sharedFormula == null)
                        {
                        //logger.warn("Shared template formula is null - " +
                        //            "trying most recent formula template");
                        SharedFormulaRecord lastSharedFormula = (SharedFormulaRecord)sharedFormulas[sharedFormulas.Count - 1];

                        if (lastSharedFormula != null)
                            sharedFormula = lastSharedFormula.getTemplateFormula();
                        }

                    SharedFormulaRecord sfr = new SharedFormulaRecord(r, sharedFormula, workbook, workbook, sheet);
                    sharedFormulas.Add(sfr);
                    sharedFormula = null;
                    }
                else if (type == Type.FORMULA || type == Type.FORMULA2)
                    {
                    FormulaRecord fr = new FormulaRecord(r,
                                                         excelFile,
                                                         formattingRecords,
                                                         workbook,
                                                         workbook,
                                                         sheet,
                                                         workbookSettings);

                    if (fr.isShared())
                        {
                        BaseSharedFormulaRecord prevSharedFormula = sharedFormula;
                        sharedFormula = (BaseSharedFormulaRecord)fr.getFormula();

                        // See if it fits in any of the shared formulas
                        sharedFormulaAdded = addToSharedFormulas(sharedFormula);

                        if (sharedFormulaAdded)
                            {
                            sharedFormula = prevSharedFormula;
                            }

                        // If we still haven't added the previous base shared formula,
                        // revert it to an ordinary formula and add it to the cell
                        if (!sharedFormulaAdded && prevSharedFormula != null)
                            {
                            // Do nothing.  It's possible for the biff file to contain the
                            // record sequence
                            // FORMULA-SHRFMLA-FORMULA-SHRFMLA-FORMULA-FORMULA-FORMULA
                            // ie. it first lists all the formula templates, then it
                            // lists all the individual formulas
                            addCell(revertSharedFormula(prevSharedFormula));
                            }
                        }
                    else
                        {
                        Cell cell = fr.getFormula();
                        try
                            {
                            // See if the formula evaluates to date
                            if (fr.getFormula().getType() == CellType.NUMBER_FORMULA)
                                {
                                NumberFormulaRecord nfr = (NumberFormulaRecord)fr.getFormula();
                                if (formattingRecords.isDate(nfr.getXFIndex()))
                                    {
                                    cell = new DateFormulaRecord(nfr,
                                                                 formattingRecords,
                                                                 workbook,
                                                                 workbook,
                                                                 nineteenFour,
                                                                 sheet);
                                    }
                                }

                            addCell(cell);
                            }
                        catch (FormulaException e)
                            {
                            // Something has gone wrong trying to read the formula data eg. it
                            // might be unsupported biff7 data
                            //logger.warn(CellReferenceHelper.getCellReference(cell.getColumn(), cell.getRow()) + " " + e.Message);
                            }
                        }
                    }
                else if (type == Type.LABEL)
                    {
                    LabelRecord lr = null;

                    if (workbookBof.isBiff8())
                        {
                        lr = new LabelRecord(r, formattingRecords, sheet, workbookSettings);
                        }
                    else
                        {
                        lr = new LabelRecord(r, formattingRecords, sheet, workbookSettings,
                                             LabelRecord.biff7);
                        }
                    addCell(lr);
                    }
                else if (type == Type.RSTRING)
                    {
                    RStringRecord lr = null;

                    // RString records are obsolete in biff 8
                    Assert.verify(!workbookBof.isBiff8());
                    lr = new RStringRecord(r, formattingRecords,
                                           sheet, workbookSettings,
                                           RStringRecord.biff7);
                    addCell(lr);
                    }
                else if (type == Type.NAME)
                    {
                    ;
                    }
                else if (type == Type.PASSWORD)
                    {
                    PasswordRecord pr = new PasswordRecord(r);
                    settings.setPasswordHash(pr.getPasswordHash());
                    }
                else if (type == Type.ROW)
                    {
                    RowRecord rr = new RowRecord(r);

                    // See if the row has anything funny about it
                    if (!rr.isDefaultHeight() ||
                        !rr.matchesDefaultFontHeight() ||
                        rr.isCollapsed() ||
                        rr.hasDefaultFormat() ||
                        rr.getOutlineLevel() != 0)
                        {
                        rowProperties.Add(rr);
                        }
                    }
                else if (type == Type.BLANK)
                    {
                    if (!workbookSettings.getIgnoreBlanks())
                        {
                        BlankCell bc = new BlankCell(r, formattingRecords, sheet);
                        addCell(bc);
                        }
                    }
                else if (type == Type.MULBLANK)
                    {
                    if (!workbookSettings.getIgnoreBlanks())
                        {
                        MulBlankRecord mulblank = new MulBlankRecord(r);

                        // Get the individual cell records from the multiple record
                        int num = mulblank.getNumberOfColumns();

                        for (int i = 0; i < num; i++)
                            {
                            int ixf = mulblank.getXFIndex(i);

                            MulBlankCell mbc = new MulBlankCell
                              (mulblank.getRow(),
                               mulblank.getFirstColumn() + i,
                               ixf,
                               formattingRecords,
                               sheet);

                            addCell(mbc);
                            }
                        }
                    }
                else if (type == Type.SCL)
                    {
                    SCLRecord scl = new SCLRecord(r);
                    settings.setZoomFactor(scl.getZoomFactor());
                    }
                else if (type == Type.COLINFO)
                    {
                    ColumnInfoRecord cir = new ColumnInfoRecord(r);
                    columnInfosArray.Add(cir);
                    }
                else if (type == Type.HEADER)
                    {
                    HeaderRecord hr = null;
                    if (workbookBof.isBiff8())
                        hr = new HeaderRecord(r, workbookSettings);
                    else
                        hr = new HeaderRecord(r, workbookSettings, HeaderRecord.biff7);

                    HeaderFooter header = new HeaderFooter(hr.getHeader());
                    settings.setHeader(header);
                    }
                else if (type == Type.FOOTER)
                    {
                    FooterRecord fr = null;
                    if (workbookBof.isBiff8())
                        {
                        fr = new FooterRecord(r, workbookSettings);
                        }
                    else
                        {
                        fr = new FooterRecord(r, workbookSettings, FooterRecord.biff7);
                        }

                    HeaderFooter footer = new HeaderFooter(fr.getFooter());
                    settings.setFooter(footer);
                    }
                else if (type == Type.SETUP)
                    {
                    SetupRecord sr = new SetupRecord(r);

                    // If the setup record has its not initialized bit set, then
                    // use the sheet settings default values
                    if (sr.getInitialized())
                        {
                        if (sr.isPortrait())
                            {
                            settings.setOrientation(PageOrientation.PORTRAIT);
                            }
                        else
                            {
                            settings.setOrientation(PageOrientation.LANDSCAPE);
                            }
                        if (sr.isRightDown())
                            {
                            settings.setPageOrder(PageOrder.RIGHT_THEN_DOWN);
                            }
                        else
                            {
                            settings.setPageOrder(PageOrder.DOWN_THEN_RIGHT);
                            }
                        settings.setPaperSize(PaperSize.getPaperSize(sr.getPaperSize()));
                        settings.setHeaderMargin(sr.getHeaderMargin());
                        settings.setFooterMargin(sr.getFooterMargin());
                        settings.setScaleFactor(sr.getScaleFactor());
                        settings.setPageStart(sr.getPageStart());
                        settings.setFitWidth(sr.getFitWidth());
                        settings.setFitHeight(sr.getFitHeight());
                        settings.setHorizontalPrintResolution
                          (sr.getHorizontalPrintResolution());
                        settings.setVerticalPrintResolution(sr.getVerticalPrintResolution());
                        settings.setCopies(sr.getCopies());

                        if (workspaceOptions != null)
                            {
                            settings.setFitToPages(workspaceOptions.getFitToPages());
                            }
                        }
                    }
                else if (type == Type.WSBOOL)
                    {
                    workspaceOptions = new WorkspaceInformationRecord(r);
                    }
                else if (type == Type.DEFCOLWIDTH)
                    {
                    DefaultColumnWidthRecord dcwr = new DefaultColumnWidthRecord(r);
                    settings.setDefaultColumnWidth(dcwr.getWidth());
                    }
                else if (type == Type.DEFAULTROWHEIGHT)
                    {
                    DefaultRowHeightRecord drhr = new DefaultRowHeightRecord(r);
                    if (drhr.getHeight() != 0)
                        {
                        settings.setDefaultRowHeight(drhr.getHeight());
                        }
                    }
                else if (type == Type.CONDFMT)
                    {
                    ConditionalFormatRangeRecord cfrr =
                      new ConditionalFormatRangeRecord(r);
                    condFormat = new ConditionalFormat(cfrr);
                    conditionalFormats.Add(condFormat);
                    }
                else if (type == Type.CF)
                    {
                    ConditionalFormatRecord cfr = new ConditionalFormatRecord(r);
                    condFormat.addCondition(cfr);
                    }
                else if (type == Type.FILTERMODE)
                    {
                    filterMode = new FilterModeRecord(r);
                    }
                else if (type == Type.AUTOFILTERINFO)
                    {
                    autoFilterInfo = new AutoFilterInfoRecord(r);
                    }
                else if (type == Type.AUTOFILTER)
                    {
                    if (!workbookSettings.getAutoFilterDisabled())
                        {
                        AutoFilterRecord af = new AutoFilterRecord(r);

                        if (autoFilter == null)
                            {
                            autoFilter = new AutoFilter(filterMode, autoFilterInfo);
                            filterMode = null;
                            autoFilterInfo = null;
                            }

                        autoFilter.add(af);
                        }
                    }
                else if (type == Type.LEFTMARGIN)
                    {
                    MarginRecord m = new LeftMarginRecord(r);
                    settings.setLeftMargin(m.getMargin());
                    }
                else if (type == Type.RIGHTMARGIN)
                    {
                    MarginRecord m = new RightMarginRecord(r);
                    settings.setRightMargin(m.getMargin());
                    }
                else if (type == Type.TOPMARGIN)
                    {
                    MarginRecord m = new TopMarginRecord(r);
                    settings.setTopMargin(m.getMargin());
                    }
                else if (type == Type.BOTTOMMARGIN)
                    {
                    MarginRecord m = new BottomMarginRecord(r);
                    settings.setBottomMargin(m.getMargin());
                    }
                else if (type == Type.HORIZONTALPAGEBREAKS)
                    {
                    HorizontalPageBreaksRecord dr = null;

                    if (workbookBof.isBiff8())
                        {
                        dr = new HorizontalPageBreaksRecord(r);
                        }
                    else
                        {
                        dr = new HorizontalPageBreaksRecord
                          (r, HorizontalPageBreaksRecord.biff7);
                        }
                    rowBreaks = dr.getRowBreaks();
                    }
                else if (type == Type.VERTICALPAGEBREAKS)
                    {
                    VerticalPageBreaksRecord dr = null;

                    if (workbookBof.isBiff8())
                        {
                        dr = new VerticalPageBreaksRecord(r);
                        }
                    else
                        {
                        dr = new VerticalPageBreaksRecord
                          (r, VerticalPageBreaksRecord.biff7);
                        }
                    columnBreaks = dr.getColumnBreaks();
                    }
                else if (type == Type.PLS)
                    {
                    plsRecord = new PLSRecord(r);

                    // Check for Continue records
                    while (excelFile.peek().getType() == Type.CONTINUE)
                        {
                        r.addContinueRecord(excelFile.next());
                        }
                    }
                else if (type == Type.DVAL)
                    {
                    if (!workbookSettings.getCellValidationDisabled())
                        {
                        DataValidityListRecord dvlr = new DataValidityListRecord(r);
                        if (dvlr.getObjectId() == DataValidation.DEFAULT_OBJECT_ID)
                            {
                            if (msoRecord != null && objRecord == null)
                                {
                                // there is a drop down associated with this data validation
                                if (drawingData == null)
                                    drawingData = new DrawingData();

                                Drawing2 d2 = new Drawing2(msoRecord, drawingData,
                                                           workbook.getDrawingGroup());
                                drawings.Add(d2);
                                msoRecord = null;

                                dataValidation = new DataValidation(dvlr);
                                }
                            else
                                {
                                // no drop down
                                dataValidation = new DataValidation(dvlr);
                                }
                            }
                        else if (objectIds.Contains(dvlr.getObjectId()))
                            dataValidation = new DataValidation(dvlr);
                        else
                            {
                            //logger.warn("object id " + dvlr.getObjectId() + " referenced " +
                            //            " by data validity list record not found - ignoring");
                            }
                        }
                    }
                else if (type == Type.HCENTER)
                    {
                    CentreRecord hr = new CentreRecord(r);
                    settings.setHorizontalCentre(hr.isCentre());
                    }
                else if (type == Type.VCENTER)
                    {
                    CentreRecord vc = new CentreRecord(r);
                    settings.setVerticalCentre(vc.isCentre());
                    }
                else if (type == Type.DV)
                    {
                    if (!workbookSettings.getCellValidationDisabled())
                        {
                        DataValiditySettingsRecord dvsr =
                          new DataValiditySettingsRecord(r,
                                                         workbook,
                                                         workbook,
                                                         workbook.getSettings());
                        if (dataValidation != null)
                            {
                            dataValidation.add(dvsr);
                            addCellValidation(dvsr.getFirstColumn(),
                                              dvsr.getFirstRow(),
                                              dvsr.getLastColumn(),
                                              dvsr.getLastRow(),
                                              dvsr);
                            }
                        else
                            {
                            //logger.warn("cannot add data validity settings");
                            }
                        }
                    }
                else if (type == Type.OBJ)
                    {
                    objRecord = new ObjRecord(r);

                    if (!workbookSettings.getDrawingsDisabled())
                        {
                        // sometimes excel writes out continue records instead of drawing
                        // records, so forcibly hack the stashed continue record into
                        // a drawing record
                        if (msoRecord == null && continueRecord != null)
                            {
                            //logger.warn("Cannot find drawing record - using continue record");
                            msoRecord = new MsoDrawingRecord(continueRecord.getRecord());
                            continueRecord = null;
                            }
                        handleobjectRecord(objRecord, msoRecord, comments);
                        objectIds.Add((objRecord.getObjectId()));
                        }

                    // Save chart handling until the chart BOF record appears
                    if (objRecord.getType() != ObjRecord.CHART)
                        {
                        objRecord = null;
                        msoRecord = null;
                        }
                    }
                else if (type == Type.MSODRAWING)
                    {
                    if (!workbookSettings.getDrawingsDisabled())
                        {
                        if (msoRecord != null)
                            {
                            // For form controls, a rogue MSODRAWING record can crop up
                            // after the main one.  Add these into the drawing data
                            drawingData.addRawData(msoRecord.getData());
                            }
                        msoRecord = new MsoDrawingRecord(r);

                        if (firstMsoRecord)
                            {
                            msoRecord.setFirst();
                            firstMsoRecord = false;
                            }
                        }
                    }
                else if (type == Type.BUTTONPROPERTYSET)
                    {
                    buttonPropertySet = new ButtonPropertySetRecord(r);
                    }
                else if (type == Type.CALCMODE)
                    {
                    CalcModeRecord cmr = new CalcModeRecord(r);
                    settings.setAutomaticFormulaCalculation(cmr.isAutomatic());
                    }
                else if (type == Type.SAVERECALC)
                    {
                    SaveRecalcRecord cmr = new SaveRecalcRecord(r);
                    settings.setRecalculateFormulasBeforeSave(cmr.getRecalculateOnSave());
                    }
                else if (type == Type.GUTS)
                    {
                    GuttersRecord gr = new GuttersRecord(r);
                    maxRowOutlineLevel =
                      gr.getRowOutlineLevel() > 0 ? gr.getRowOutlineLevel() - 1 : 0;
                    maxColumnOutlineLevel =
                      gr.getColumnOutlineLevel() > 0 ? gr.getRowOutlineLevel() - 1 : 0;
                    }
                else if (type == Type.BOF)
                    {
                    BOFRecord br = new BOFRecord(r);
                    Assert.verify(!br.isWorksheet());

                    int startpos = excelFile.getPos() - r.getLength() - 4;

                    // Skip to the end of the nested bof
                    // Thanks to Rohit for spotting this
                    Record r2 = excelFile.next();
                    while (r2.getCode() != Type.EOF.value)
                        r2 = excelFile.next();

                    if (br.isChart())
                        {
                        if (!workbook.getWorkbookBof().isBiff8())
                            {
                            //logger.warn("only biff8 charts are supported");
                            }
                        else
                            {
                            if (drawingData == null)
                                drawingData = new DrawingData();

                            if (!workbookSettings.getDrawingsDisabled())
                                {
                                Chart chart = new Chart(msoRecord, objRecord, drawingData,
                                                        startpos, excelFile.getPos(),
                                                        excelFile, workbookSettings);
                                charts.Add(chart);

                                if (workbook.getDrawingGroup() != null)
                                    workbook.getDrawingGroup().add(chart);
                                }
                            }

                        // Reset the drawing records
                        msoRecord = null;
                        objRecord = null;
                        }

                    // If this worksheet is just a chart, then the EOF reached
                    // represents the end of the sheet as well as the end of the chart
                    if (sheetBof.isChart())
                        cont = false;
                    }
                else if (type == Type.EOF)
                    cont = false;
                }

            // Restore the file to its accurate position
            excelFile.restorePos();

            // Add any out of bounds cells
            if (outOfBoundsCells.Count > 0)
                handleOutOfBoundsCells();

            // Add all the shared formulas to the sheet as individual formulas
            foreach (SharedFormulaRecord sfr in sharedFormulas)
                {
                Cell[] sfnr = sfr.getFormulas(formattingRecords, nineteenFour);

                for (int sf = 0; sf < sfnr.Length; sf++)
                    addCell(sfnr[sf]);
                }

            // If the last base shared formula wasn't added to the sheet, then
            // revert it to an ordinary formula and add it
            if (!sharedFormulaAdded && sharedFormula != null)
                addCell(revertSharedFormula(sharedFormula));

            // If there is a stray msoDrawing record, then flag to the drawing group
            // that one has been omitted
            if (msoRecord != null && workbook.getDrawingGroup() != null)
                workbook.getDrawingGroup().setDrawingsOmitted(msoRecord, objRecord);

            // Check that the comments hash is empty
            if (comments.Count != 0)
                {
                //logger.warn("Not all comments have a corresponding Note record");
                }
        }
Esempio n. 8
0
 /// <summary>
 /// This must be called to Setup the internal work book references whenever
 /// a RichTextString Is Added to a cell
 /// </summary>
 /// <param name="book">The workbook.</param>
 /// <param name="record">The record.</param>
 public void SetWorkbookReferences(Workbook book, LabelSSTRecord record)
 {
     this.book   = book;
     this.record = record;
 }