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
}
}
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();
}
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());
}
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());
}
Aggregations