Search in sources :

Example 6 with NoteRecord

use of org.apache.poi.hssf.record.NoteRecord 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 7 with NoteRecord

use of org.apache.poi.hssf.record.NoteRecord 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)

Example 8 with NoteRecord

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

the class HSSFPatriarch method preSerialize.

/**
     * check if any shapes contain wrong data
     * At now(13.08.2010) check if patriarch contains 2 or more comments with same coordinates
     */
protected void preSerialize() {
    Map<Integer, NoteRecord> tailRecords = _boundAggregate.getTailRecords();
    /**
         * contains coordinates of comments we iterate over
         */
    Set<String> coordinates = new HashSet<String>(tailRecords.size());
    for (NoteRecord rec : tailRecords.values()) {
        String noteRef = new CellReference(rec.getRow(), rec.getColumn()).formatAsString();
        if (coordinates.contains(noteRef)) {
            throw new IllegalStateException("found multiple cell comments for cell " + noteRef);
        } else {
            coordinates.add(noteRef);
        }
    }
}
Also used : NoteRecord(org.apache.poi.hssf.record.NoteRecord) CellReference(org.apache.poi.hssf.util.CellReference) HashSet(java.util.HashSet)

Aggregations

NoteRecord (org.apache.poi.hssf.record.NoteRecord)8 BOFRecord (org.apache.poi.hssf.record.BOFRecord)3 ObjRecord (org.apache.poi.hssf.record.ObjRecord)3 StringRecord (org.apache.poi.hssf.record.StringRecord)3 TextObjectRecord (org.apache.poi.hssf.record.TextObjectRecord)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 FormulaRecord (org.apache.poi.hssf.record.FormulaRecord)2 MulBlankRecord (org.apache.poi.hssf.record.MulBlankRecord)2 NumberRecord (org.apache.poi.hssf.record.NumberRecord)2 RowRecord (org.apache.poi.hssf.record.RowRecord)2 Test (org.junit.Test)2 HashSet (java.util.HashSet)1 DefaultEscherRecordFactory (org.apache.poi.ddf.DefaultEscherRecordFactory)1 EscherContainerRecord (org.apache.poi.ddf.EscherContainerRecord)1 EscherDggRecord (org.apache.poi.ddf.EscherDggRecord)1 MissingRowDummyRecord (org.apache.poi.hssf.eventusermodel.dummyrecord.MissingRowDummyRecord)1 BoolErrRecord (org.apache.poi.hssf.record.BoolErrRecord)1