Search in sources :

Example 1 with LastCellOfRowDummyRecord

use of org.apache.poi.hssf.eventusermodel.dummyrecord.LastCellOfRowDummyRecord in project poi by apache.

the class TestMissingRecordAwareHSSFListener method testEndOfRow_bug45672.

/**
	 * Make sure that the presence of shared formulas does not cause extra 
	 * end-of-row records.
	 */
public void testEndOfRow_bug45672() {
    readRecords("ex45672.xls");
    Record[] rr = r;
    int eorCount = 0;
    int sfrCount = 0;
    for (Record record : rr) {
        if (record instanceof SharedFormulaRecord) {
            sfrCount++;
        }
        if (record instanceof LastCellOfRowDummyRecord) {
            eorCount++;
        }
    }
    if (eorCount == 2) {
        throw new AssertionFailedError("Identified bug 45672");
    }
    assertEquals(1, eorCount);
    assertEquals(1, sfrCount);
}
Also used : LastCellOfRowDummyRecord(org.apache.poi.hssf.eventusermodel.dummyrecord.LastCellOfRowDummyRecord) SharedFormulaRecord(org.apache.poi.hssf.record.SharedFormulaRecord) Record(org.apache.poi.hssf.record.Record) NumberRecord(org.apache.poi.hssf.record.NumberRecord) StringRecord(org.apache.poi.hssf.record.StringRecord) MissingRowDummyRecord(org.apache.poi.hssf.eventusermodel.dummyrecord.MissingRowDummyRecord) MissingCellDummyRecord(org.apache.poi.hssf.eventusermodel.dummyrecord.MissingCellDummyRecord) LastCellOfRowDummyRecord(org.apache.poi.hssf.eventusermodel.dummyrecord.LastCellOfRowDummyRecord) LabelSSTRecord(org.apache.poi.hssf.record.LabelSSTRecord) MulBlankRecord(org.apache.poi.hssf.record.MulBlankRecord) RowRecord(org.apache.poi.hssf.record.RowRecord) BOFRecord(org.apache.poi.hssf.record.BOFRecord) SharedFormulaRecord(org.apache.poi.hssf.record.SharedFormulaRecord) DimensionsRecord(org.apache.poi.hssf.record.DimensionsRecord) WindowTwoRecord(org.apache.poi.hssf.record.WindowTwoRecord) BlankRecord(org.apache.poi.hssf.record.BlankRecord) FormulaRecord(org.apache.poi.hssf.record.FormulaRecord) AssertionFailedError(junit.framework.AssertionFailedError)

Example 2 with LastCellOfRowDummyRecord

use of org.apache.poi.hssf.eventusermodel.dummyrecord.LastCellOfRowDummyRecord in project poi by apache.

the class TestMissingRecordAwareHSSFListener method testStringRecordHandling.

public void testStringRecordHandling() {
    readRecords("53588.xls");
    Record[] rr = r;
    int missingCount = 0;
    int lastCount = 0;
    for (Record record : rr) {
        if (record instanceof MissingCellDummyRecord) {
            missingCount++;
        }
        if (record instanceof LastCellOfRowDummyRecord) {
            lastCount++;
        }
    }
    assertEquals(1, missingCount);
    assertEquals(1, lastCount);
}
Also used : LastCellOfRowDummyRecord(org.apache.poi.hssf.eventusermodel.dummyrecord.LastCellOfRowDummyRecord) Record(org.apache.poi.hssf.record.Record) NumberRecord(org.apache.poi.hssf.record.NumberRecord) StringRecord(org.apache.poi.hssf.record.StringRecord) MissingRowDummyRecord(org.apache.poi.hssf.eventusermodel.dummyrecord.MissingRowDummyRecord) MissingCellDummyRecord(org.apache.poi.hssf.eventusermodel.dummyrecord.MissingCellDummyRecord) LastCellOfRowDummyRecord(org.apache.poi.hssf.eventusermodel.dummyrecord.LastCellOfRowDummyRecord) LabelSSTRecord(org.apache.poi.hssf.record.LabelSSTRecord) MulBlankRecord(org.apache.poi.hssf.record.MulBlankRecord) RowRecord(org.apache.poi.hssf.record.RowRecord) BOFRecord(org.apache.poi.hssf.record.BOFRecord) SharedFormulaRecord(org.apache.poi.hssf.record.SharedFormulaRecord) DimensionsRecord(org.apache.poi.hssf.record.DimensionsRecord) WindowTwoRecord(org.apache.poi.hssf.record.WindowTwoRecord) BlankRecord(org.apache.poi.hssf.record.BlankRecord) FormulaRecord(org.apache.poi.hssf.record.FormulaRecord) MissingCellDummyRecord(org.apache.poi.hssf.eventusermodel.dummyrecord.MissingCellDummyRecord)

