簡體   English   中英

無法從 excel 單元中獲取 boolean 值

[英]Cannot get boolean value from excel cell

您好,我在嘗試從 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

在 excel 文件中,我剛剛將“TRUE”和“FALSE”寫為第 6 列中的值,但不知何故,它們被視為字符串而不是布爾值

本地化 Excel 應用程序中的 Boolean 值也已本地化。 例如,在我的德語 Excel boolean 值是WAHRFALSCH 如果我使用德語 Excel GUI 將TRUEFALSE放入單元格,那么這不是boolean 值而是文本字符串。

所以在獲取單元格值之前總是需要檢查CellType 或者使用DataFormatter始終獲取與單元格類型無關的字符串值。

兩者都顯示在Busy Developers' Guide to HSSF and XSSF Features->Getting the cell contents中。

此外,您可能會得到錯誤的單元格。 Row.getCell中, int cellnum是從 0 開始的。 A列是cellnum 0。所以cellnum 6 是G列,這是第 7 列而不是第 6 列。 但那是次要的。 主要是需要在獲取單元格值或使用DataFormatter之前檢查CellType

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM