![](/img/trans.png)
[英]When reading cell data from excel in java it returns decimal value
[英]How to extract only the integer value instead of the Double value when reading data from Excel using Java
我需要一个代码,该代码应该从Excel以int值而不是String值获取数据。
它显示的是值2.0
,该值最初保存在工作表中为2`。
尝试1:
HSSFCell number = sheet.getRow(1).getCell(26);
Integer result =Integer.valueOf(number);
System.out.println(result);
尝试2:
int L = Integer.parseInt(getRow(1).getCell(26));
Error:The method parseInt(String) in the type Integer is not applicable for the arguments (Cell)
码:
System.out.println(+sheet.getRow(1).getCell(26));
输出:2.0
预期产量:2
我建议采用以下方式,不要尝试将HSSFCell
转换为Integer
/ int
:
// get the cell as object (this is NOT a number, instead, it contains one)
HSSFCell numberCell = sheet.getRow(1).getCell(26);
// get the cell value as double (there is no direct integer version)
double cellValue = numberCell.getNumericCellValue();
// cast the value to an int
int number = (int) cellValue;
一旦从excel读取Double值而不是将其显示为2.0即可将其打印为2 ,则可以使用DecimalFormat 类方法,并且可以使用以下解决方案:
HSSFCell number = sheet.getRow(1).getCell(26);
Double result =Double.valueOf(number);
DecimalFormat format = new DecimalFormat();
format.setDecimalSeparatorAlwaysShown(false);
System.out.println(format.format(result));
如果您的用例是打印String格式,则可以添加String.valueOf()
并且可以使用以下解决方案:
HSSFCell number = sheet.getRow(1).getCell(26);
Double result =Double.valueOf(number);
DecimalFormat format = new DecimalFormat();
format.setDecimalSeparatorAlwaysShown(false);
System.out.println(String.valueOf(format.format(result)));
你可以试试
int L = Integer.parseInt(getRow(1).getCell(26).getStringCellValue());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.