What is the safe way to get cell value as String
using Apache POI?
For some non-empty cells I get the following exception:
com.monitorjbl.xlsx.exceptions.NotSupportedException: null
at com.monitorjbl.xlsx.impl.StreamingCell.getSheet(StreamingCell.java:330)
at org.apache.poi.ss.usermodel.DataFormatter.isDate1904(DataFormatter.java:313)
at org.apache.poi.ss.usermodel.DataFormatter.getFormat(DataFormatter.java:309)
at org.apache.poi.ss.usermodel.DataFormatter.getFormattedNumberString(DataFormatter.java:868)
at org.apache.poi.ss.usermodel.DataFormatter.formatCellValue(DataFormatter.java:1021)
at org.apache.poi.ss.usermodel.DataFormatter.formatCellValue(DataFormatter.java:971)
at org.apache.poi.ss.usermodel.DataFormatter.formatCellValue(DataFormatter.java:950)
...
I use the code below to extract cell values as text at the moment:
import org.apache.poi.ss.usermodel.*;
private static DataFormatter formatter = new DataFormatter();
private static String formatCellValue(Cell cell) {
return formatter.formatCellValue(cell);
}
The javadoc of the method says:
Returns the formatted value of a cell as a String regardless of the cell type. If the Excel format pattern cannot be parsed then the cell value will be formatted using a default format. When passed a null or blank cell, this method will return an empty String (""). Formulas in formula type cells will not be evaluated.