gpt4 book ai didi

android - 使用 Apache POI 库解析出现在 Excel 工作表中的值

转载 作者:太空狗 更新时间:2023-10-29 16:38:47 26 4
gpt4 key购买 nike

我在使用 poi 库解析 excel 表时遇到问题。其中所有字符串值都是正确的,但电话被转换为十进制格式,就像电话号码在解析后是 9876543210 我得到 9.876543210E10如何在解析时获得正确的电话号码。我的代码是-

while(cellIter.hasNext()){

HSSFCell myCell = (HSSFCell) cellIter.next();
String cellValue = "";

// Check for cell Type
if(myCell.getCellType() == HSSFCell.CELL_TYPE_STRING){
cellValue = myCell.getStringCellValue();
}
else if(myCell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
System.out.println("cell type -- number"+myCell.getNumericCellValue());
cellValue =new Double( (myCell.getNumericCellValue()))+"";
}
}

最佳答案

使用 setCellType(Cell.CELL_TYPE_STRING) 在读取前设置列的类型。

喜欢

else if(myCell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
// Set type as String
myCell.setCellType(Cell.CELL_TYPE_STRING);
// And read as String
String numberAsString = myCell.getStringCellValue();

System.out.println("cell type -- number"+numberAsString);

try {
cellValue = Double.parseDouble(numberAsString);
} catch (NumberFormatException e) {
cellValue = 0; // Handle unexpected values
}
}

关于android - 使用 Apache POI 库解析出现在 Excel 工作表中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21675641/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com