[英]Apache POI xlsx read, cell with # value - error unexpected cell type(5)
你能帮我解决这样的问题吗?
我需要将每个单元格读取为字符串值。 在这种情况下,我使用的是 appache poi lib。 以及用于标准化每个单元格的方法:
String getNormilizedCell(Cell cell){
return new DataFormatter().formatCellValue(cell);}
但是当 In.xlsx 文件时,我遇到了这样的值:
|#N/A|#N/A|...|...|...
我收到错误[Unexpected Cell type (5)] ,我不知道如何处理。 在谷歌我找不到必要的信息。
DataFormatter
类仅处理CELL_TYPE_FORMULA
, CELL_TYPE_NUMERIC
, CELL_TYPE_STRING
, CELL_TYPE_BOOLEAN
和CELL_TYPE_BLANK
。 它不处理CELL_TYPE_ERROR
,即5
。
您必须首先检测错误单元格类型,然后特别处理它,参考错误单元格值代码来解决此问题 :
if (cell.getCellType() == Cell.CELL_TYPE_ERROR) {
byte errorValue = cell.getErrorCellValue();
switch(errorValue) {
case ERROR_DIV_0:
return "#DIV/0!";
case ERROR_NA:
return "#N/A";
case ERROR_NAME:
return "#NAME?";
case ERROR_NULL:
return "#NULL!";
case ERROR_NUM:
return "#NUM!";
case ERROR_REF:
return "#REF!";
case ERROR_VALUE:
return "#VALUE!";
default:
return "Unknown error value: " + errorValue + "!";
}
} else {
return new DataFormatter().formatCellValue(cell);
}
您需要升级Apache POI的副本!
从r1537552开始, DataFormatter现在很乐意为您格式化Error cells。 它将使用FormulaError常量返回Excel显示的错误字符串
正如@Gagravarr所说,DataFormatter现在处理大多数错误(我使用poi-3.11-beta2)。 但是,正如我在评论中所说,一些公式错误仍然可以抛出异常。
例如,当xxx不是真实函数时,在评估类似=xxx()
的公式时,Excel会显示#NAME?
但我们得到一个“不知道如何评估名称'xxx'”运行时异常。
幸运的是,处理起来很简单:
public String readCellValue(Cell cell)
{
switch (cell.getCellType())
{
case Cell.CELL_TYPE_BLANK:
return "(blank)";
case Cell.CELL_TYPE_BOOLEAN:
return String.valueOf(cell.getBooleanCellValue());
case Cell.CELL_TYPE_ERROR:
return String.valueOf(cell.getErrorCellValue());
case Cell.CELL_TYPE_FORMULA:
return readFormattedCellValue(cell);
case Cell.CELL_TYPE_NUMERIC:
return String.valueOf(cell.getNumericCellValue());
case Cell.CELL_TYPE_STRING:
return cell.getStringCellValue();
default:
return "Unknown type!";
}
}
public String readFormattedCellValue(Cell cell)
{
try
{
return formatter.formatCellValue(cell, evaluator);
}
catch (RuntimeException e)
{
return e.getMessage(); // Error from evaluator, for example "Don't know how to evaluate name 'xxx'" if we have =xxx() in cell
}
}
对于记录, formatter
和evaluator
程序的创建方式与转换为CSV示例类似:
try (FileInputStream fis = new FileInputStream(file))
{
// Open the workbook and then create the FormulaEvaluator and
// DataFormatter instances that will be needed to, respectively,
// force evaluation of formulae found in cells and create a
// formatted String encapsulating the cells contents.
workbook = WorkbookFactory.create(fis);
evaluator = workbook.getCreationHelper().createFormulaEvaluator();
formatter = new DataFormatter(true);
}
由于某些未知原因, WorkbookFactory
仅存在于poi-ooml-3.11-beta2.jar中,而不是poi-3.11-beta2.jar中。
I too faced the same error while working on Spring batch insert excel data to DB.
I was using spring-batch-excel version 0.5.0 and poi 3.12
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-excel</artifactId>
<version>0.5.0-SNAPSHOT</version>
</dependency>
Once Updated to spring-batch-excel version 1.0.1 and poi 3.17, It was resolved.
<dependency>
<groupId>com.github.kumarpankaj18</groupId>
<artifactId>spring-batch-excel</artifactId>
<version>1.0.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
注意:即使 poi 5.0.0 也不起作用,我只能使用 3.17
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.