Search in sources :

Example 6 with BlankRecord

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

the class TestValueRecordsAggregate method testData.

private static List<Record> testData() {
    List<Record> records = new ArrayList<Record>();
    FormulaRecord formulaRecord = new FormulaRecord();
    BlankRecord blankRecord = new BlankRecord();
    formulaRecord.setRow(1);
    formulaRecord.setColumn((short) 1);
    blankRecord.setRow(2);
    blankRecord.setColumn((short) 2);
    records.add(formulaRecord);
    records.add(blankRecord);
    records.add(new WindowTwoRecord());
    return records;
}
Also used : MulBlankRecord(org.apache.poi.hssf.record.MulBlankRecord) BlankRecord(org.apache.poi.hssf.record.BlankRecord) SharedFormulaRecord(org.apache.poi.hssf.record.SharedFormulaRecord) FormulaRecord(org.apache.poi.hssf.record.FormulaRecord) ArrayList(java.util.ArrayList) Record(org.apache.poi.hssf.record.Record) MulBlankRecord(org.apache.poi.hssf.record.MulBlankRecord) SharedFormulaRecord(org.apache.poi.hssf.record.SharedFormulaRecord) WindowTwoRecord(org.apache.poi.hssf.record.WindowTwoRecord) BlankRecord(org.apache.poi.hssf.record.BlankRecord) FormulaRecord(org.apache.poi.hssf.record.FormulaRecord) WindowTwoRecord(org.apache.poi.hssf.record.WindowTwoRecord)

Example 7 with BlankRecord

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

the class TestValueRecordsAggregate method confirmMulBlank.

private void confirmMulBlank(int expectedTotalBlankCells, int expectedNumberOfMulBlankRecords, int expectedNumberOfSingleBlankRecords) {
    // assumed row ranges set-up by caller:
    final int firstRow = 1;
    final int lastRow = 2;
    final class BlankStats {

        public int countBlankCells;

        public int countMulBlankRecords;

        public int countSingleBlankRecords;
    }
    final BlankStats bs = new BlankStats();
    RecordVisitor rv = new RecordVisitor() {

        @Override
        public void visitRecord(Record r) {
            if (r instanceof MulBlankRecord) {
                MulBlankRecord mbr = (MulBlankRecord) r;
                bs.countMulBlankRecords++;
                bs.countBlankCells += mbr.getNumColumns();
            } else if (r instanceof BlankRecord) {
                bs.countSingleBlankRecords++;
                bs.countBlankCells++;
            }
        }
    };
    for (int rowIx = firstRow; rowIx <= lastRow; rowIx++) {
        if (valueRecord.rowHasCells(rowIx)) {
            valueRecord.visitCellsForRow(rowIx, rv);
        }
    }
    assertEquals(expectedTotalBlankCells, bs.countBlankCells);
    assertEquals(expectedNumberOfMulBlankRecords, bs.countMulBlankRecords);
    assertEquals(expectedNumberOfSingleBlankRecords, bs.countSingleBlankRecords);
}
Also used : MulBlankRecord(org.apache.poi.hssf.record.MulBlankRecord) MulBlankRecord(org.apache.poi.hssf.record.MulBlankRecord) BlankRecord(org.apache.poi.hssf.record.BlankRecord) Record(org.apache.poi.hssf.record.Record) MulBlankRecord(org.apache.poi.hssf.record.MulBlankRecord) SharedFormulaRecord(org.apache.poi.hssf.record.SharedFormulaRecord) WindowTwoRecord(org.apache.poi.hssf.record.WindowTwoRecord) BlankRecord(org.apache.poi.hssf.record.BlankRecord) FormulaRecord(org.apache.poi.hssf.record.FormulaRecord) RecordVisitor(org.apache.poi.hssf.record.aggregates.RecordAggregate.RecordVisitor)

Example 8 with BlankRecord

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

the class TestHSSFRow method testLastAndFirstColumns_bug46654.