Example 3 with LastCellOfRowDummyRecord

use of org.apache.poi.hssf.eventusermodel.dummyrecord.LastCellOfRowDummyRecord in project jeesuite-libs by vakinge.

the class XLS2CSV method processRecord.

/**
 * Main HSSFListener method, processes events, and outputs the
 *  CSV as the file is processed.
 */
public void processRecord(Record record) {
    // 超过10行空白就不处理了
    if (blankRowNum == 10)
        return;
    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);
                }
                // sheetName
                // System.out.println(orderedBSRs[sheetIndex].getSheetname() + " [" + (sheetIndex+1) + "]:" );
                String sheetname = orderedBSRs[sheetIndex].getSheetname();
                results.add(ExcelValidator.SHEET_NAME_PREFIX + sheetname);
            }
            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 (thisStr != null) {
        if (thisColumn > 0) {
            _resultRowTmp.append(ExcelValidator.FIELD_SPLIT);
        }
        _resultRowTmp.append(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++) {
                _resultRowTmp.append(',');
            }
        }
        // We're onto a new row
        lastColumnNumber = -1;
        // End the row
        if (!ExcelValidator.isBlankCSVRow(_resultRowTmp.toString())) {
            results.add(_resultRowTmp.toString());
        } else {
            blankRowNum = blankRowNum + 1;
        }
        _resultRowTmp.setLength(0);
    }
}
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 4 with LastCellOfRowDummyRecord

use of org.apache.poi.hssf.eventusermodel.dummyrecord.LastCellOfRowDummyRecord in project h2o-2 by h2oai.

the class XlsParser method processRecord.

