Search in sources :

Example 6 with NumberRecord

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

the class TestFormatTrackingHSSFListener method testTurnToString.

/**
	 * Ensure that all number and formula records can be
	 *  turned into strings without problems.
	 * For now, we're just looking to get text back, no
	 *  exceptions thrown, but in future we might also
	 *  want to check the exact strings!
	 */
@Test
public void testTurnToString() throws Exception {
    String[] files = new String[] { "45365.xls", "45365-2.xls", "MissingBits.xls" };
    for (String file : files) {
        processFile(file);
        // Check we found our formats
        assertTrue(listener.getNumberOfCustomFormats() > 5);
        assertTrue(listener.getNumberOfExtendedFormats() > 5);
        //  cells into strings without error
        for (Record r : mockListen._records) {
            CellValueRecordInterface cvr = null;
            if (r instanceof NumberRecord) {
                cvr = (CellValueRecordInterface) r;
            }
            if (r instanceof FormulaRecord) {
                cvr = (CellValueRecordInterface) r;
            }
            if (cvr != null) {
                // Should always give us a string 
                String s = listener.formatNumberDateCell(cvr);
                assertNotNull(s);
                assertTrue(s.length() > 0);
            }
        }
    // TODO - test some specific format strings
    }
}
Also used : FormulaRecord(org.apache.poi.hssf.record.FormulaRecord) CellValueRecordInterface(org.apache.poi.hssf.record.CellValueRecordInterface) Record(org.apache.poi.hssf.record.Record) NumberRecord(org.apache.poi.hssf.record.NumberRecord) FormulaRecord(org.apache.poi.hssf.record.FormulaRecord) NumberRecord(org.apache.poi.hssf.record.NumberRecord) Test(org.junit.Test)

Example 7 with NumberRecord

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

the class HSSFCell method getNumericCellValue.

/**
     * Get the value of the cell as a number.
     * For strings we throw an exception.
     * For blank cells we return a 0.
     * See {@link HSSFDataFormatter} for turning this
     *  number into a string similar to that which
     *  Excel would render this number as.
     */
public double getNumericCellValue() {
    switch(_cellType) {
        case BLANK:
            return 0.0;
        case NUMERIC:
            return ((NumberRecord) _record).getValue();
        default:
            throw typeMismatch(CellType.NUMERIC, _cellType, false);
        case FORMULA:
            break;
    }
    FormulaRecord fr = ((FormulaRecordAggregate) _record).getFormulaRecord();
    checkFormulaCachedValueType(CellType.NUMERIC, fr);
    return fr.getValue();
}
Also used : FormulaRecord(org.apache.poi.hssf.record.FormulaRecord) FormulaRecordAggregate(org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate) NumberRecord(org.apache.poi.hssf.record.NumberRecord)

Example 8 with NumberRecord

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

the class TestRowBlocksReader method testAbnormalPivotTableRecords_bug46280.

@Test
public void testAbnormalPivotTableRecords_bug46280() {
    int SXVIEW_SID = ViewDefinitionRecord.sid;
    Record[] inRecs = { new RowRecord(0), new NumberRecord(), // normally MSODRAWING(0x00EC) would come here before SXVIEW
    new UnknownRecord(SXVIEW_SID, "dummydata (SXVIEW: View Definition)".getBytes(LocaleUtil.CHARSET_1252)), new WindowTwoRecord() };
    RecordStream rs = new RecordStream(Arrays.asList(inRecs), 0);
    RowBlocksReader rbr = new RowBlocksReader(rs);
    if (rs.peekNextClass() == WindowTwoRecord.class) {
        // Should have stopped at the SXVIEW record
        fail("Identified bug 46280b");
    }
    RecordStream rbStream = rbr.getPlainRecordStream();
    assertEquals(inRecs[0], rbStream.getNext());
    assertEquals(inRecs[1], rbStream.getNext());
    assertFalse(rbStream.hasNext());
    assertTrue(rs.hasNext());
    assertEquals(inRecs[2], rs.getNext());
    assertEquals(inRecs[3], rs.getNext());
}
Also used : RowRecord(org.apache.poi.hssf.record.RowRecord) UnknownRecord(org.apache.poi.hssf.record.UnknownRecord) Record(org.apache.poi.hssf.record.Record) NumberRecord(org.apache.poi.hssf.record.NumberRecord) UnknownRecord(org.apache.poi.hssf.record.UnknownRecord) ViewDefinitionRecord(org.apache.poi.hssf.record.pivottable.ViewDefinitionRecord) RowRecord(org.apache.poi.hssf.record.RowRecord) WindowTwoRecord(org.apache.poi.hssf.record.WindowTwoRecord) WindowTwoRecord(org.apache.poi.hssf.record.WindowTwoRecord) NumberRecord(org.apache.poi.hssf.record.NumberRecord) Test(org.junit.Test)

Example 9 with NumberRecord

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

the class TestSheet method testMissingDims.

