从Java中的Excel读取单元格数据时,返回的是十进制值。

huangapple go评论66阅读模式
英文:

When reading cell data from excel in java it returns decimal value

问题

我正在尝试从Excel文件中读取数值。字段中的值为7430031259,我使用了以下代码
item_code = cell.toString();
来获取该值,但输出结果却是7.430031259E9
有什么解决办法吗?

英文:

I am trying to read values from the excel file. The value in field is 7430031259, I used the code
item_code = cell.toString(); to get the value, but the output is like this 7.430031259E9.
How to solve this?

答案1

得分: 3

获取Apache POI中单元格值的最佳可能性是使用专为此目的设计的方法(toString()不是)。

这意味着如果您期望直接获得数字单元格的值,您应该直接接收它:

double numericValue = cell.getNumericCellValue();

应该可以实现您想要的效果。

有关更多有用的方法,请查阅XSSFCell的JavaDocs

如果您仍然希望在科学/指数表示法中使用cell.toString()与表示为数字值的String表示形式,您可以使用BigDecimal作为中间结果:

BigDecimal bdItemCode = new BigDecimal(cell.toString());
// 获取其double值
double dblItemCode = bdItemCode.doubleValue();
// 或获取普通表示法的字符串
String strItemCode = bdItemCode.toPlainString();
英文:

The best possibility of getting a cell value in apache poi is to use the methods that are meant for this purpose (toString() of a cell isn't).

That means you should directly receive a numeric cell value if you are expecting it:

double numericValue = cell.getNumericCellValue();

should do what you are trying to achieve.

Check the the JavaDocs of XSSFCell for more useful methods.

For the case you still want to use cell.toString() with a String representation of a numeric value in scientific / exponential notation, you can use a BigDecimal as an intermediate result:

BigDecimal bdItemCode = new BigDecimal(cell.toString());
// get the double value of it
double dblItemCode = bdItemCode.doubleValue();
// or get a String in plain notation
String strItemCode = bdItemCode.toPlainString();

huangapple
  • 本文由 发表于 2020年8月17日 20:11:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63450558.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定