Search in sources :

Example 6 with BOFRecord

use of org.apache.poi.hssf.record.BOFRecord in project poi by apache.

the class TestEventRecordFactory method testCreateRecord.

/**
     * tests that the create record function returns a properly 
     * constructed record in the simple case.
     */
public void testCreateRecord() {
    BOFRecord bof = new BOFRecord();
    bof.setBuild((short) 0);
    bof.setBuildYear((short) 1999);
    bof.setRequiredVersion(123);
    bof.setType(BOFRecord.TYPE_WORKBOOK);
    bof.setVersion((short) 0x06);
    bof.setHistoryBitMask(BOFRecord.HISTORY_MASK);
    byte[] bytes = bof.serialize();
    Record[] records = RecordFactory.createRecord(TestcaseRecordInputStream.create(bytes));
    assertTrue("record.length must be 1, was =" + records.length, records.length == 1);
    assertTrue("record is the same", compareRec(bof, records[0]));
}
Also used : Record(org.apache.poi.hssf.record.Record) BOFRecord(org.apache.poi.hssf.record.BOFRecord) EOFRecord(org.apache.poi.hssf.record.EOFRecord) UnknownRecord(org.apache.poi.hssf.record.UnknownRecord) ContinueRecord(org.apache.poi.hssf.record.ContinueRecord) BOFRecord(org.apache.poi.hssf.record.BOFRecord)

Example 7 with BOFRecord

use of org.apache.poi.hssf.record.BOFRecord in project poi by apache.

the class InternalWorkbook method createBOF.

private static BOFRecord createBOF() {
    BOFRecord retval = new BOFRecord();
    retval.setVersion((short) 0x600);
    retval.setType(BOFRecord.TYPE_WORKBOOK);
    retval.setBuild((short) 0x10d3);
    retval.setBuildYear((short) 1996);
    // was c1 before verify
    retval.setHistoryBitMask(0x41);
    retval.setRequiredVersion(0x6);
    return retval;
}
Also used : BOFRecord(org.apache.poi.hssf.record.BOFRecord)

Example 8 with BOFRecord

use of org.apache.poi.hssf.record.BOFRecord in project poi by apache.

the class InternalSheet method visitContainedRecords.

public void visitContainedRecords(RecordVisitor rv, int offset) {
    PositionTrackingVisitor ptv = new PositionTrackingVisitor(rv, offset);
    boolean haveSerializedIndex = false;
    for (int k = 0; k < _records.size(); k++) {
        RecordBase record = _records.get(k);
        if (record instanceof RecordAggregate) {
            RecordAggregate agg = (RecordAggregate) record;
            agg.visitContainedRecords(ptv);
        } else {
            ptv.visitRecord((Record) record);
        }
        // If the BOF record was just serialized then add the IndexRecord
        if (record instanceof BOFRecord) {
            if (!haveSerializedIndex) {
                haveSerializedIndex = true;
                //  and one shouldn't go in after that!
                if (_isUncalced) {
                    ptv.visitRecord(new UncalcedRecord());
                }
                //remove this guard. So be safe it is left here.
                if (_rowsAggregate != null) {
                    // find forward distance to first RowRecord
                    int initRecsSize = getSizeOfInitialSheetRecords(k);
                    int currentPos = ptv.getPosition();
                    ptv.visitRecord(_rowsAggregate.createIndexRecord(currentPos, initRecsSize));
                }
            }
        }
    }
}
Also used : PositionTrackingVisitor(org.apache.poi.hssf.record.aggregates.RecordAggregate.PositionTrackingVisitor) UncalcedRecord(org.apache.poi.hssf.record.UncalcedRecord) RecordBase(org.apache.poi.hssf.record.RecordBase) CustomViewSettingsRecordAggregate(org.apache.poi.hssf.record.aggregates.CustomViewSettingsRecordAggregate) RecordAggregate(org.apache.poi.hssf.record.aggregates.RecordAggregate) ChartSubstreamRecordAggregate(org.apache.poi.hssf.record.aggregates.ChartSubstreamRecordAggregate) BOFRecord(org.apache.poi.hssf.record.BOFRecord)

Example 9 with BOFRecord

use of org.apache.poi.hssf.record.BOFRecord in project poi by apache.

the class XLS2CSVmra method processRecord.

