我正在用java读取Excel,在我的单元格中我有一个公式,但我想得到结果 -> 20.000 或 2.154 等,但我得到 ->
IF(F2="买入",+(H2-G2+1)*I2,+(H2-G2+1)I2(-1))
<小时/>switch (cell.getCellType()) {
case Cell.CELL_TYPE_FORMULA:
stringValue = cell.getCellFormula();
break;
...
问题是我无法计算这个公式,因为我读取了Excel单元格然后...h2-g2...我的代码不知道这一点...
我正在使用
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15</version>
</dependency>
如何获取单元格的值?
Cell.CELL_TYPE_FORMULA: ---> IF(F2="Buy",+(H2-G2+1)*I2,+(H2-G2+1)*I2*(-1))
Cell.CELL_TYPE_STRING: ----> IF(F2="Buy",+(H2-G2+1)*I2,+(H2-G2+1)*I2*(-1))
谢谢!!
编辑, 我改变了->
case Cell.CELL_TYPE_FORMULA:
stringValue = String.valueOf(cell.getNumericCellValue());
break;
结果 -> 5.475E7
这个结果很糟糕,我的 Excel 视觉上有54.750.000
最佳答案
您获得的值是正确的,所以您已经差不多了,只是格式不符合您的预期。
尝试:
String stringValue = NumberFormat.getNumberInstance().format(cell.getNumericCellValue());
关于java - 在 getCellFormula 中获取结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52611966/