将单元格值导出为字符串的安全方法
Safe way to export cell value as String
使用 Apache POI 获取单元格值 String
的安全方法是什么?
对于一些非空单元格,我得到以下异常:
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)
...
我现在使用下面的代码将单元格值提取为文本:
import org.apache.poi.ss.usermodel.*;
private static DataFormatter formatter = new DataFormatter();
private static String formatCellValue(Cell cell) {
return formatter.formatCellValue(cell);
}
该方法的 javadoc 说:
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.
您的手机似乎是 com.monitorjbl.xlsx.impl.StreamingCell
。这与 apache poi
XSSFCell
或 HSSFCell
不同。但是 DataFormatter
需要 apache poi
Cell
才能使用。请参阅 https://github.com/monitorjbl/excel-streaming-reader#usage 了解如何使用此流 reader。
com.monitorjbl.xlsx.impl.StreamingCell 实现 org.apache.poi.ss.usermodel.Cell
。但是对于许多方法来说,实现只是 throw new NotSupportedException()
。所以它只适用于它正确实现的方法。
但从当前版本开始,它实现了 Cell.getSheet
。因此,您的错误导致假设您没有使用 Excel Streaming Reader.
的当前版本
解决方案是将检查添加到上述方法并在 StreamingCell
上调用 getStringCellValue()
,这与在 POI 单元格上调用它相比总是安全的。
private static String formatCellValue(Cell cell) {
if (cell instanceof com.monitorjbl.xlsx.impl.StreamingCell) {
return cell.getStringCellValue();
}
return formatter.formatCellValue(cell);
}
使用 Apache POI 获取单元格值 String
的安全方法是什么?
对于一些非空单元格,我得到以下异常:
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)
...
我现在使用下面的代码将单元格值提取为文本:
import org.apache.poi.ss.usermodel.*;
private static DataFormatter formatter = new DataFormatter();
private static String formatCellValue(Cell cell) {
return formatter.formatCellValue(cell);
}
该方法的 javadoc 说:
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.
您的手机似乎是 com.monitorjbl.xlsx.impl.StreamingCell
。这与 apache poi
XSSFCell
或 HSSFCell
不同。但是 DataFormatter
需要 apache poi
Cell
才能使用。请参阅 https://github.com/monitorjbl/excel-streaming-reader#usage 了解如何使用此流 reader。
com.monitorjbl.xlsx.impl.StreamingCell 实现 org.apache.poi.ss.usermodel.Cell
。但是对于许多方法来说,实现只是 throw new NotSupportedException()
。所以它只适用于它正确实现的方法。
但从当前版本开始,它实现了 Cell.getSheet
。因此,您的错误导致假设您没有使用 Excel Streaming Reader.
解决方案是将检查添加到上述方法并在 StreamingCell
上调用 getStringCellValue()
,这与在 POI 单元格上调用它相比总是安全的。
private static String formatCellValue(Cell cell) {
if (cell instanceof com.monitorjbl.xlsx.impl.StreamingCell) {
return cell.getStringCellValue();
}
return formatter.formatCellValue(cell);
}