/**
	 * Main HSSFListener method, processes events, and outputs the
	 *  CSV as the file is processed.
	 */
@Override
public void processRecord(Record record) {
    int thisRow = -1;
    int thisColumn = -1;
    String thisStr = null;
    switch(record.getSid()) {
        case BoundSheetRecord.sid:
            boundSheetRecords.add((BoundSheetRecord) record);
            break;
        case BOFRecord.sid:
            BOFRecord br = (BOFRecord) record;
            if (br.getType() == 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
                sheetIndex++;
                if (orderedBSRs == null) {
                    orderedBSRs = BoundSheetRecord.orderByBofPosition(boundSheetRecords);
                }
                output.println();
                output.println(orderedBSRs[sheetIndex].getSheetname() + " [" + (sheetIndex + 1) + "]:");
            }
            break;
        case SSTRecord.sid:
            sstRecord = (SSTRecord) record;
            break;
        case BlankRecord.sid:
            BlankRecord brec = (BlankRecord) record;
            thisRow = brec.getRow();
            thisColumn = brec.getColumn();
            thisStr = "";
            break;
        case BoolErrRecord.sid:
            BoolErrRecord berec = (BoolErrRecord) record;
            thisRow = berec.getRow();
            thisColumn = berec.getColumn();
            thisStr = "";
            break;
        case FormulaRecord.sid:
            FormulaRecord frec = (FormulaRecord) record;
            thisRow = frec.getRow();
            thisColumn = frec.getColumn();
            if (outputFormulaValues) {
                if (Double.isNaN(frec.getValue())) {
                    // Formula result is a string
                    // This is stored in the next record
                    outputNextStringRecord = true;
                    nextRow = frec.getRow();
                    nextColumn = frec.getColumn();
                } else {
                    thisStr = formatListener.formatNumberDateCell(frec);
                }
            } else {
                thisStr = '"' + HSSFFormulaParser.toFormulaString(stubWorkbook, frec.getParsedExpression()) + '"';
            }
            break;
        case StringRecord.sid:
            if (outputNextStringRecord) {
                // String for formula
                StringRecord srec = (StringRecord) record;
                thisStr = srec.getString();
                thisRow = nextRow;
                thisColumn = nextColumn;
                outputNextStringRecord = false;
            }
            break;
        case LabelRecord.sid:
            LabelRecord lrec = (LabelRecord) record;
            thisRow = lrec.getRow();
            thisColumn = lrec.getColumn();
            thisStr = '"' + lrec.getValue() + '"';
            break;
        case LabelSSTRecord.sid:
            LabelSSTRecord lsrec = (LabelSSTRecord) record;
            thisRow = lsrec.getRow();
            thisColumn = lsrec.getColumn();
            if (sstRecord == null) {
                thisStr = '"' + "(No SST Record, can't identify string)" + '"';
            } else {
                thisStr = '"' + sstRecord.getString(lsrec.getSSTIndex()).toString() + '"';
            }
            break;
        case NoteRecord.sid:
            NoteRecord nrec = (NoteRecord) record;
            thisRow = nrec.getRow();
            thisColumn = nrec.getColumn();
            // TODO: Find object to match nrec.getShapeId()
            thisStr = '"' + "(TODO)" + '"';
            break;
        case NumberRecord.sid:
            NumberRecord numrec = (NumberRecord) record;
            thisRow = numrec.getRow();
            thisColumn = numrec.getColumn();
            // Format
            thisStr = formatListener.formatNumberDateCell(numrec);
            break;
        case RKRecord.sid:
            RKRecord rkrec = (RKRecord) record;
            thisRow = rkrec.getRow();
            thisColumn = rkrec.getColumn();
            thisStr = '"' + "(TODO)" + '"';
            break;
        default:
            break;
    }
    // Handle new row
    if (thisRow != -1 && thisRow != lastRowNumber) {
        lastColumnNumber = -1;
    }
    // Handle missing column
    if (record instanceof MissingCellDummyRecord) {
        MissingCellDummyRecord mc = (MissingCellDummyRecord) record;
        thisRow = mc.getRow();
        thisColumn = mc.getColumn();
        thisStr = "";
    }
    // If we got something to print out, do so
    if (thisStr != null) {
        if (thisColumn > 0) {
            output.print(',');
        }
        output.print(thisStr);
    }
    // Update column and row count
    if (thisRow > -1)
        lastRowNumber = thisRow;
    if (thisColumn > -1)
        lastColumnNumber = thisColumn;
    // Handle end of row
    if (record instanceof LastCellOfRowDummyRecord) {
        // Print out any missing commas if needed
        if (minColumns > 0) {
            // Columns are 0 based
            if (lastColumnNumber == -1) {
                lastColumnNumber = 0;
            }
            for (int i = lastColumnNumber; i < (minColumns); i++) {
                output.print(',');
            }
        }
        // We're onto a new row
        lastColumnNumber = -1;
        // End the row
        output.println();
    }
}
Also used : BlankRecord(org.apache.poi.hssf.record.BlankRecord) BOFRecord(org.apache.poi.hssf.record.BOFRecord) LabelRecord(org.apache.poi.hssf.record.LabelRecord) LabelSSTRecord(org.apache.poi.hssf.record.LabelSSTRecord) StringRecord(org.apache.poi.hssf.record.StringRecord) BoolErrRecord(org.apache.poi.hssf.record.BoolErrRecord) FormulaRecord(org.apache.poi.hssf.record.FormulaRecord) RKRecord(org.apache.poi.hssf.record.RKRecord) LastCellOfRowDummyRecord(org.apache.poi.hssf.eventusermodel.dummyrecord.LastCellOfRowDummyRecord) NoteRecord(org.apache.poi.hssf.record.NoteRecord) NumberRecord(org.apache.poi.hssf.record.NumberRecord) MissingCellDummyRecord(org.apache.poi.hssf.eventusermodel.dummyrecord.MissingCellDummyRecord)