/**
	 * Some apps seem to write files with missing DIMENSION records.
	 * Excel(2007) tolerates this, so POI should too.
	 */
@Test
public void testMissingDims() {
    int rowIx = 5;
    int colIx = 6;
    NumberRecord nr = new NumberRecord();
    nr.setRow(rowIx);
    nr.setColumn((short) colIx);
    nr.setValue(3.0);
    List<Record> inRecs = new ArrayList<Record>();
    inRecs.add(BOFRecord.createSheetBOF());
    inRecs.add(new RowRecord(rowIx));
    inRecs.add(nr);
    inRecs.add(createWindow2Record());
    inRecs.add(EOFRecord.instance);
    InternalSheet sheet;
    try {
        sheet = createSheet(inRecs);
    } catch (RuntimeException e) {
        if ("DimensionsRecord was not found".equals(e.getMessage())) {
            throw new AssertionFailedError("Identified bug 46206");
        }
        throw e;
    }
    RecordCollector rv = new RecordCollector();
    sheet.visitContainedRecords(rv, rowIx);
    Record[] outRecs = rv.getRecords();
    assertEquals(8, outRecs.length);
    DimensionsRecord dims = (DimensionsRecord) outRecs[5];
    assertEquals(rowIx, dims.getFirstRow());
    assertEquals(rowIx, dims.getLastRow());
    assertEquals(colIx, dims.getFirstCol());
    assertEquals(colIx, dims.getLastCol());
}
Also used : RecordCollector(org.apache.poi.hssf.usermodel.RecordInspector.RecordCollector) RowRecord(org.apache.poi.hssf.record.RowRecord) ArrayList(java.util.ArrayList) StringRecord(org.apache.poi.hssf.record.StringRecord) GutsRecord(org.apache.poi.hssf.record.GutsRecord) MergeCellsRecord(org.apache.poi.hssf.record.MergeCellsRecord) DrawingRecord(org.apache.poi.hssf.record.DrawingRecord) NoteRecord(org.apache.poi.hssf.record.NoteRecord) Record(org.apache.poi.hssf.record.Record) ObjRecord(org.apache.poi.hssf.record.ObjRecord) EOFRecord(org.apache.poi.hssf.record.EOFRecord) RowRecord(org.apache.poi.hssf.record.RowRecord) ColumnInfoRecord(org.apache.poi.hssf.record.ColumnInfoRecord) WindowTwoRecord(org.apache.poi.hssf.record.WindowTwoRecord) EscherDggRecord(org.apache.poi.ddf.EscherDggRecord) TextObjectRecord(org.apache.poi.hssf.record.TextObjectRecord) IndexRecord(org.apache.poi.hssf.record.IndexRecord) BOFRecord(org.apache.poi.hssf.record.BOFRecord) NumberRecord(org.apache.poi.hssf.record.NumberRecord) MulBlankRecord(org.apache.poi.hssf.record.MulBlankRecord) UncalcedRecord(org.apache.poi.hssf.record.UncalcedRecord) DimensionsRecord(org.apache.poi.hssf.record.DimensionsRecord) BlankRecord(org.apache.poi.hssf.record.BlankRecord) FormulaRecord(org.apache.poi.hssf.record.FormulaRecord) DimensionsRecord(org.apache.poi.hssf.record.DimensionsRecord) NumberRecord(org.apache.poi.hssf.record.NumberRecord) AssertionFailedError(junit.framework.AssertionFailedError) Test(org.junit.Test)

Aggregations

NumberRecord (org.apache.poi.hssf.record.NumberRecord)9 FormulaRecord (org.apache.poi.hssf.record.FormulaRecord)6 Record (org.apache.poi.hssf.record.Record)5 Test (org.junit.Test)4 BlankRecord (org.apache.poi.hssf.record.BlankRecord)3 RowRecord (org.apache.poi.hssf.record.RowRecord)3 WindowTwoRecord (org.apache.poi.hssf.record.WindowTwoRecord)3 EncryptedDocumentException (org.apache.poi.EncryptedDocumentException)2 BOFRecord (org.apache.poi.hssf.record.BOFRecord)2 BoolErrRecord (org.apache.poi.hssf.record.BoolErrRecord)2 ContinueRecord (org.apache.poi.hssf.record.ContinueRecord)2 EOFRecord (org.apache.poi.hssf.record.EOFRecord)2 LabelSSTRecord (org.apache.poi.hssf.record.LabelSSTRecord)2 NoteRecord (org.apache.poi.hssf.record.NoteRecord)2 RKRecord (org.apache.poi.hssf.record.RKRecord)2 StringRecord (org.apache.poi.hssf.record.StringRecord)2 UnknownRecord (org.apache.poi.hssf.record.UnknownRecord)2 FormulaRecordAggregate (org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate)2 ArrayList (java.util.ArrayList)1 AssertionFailedError (junit.framework.AssertionFailedError)1