[英]Cannot get boolean value from excel cell
Hello i am getting an error when trying to access a boolean value from an excel file.您好,我在尝试从 excel 文件访问 boolean 值时遇到错误。
final Cell enabledCell = row.getCell(6);
enabledCell.getBooleanCellValue() // this throws an exception
java.lang.IllegalStateException: Cannot get a BOOLEAN value from a STRING cell
In the excel file i have just written "TRUE" and "FALSE" as values in the 6th collumn, but somehow they are perceived as strings and not booleans在 excel 文件中,我刚刚将“TRUE”和“FALSE”写为第 6 列中的值,但不知何故,它们被视为字符串而不是布尔值
Boolean values in localized Excel applications also are localized.本地化 Excel 应用程序中的 Boolean 值也已本地化。 For example in my German Excel boolean values are
WAHR
and FALSCH
.例如,在我的德语 Excel boolean 值是
WAHR
和FALSCH
。 If I put TRUE
or FALSE
into cells using my German Excel GUI, then this are not boolean values but text strings.如果我使用德语 Excel GUI 将
TRUE
或FALSE
放入单元格,那么这不是boolean 值而是文本字符串。
So there is always the need to check CellType
before getting the cell value.所以在获取单元格值之前总是需要检查
CellType
。 Or to use DataFormatter
to always get string values independent of cell type.或者使用
DataFormatter
始终获取与单元格类型无关的字符串值。
Both is shown in Busy Developers' Guide to HSSF and XSSF Features->Getting the cell contents .两者都显示在Busy Developers' Guide to HSSF and XSSF Features->Getting the cell contents中。
Moreover you might get the wrong cell.此外,您可能会得到错误的单元格。 In Row.getCell the
int cellnum
is 0-based.在Row.getCell中,
int cellnum
是从 0 开始的。 Column A
is cellnum
0. So cellnum
6 is column G
, which is the 7th column and not the 6th. A
列是cellnum
0。所以cellnum
6 是G
列,这是第 7 列而不是第 6 列。 But that's secondary.但那是次要的。 Primary is the need to check
CellType
before getting the cell value or to use DataFormatter
.主要是需要在获取单元格值或使用
DataFormatter
之前检查CellType
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.