Example 10 with BOFRecord

use of org.apache.poi.hssf.record.BOFRecord in project poi by apache.

the class MissingRecordAwareHSSFListener method processRecord.

public void processRecord(Record record) {
    int thisRow;
    int thisColumn;
    CellValueRecordInterface[] expandedRecords = null;
    if (record instanceof CellValueRecordInterface) {
        CellValueRecordInterface valueRec = (CellValueRecordInterface) record;
        thisRow = valueRec.getRow();
        thisColumn = valueRec.getColumn();
    } else {
        if (record instanceof StringRecord) {
            //it contains only cashed result of the previous FormulaRecord evaluation
            childListener.processRecord(record);
            return;
        }
        thisRow = -1;
        thisColumn = -1;
        switch(record.getSid()) {
            // the workbook
            case BOFRecord.sid:
                BOFRecord bof = (BOFRecord) record;
                if (bof.getType() == BOFRecord.TYPE_WORKBOOK || bof.getType() == BOFRecord.TYPE_WORKSHEET) {
                    // Reset the row and column counts - new workbook / worksheet
                    resetCounts();
                }
                break;
            case RowRecord.sid:
                RowRecord rowrec = (RowRecord) record;
                // If there's a jump in rows, fire off missing row records
                if (lastRowRow + 1 < rowrec.getRowNumber()) {
                    for (int i = (lastRowRow + 1); i < rowrec.getRowNumber(); i++) {
                        MissingRowDummyRecord dr = new MissingRowDummyRecord(i);
                        childListener.processRecord(dr);
                    }
                }
                // Record this as the last row we saw
                lastRowRow = rowrec.getRowNumber();
                lastCellColumn = -1;
                break;
            case SharedFormulaRecord.sid:
                // SharedFormulaRecord occurs after the first FormulaRecord of the cell range.
                // There are probably (but not always) more cell records after this
                // - so don't fire off the LastCellOfRowDummyRecord yet
                childListener.processRecord(record);
                return;
            case MulBlankRecord.sid:
                // These appear in the middle of the cell records, to
                //  specify that the next bunch are empty but styled
                // Expand this out into multiple blank cells
                MulBlankRecord mbr = (MulBlankRecord) record;
                expandedRecords = RecordFactory.convertBlankRecords(mbr);
                break;
            case MulRKRecord.sid:
                // This is multiple consecutive number cells in one record
                // Exand this out into multiple regular number cells
                MulRKRecord mrk = (MulRKRecord) record;
                expandedRecords = RecordFactory.convertRKRecords(mrk);
                break;
            case NoteRecord.sid:
                NoteRecord nrec = (NoteRecord) record;
                thisRow = nrec.getRow();
                thisColumn = nrec.getColumn();
                break;
            default:
                break;
        }
    }
    // First part of expanded record handling
    if (expandedRecords != null && expandedRecords.length > 0) {
        thisRow = expandedRecords[0].getRow();
        thisColumn = expandedRecords[0].getColumn();
    }
    //  dummy end-of-row records
    if (thisRow != lastCellRow && thisRow > 0) {
        if (lastCellRow == -1)
            lastCellRow = 0;
        for (int i = lastCellRow; i < thisRow; i++) {
            int cols = -1;
            if (i == lastCellRow) {
                cols = lastCellColumn;
            }
            childListener.processRecord(new LastCellOfRowDummyRecord(i, cols));
        }
    }
    // final dummy end-of-row record
    if (lastCellRow != -1 && lastCellColumn != -1 && thisRow == -1) {
        childListener.processRecord(new LastCellOfRowDummyRecord(lastCellRow, lastCellColumn));
        lastCellRow = -1;
        lastCellColumn = -1;
    }
    //  the column counter
    if (thisRow != lastCellRow) {
        lastCellColumn = -1;
    }
    //  the dummy cell records
    if (lastCellColumn != thisColumn - 1) {
        for (int i = lastCellColumn + 1; i < thisColumn; i++) {
            childListener.processRecord(new MissingCellDummyRecord(thisRow, i));
        }
    }
    // Next part of expanded record handling
    if (expandedRecords != null && expandedRecords.length > 0) {
        thisColumn = expandedRecords[expandedRecords.length - 1].getColumn();
    }
    // Update cell and row counts as needed
    if (thisColumn != -1) {
        lastCellColumn = thisColumn;
        lastCellRow = thisRow;
    }
    // Pass along the record(s)
    if (expandedRecords != null && expandedRecords.length > 0) {
        for (CellValueRecordInterface r : expandedRecords) {
            childListener.processRecord((Record) r);
        }
    } else {
        childListener.processRecord(record);
    }
}
Also used : MissingRowDummyRecord(org.apache.poi.hssf.eventusermodel.dummyrecord.MissingRowDummyRecord) MulBlankRecord(org.apache.poi.hssf.record.MulBlankRecord) CellValueRecordInterface(org.apache.poi.hssf.record.CellValueRecordInterface) RowRecord(org.apache.poi.hssf.record.RowRecord) LastCellOfRowDummyRecord(org.apache.poi.hssf.eventusermodel.dummyrecord.LastCellOfRowDummyRecord) MulRKRecord(org.apache.poi.hssf.record.MulRKRecord) NoteRecord(org.apache.poi.hssf.record.NoteRecord) BOFRecord(org.apache.poi.hssf.record.BOFRecord) MissingCellDummyRecord(org.apache.poi.hssf.eventusermodel.dummyrecord.MissingCellDummyRecord) StringRecord(org.apache.poi.hssf.record.StringRecord)