@Test
public void testLastAndFirstColumns_bug46654() throws IOException {
    int ROW_IX = 10;
    int COL_IX = 3;
    HSSFWorkbook workbook = new HSSFWorkbook();
    HSSFSheet sheet = workbook.createSheet("Sheet1");
    RowRecord rowRec = new RowRecord(ROW_IX);
    rowRec.setFirstCol((short) 2);
    rowRec.setLastCol((short) 5);
    BlankRecord br = new BlankRecord();
    br.setRow(ROW_IX);
    br.setColumn((short) COL_IX);
    sheet.getSheet().addValueRecord(ROW_IX, br);
    HSSFRow row = new HSSFRow(workbook, sheet, rowRec);
    HSSFCell cell = row.createCellFromRecord(br);
    if (row.getFirstCellNum() == 2 && row.getLastCellNum() == 5) {
        fail("Identified bug 46654a");
    }
    assertEquals(COL_IX, row.getFirstCellNum());
    assertEquals(COL_IX + 1, row.getLastCellNum());
    row.removeCell(cell);
    assertEquals(-1, row.getFirstCellNum());
    assertEquals(-1, row.getLastCellNum());
    workbook.close();
}
Also used : BlankRecord(org.apache.poi.hssf.record.BlankRecord) RowRecord(org.apache.poi.hssf.record.RowRecord) Test(org.junit.Test)

Example 9 with BlankRecord

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

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

the class TestMissingRecordAwareHSSFListener method testMulBlankHandling.

/**
	 * MulBlank records hold multiple blank cells. Check we
	 *  can handle them correctly.
	 */
public void testMulBlankHandling() {
    readRecords("45672.xls");
    // Check that we don't have any MulBlankRecords, but do
    //  have lots of BlankRecords
    Record[] rr = r;
    int eorCount = 0;
    int mbrCount = 0;
    int brCount = 0;
    for (Record record : rr) {
        if (record instanceof MulBlankRecord) {
            mbrCount++;
        }
        if (record instanceof BlankRecord) {
            brCount++;
        }
        if (record instanceof LastCellOfRowDummyRecord) {
            eorCount++;
        }
    }
    if (mbrCount > 0) {
        throw new AssertionFailedError("Identified bug 45672");
    }
    if (brCount < 20) {
        throw new AssertionFailedError("Identified bug 45672");
    }
    if (eorCount != 2) {
        throw new AssertionFailedError("Identified bug 45672");
    }
    assertEquals(2, eorCount);
}
Also used : MulBlankRecord(org.apache.poi.hssf.record.MulBlankRecord) MulBlankRecord(org.apache.poi.hssf.record.MulBlankRecord) BlankRecord(org.apache.poi.hssf.record.BlankRecord) 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) AssertionFailedError(junit.framework.AssertionFailedError)

Aggregations

BlankRecord (org.apache.poi.hssf.record.BlankRecord)15 MulBlankRecord (org.apache.poi.hssf.record.MulBlankRecord)12 Test (org.junit.Test)7 FormulaRecord (org.apache.poi.hssf.record.FormulaRecord)4 RowRecord (org.apache.poi.hssf.record.RowRecord)4 CellValueRecordInterface (org.apache.poi.hssf.record.CellValueRecordInterface)3 LabelSSTRecord (org.apache.poi.hssf.record.LabelSSTRecord)3 NumberRecord (org.apache.poi.hssf.record.NumberRecord)3 Record (org.apache.poi.hssf.record.Record)3 SharedFormulaRecord (org.apache.poi.hssf.record.SharedFormulaRecord)3 WindowTwoRecord (org.apache.poi.hssf.record.WindowTwoRecord)3 AssertionFailedError (junit.framework.AssertionFailedError)2 LastCellOfRowDummyRecord (org.apache.poi.hssf.eventusermodel.dummyrecord.LastCellOfRowDummyRecord)2 MissingCellDummyRecord (org.apache.poi.hssf.eventusermodel.dummyrecord.MissingCellDummyRecord)2 BOFRecord (org.apache.poi.hssf.record.BOFRecord)2 BoolErrRecord (org.apache.poi.hssf.record.BoolErrRecord)2 DimensionsRecord (org.apache.poi.hssf.record.DimensionsRecord)2 StringRecord (org.apache.poi.hssf.record.StringRecord)2 ArrayList (java.util.ArrayList)1 MissingRowDummyRecord (org.apache.poi.hssf.eventusermodel.dummyrecord.MissingRowDummyRecord)1