Esempio n. 1
0
        public void TestOrdering()
        {
            BoundSheetRecord bs1 = new BoundSheetRecord("SheetB");
            BoundSheetRecord bs2 = new BoundSheetRecord("SheetC");
            BoundSheetRecord bs3 = new BoundSheetRecord("SheetA");

            bs1.PositionOfBof = (/*setter*/ 11);
            bs2.PositionOfBof = (/*setter*/ 33);
            bs3.PositionOfBof = (/*setter*/ 22);

            List <BoundSheetRecord> l = new List <BoundSheetRecord>();

            l.Add(bs1);
            l.Add(bs2);
            l.Add(bs3);

            BoundSheetRecord[] r = BoundSheetRecord.OrderByBofPosition(l);
            Assert.AreEqual(3, r.Length);
            Assert.AreEqual(bs1, r[0]);
            Assert.AreEqual(bs3, r[1]);
            Assert.AreEqual(bs2, r[2]);
        }
Esempio n. 2
0
        /// <summary>
        /// Main HSSFListener method, processes events, and outputs the
        /// strings as the file is processed
        /// </summary>
        public void ProcessRecord(Record record)
        {
            var    thisRow = -1;
            string thisStr = null;

            switch (record.Sid)
            {
            case BoundSheetRecord.sid:
                _boundSheetRecords.Add(record);
                break;

            case BOFRecord.sid:
                var br = (BOFRecord)record;
                if (br.Type == BOFRecord.TYPE_WORKSHEET)
                {
                    // Create sub workbook if required
                    if (_workbookBuildingListener != null && _stubWorkbook == null)
                    {
                        _stubWorkbook = _workbookBuildingListener.GetStubHSSFWorkbook();
                    }

                    // Output the worksheet name
                    // Works by ordering the BSRs by the location of
                    //  their BOFRecords, and then knowing that we
                    //  process BOFRecords in byte offset order
                    if (_orderedBsRs == null)
                    {
                        _orderedBsRs = BoundSheetRecord.OrderByBofPosition(_boundSheetRecords);
                    }
                }
                break;

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

            case BlankRecord.sid:
                var brec = (BlankRecord)record;
                thisRow = brec.Row;
                thisStr = "";
                break;

            case BoolErrRecord.sid:
                var berec = (BoolErrRecord)record;
                thisRow = berec.Row;
                thisStr = "";
                break;

            case FormulaRecord.sid:
                var frec = (FormulaRecord)record;
                thisRow = frec.Row;
                if (OutputFormulaValues)
                {
                    if (double.IsNaN(frec.Value))
                    {
                        // Formula result is a string
                        // This is stored in the next record
                        _outputNextStringRecord = true;
                        _nextRow = frec.Row;
                    }
                    else
                    {
                        thisStr = _formatListener.FormatNumberDateCell(frec);
                    }
                }
                else
                {
                    thisStr = HSSFFormulaParser.ToFormulaString(_stubWorkbook, frec.ParsedExpression);
                }
                break;

            case StringRecord.sid:
                if (_outputNextStringRecord)
                {
                    // String for formula
                    var srec = (StringRecord)record;
                    thisStr = srec.String;
                    thisRow = _nextRow;
                    _outputNextStringRecord = false;
                }
                break;

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

            case LabelSSTRecord.sid:
                var lsrec = (LabelSSTRecord)record;

                thisRow = lsrec.Row;
                if (_sstRecord == null)
                {
                    thisStr = "(No SST Record, can't identify string)";
                }
                else
                {
                    thisStr = _sstRecord.GetString(lsrec.SSTIndex).ToString();
                }
                break;

            case NoteRecord.sid:
                var nrec = (NoteRecord)record;
                thisRow = nrec.Row;
                thisStr = "";
                break;

            case NumberRecord.sid:
                var numrec = (NumberRecord)record;
                thisRow = numrec.Row;
                // Format
                thisStr = _formatListener.FormatNumberDateCell(numrec);
                break;

            case RKRecord.sid:
                var rkrec = (RKRecord)record;
                thisRow = rkrec.Row;
                thisStr = "";
                break;
            }

            // Handle new row
            if (thisRow != -1 && thisRow != _lastRowNumber)
            {
                _row = new List <string>();
            }

            // Handle missing column
            if (record is MissingCellDummyRecord)
            {
                var mc = (MissingCellDummyRecord)record;
                thisRow = mc.Row;
                thisStr = "";
            }

            // If we got something to print out, do so
            if (thisStr != null)
            {
                _row.Add(thisStr);
            }

            // Update column and row count
            if (thisRow > -1)
            {
                _lastRowNumber = thisRow;
            }

            // Handle end of row
            if (record is LastCellOfRowDummyRecord)
            {
                // We're onto a new row
                Output.Add(_row);
            }
        }