Aggregations

BOFRecord (org.apache.poi.hssf.record.BOFRecord)13 EOFRecord (org.apache.poi.hssf.record.EOFRecord)6 Record (org.apache.poi.hssf.record.Record)4 NoteRecord (org.apache.poi.hssf.record.NoteRecord)3 StringRecord (org.apache.poi.hssf.record.StringRecord)3 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 LastCellOfRowDummyRecord (org.apache.poi.hssf.eventusermodel.dummyrecord.LastCellOfRowDummyRecord)2 MissingCellDummyRecord (org.apache.poi.hssf.eventusermodel.dummyrecord.MissingCellDummyRecord)2 BlankRecord (org.apache.poi.hssf.record.BlankRecord)2 ContinueRecord (org.apache.poi.hssf.record.ContinueRecord)2 FormulaRecord (org.apache.poi.hssf.record.FormulaRecord)2 MulBlankRecord (org.apache.poi.hssf.record.MulBlankRecord)2 NumberRecord (org.apache.poi.hssf.record.NumberRecord)2 RecordBase (org.apache.poi.hssf.record.RecordBase)2 RowRecord (org.apache.poi.hssf.record.RowRecord)2 UncalcedRecord (org.apache.poi.hssf.record.UncalcedRecord)2 UnknownRecord (org.apache.poi.hssf.record.UnknownRecord)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 Calendar (java.util.Calendar)1