private void processSheet( Sheet sheet,
List<? extends DataListener> listeners ) {
int maxRows = sheet.getLastRowNum();
CellRangeAddress[] mergedRanges = getMergedCells( sheet );
DataFormatter formatter = new DataFormatter( Locale.ENGLISH );
FormulaEvaluator formulaEvaluator = sheet.getWorkbook().getCreationHelper().createFormulaEvaluator();
for ( int i = 0; i <= maxRows; i++ ) {
Row row = sheet.getRow( i );
int lastCellNum = row != null ? row.getLastCellNum() : 0;
newRow( listeners, i, lastCellNum );
for ( int cellNum = 0; cellNum < lastCellNum; cellNum++ ) {
Cell cell = row.getCell( cellNum );
if ( cell == null ) {
continue;
}
double num = 0;
CellRangeAddress merged = getRangeIfMerged( cell,
mergedRanges );
if ( merged != null ) {
Cell topLeft = sheet.getRow( merged.getFirstRow() ).getCell( merged.getFirstColumn() );
newCell( listeners,
i,
cellNum,
formatter.formatCellValue( topLeft ),
topLeft.getColumnIndex() );
} else {
switch ( cell.getCellType() ) {
case Cell.CELL_TYPE_FORMULA:
String cellValue = null;
try {
CellValue cv = formulaEvaluator.evaluate( cell );
cellValue = getCellValue( cv );
newCell( listeners,
i,
cellNum,
cellValue,
DataListener.NON_MERGED );
} catch ( RuntimeException e ) {
// This is thrown if an external link cannot be resolved, so try the cached value
log.warn( "Cannot resolve externally linked value: " + formatter.formatCellValue( cell ) );
String cachedValue = tryToReadCachedValue( cell );
newCell( listeners,
i,
cellNum,
cachedValue,
DataListener.NON_MERGED );
}
break;
case Cell.CELL_TYPE_NUMERIC:
num = cell.getNumericCellValue();
default:
if ( num - Math.round( num ) != 0 ) {
newCell( listeners,
i,
cellNum,
String.valueOf( num ),
DataListener.NON_MERGED );
} else {
newCell( listeners,
i,
cellNum,
formatter.formatCellValue( cell ),
DataListener.NON_MERGED );
}
}
}
}