@Override
public void processRecord(Record record) {
    int curCol = -1;
    double curNum = Double.NaN;
    ValueString curStr = null;
    switch(record.getSid()) {
        case BoundSheetRecord.sid:
        case BOFRecord.sid:
            // we just run together multiple sheets
            break;
        case SSTRecord.sid:
            _sstRecord = (SSTRecord) record;
            break;
        case BlankRecord.sid:
            BlankRecord brec = (BlankRecord) record;
            curCol = brec.getColumn();
            curStr = _str.setTo("");
            break;
        case BoolErrRecord.sid:
            BoolErrRecord berec = (BoolErrRecord) record;
            curCol = berec.getColumn();
            curStr = _str.setTo("");
            break;
        case FormulaRecord.sid:
            FormulaRecord frec = (FormulaRecord) record;
            curCol = frec.getColumn();
            curNum = frec.getValue();
            if (Double.isNaN(curNum)) {
                // Formula result is a string
                // This is stored in the next record
                _outputNextStringRecord = true;
                _nextCol = frec.getColumn();
            }
            break;
        case StringRecord.sid:
            if (_outputNextStringRecord) {
                // String for formula
                StringRecord srec = (StringRecord) record;
                curStr = _str.setTo(srec.getString());
                curCol = _nextCol;
                _outputNextStringRecord = false;
            }
            break;
        case LabelRecord.sid:
            LabelRecord lrec = (LabelRecord) record;
            curCol = lrec.getColumn();
            curStr = _str.setTo(lrec.getValue());
            break;
        case LabelSSTRecord.sid:
            LabelSSTRecord lsrec = (LabelSSTRecord) record;
            if (_sstRecord == null) {
                Log.warn(Sys.EXCEL, "[ExcelParser] Missing SST record");
            } else {
                curCol = lsrec.getColumn();
                curStr = _str.setTo(_sstRecord.getString(lsrec.getSSTIndex()).toString());
            }
            break;
        case NoteRecord.sid:
            Log.warn(Sys.EXCEL, "Warning cell notes are unsupported");
            break;
        case NumberRecord.sid:
            NumberRecord numrec = (NumberRecord) record;
            curCol = numrec.getColumn();
            curNum = numrec.getValue();
            break;
        case RKRecord.sid:
            Log.warn(Sys.EXCEL, "Warning RK records are unsupported");
            break;
        default:
            break;
    }
    // Handle missing column
    if (record instanceof MissingCellDummyRecord) {
        MissingCellDummyRecord mc = (MissingCellDummyRecord) record;
        curCol = mc.getColumn();
        curNum = Double.NaN;
    }
    // Handle end of row
    if (record instanceof LastCellOfRowDummyRecord) {
        if (_firstRow) {
            _firstRow = false;
            String[] arr = new String[_columnNames.size()];
            arr = _columnNames.toArray(arr);
            _dout.setColumnNames(arr);
        } else {
            _dout.newLine();
            curCol = -1;
        }
    }
    if (curCol == -1)
        return;
    if (_firstRow) {
        _columnNames.add(curStr == null ? ("C" + (curCol + 1)) : curStr.toString());
    } else {
        if (curStr == null)
            if (Double.isNaN(curNum))
                _dout.addInvalidCol(curCol);
            else
                _dout.addNumCol(curCol, curNum);
        else
            _dout.addStrCol(curCol, curStr);
    }
}
Also used : LastCellOfRowDummyRecord(org.apache.poi.hssf.eventusermodel.dummyrecord.LastCellOfRowDummyRecord) MissingCellDummyRecord(org.apache.poi.hssf.eventusermodel.dummyrecord.MissingCellDummyRecord)

Example 5 with LastCellOfRowDummyRecord

use of org.apache.poi.hssf.eventusermodel.dummyrecord.LastCellOfRowDummyRecord 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)

Aggregations

LastCellOfRowDummyRecord (org.apache.poi.hssf.eventusermodel.dummyrecord.LastCellOfRowDummyRecord)9 MissingCellDummyRecord (org.apache.poi.hssf.eventusermodel.dummyrecord.MissingCellDummyRecord)9 BOFRecord (org.apache.poi.hssf.record.BOFRecord)8 StringRecord (org.apache.poi.hssf.record.StringRecord)8 BlankRecord (org.apache.poi.hssf.record.BlankRecord)7 FormulaRecord (org.apache.poi.hssf.record.FormulaRecord)7 LabelSSTRecord (org.apache.poi.hssf.record.LabelSSTRecord)7 NumberRecord (org.apache.poi.hssf.record.NumberRecord)7 MissingRowDummyRecord (org.apache.poi.hssf.eventusermodel.dummyrecord.MissingRowDummyRecord)6 MulBlankRecord (org.apache.poi.hssf.record.MulBlankRecord)6 RowRecord (org.apache.poi.hssf.record.RowRecord)6 DimensionsRecord (org.apache.poi.hssf.record.DimensionsRecord)5 Record (org.apache.poi.hssf.record.Record)5 SharedFormulaRecord (org.apache.poi.hssf.record.SharedFormulaRecord)5 WindowTwoRecord (org.apache.poi.hssf.record.WindowTwoRecord)5 NoteRecord (org.apache.poi.hssf.record.NoteRecord)3 AssertionFailedError (junit.framework.AssertionFailedError)2 BoolErrRecord (org.apache.poi.hssf.record.BoolErrRecord)2 LabelRecord (org.apache.poi.hssf.record.LabelRecord)2 RKRecord (org.apache.poi.hssf.record.RKRecord)2