简体   繁体   English

无法从 excel 单元中获取 boolean 值

[英]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 值是WAHRFALSCH If I put TRUE or FALSE into cells using my German Excel GUI, then this are not boolean values but text strings.如果我使用德语 Excel GUI 将TRUEFALSE放入单元格,那么这不是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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM