返回十进制而不是字符串(POI jar)

我需要阅读xls或xlsx表。 我成功地阅读了工作表,但它返回十进制值而不是字符串(例如:for 3 – 它返回3.0)。 我需要按原样读取单元格值。 所以我需要以字符串forms返回

POI为您提供Excel在文件中存储的确切值。 通常,如果您在Excel单元格中编写一个数字,Excel会将其存储为带格式的数字。 POI支持为您进行格式化(大多数人不这样做 – 他们希望数字为数字,以便他们可以使用它们)

您正在寻找的类是DataFormatter 。 你的代码就像是

DataFormatter fmt = new DataFormatter(); for (Row r : sheet) { for (Cell c : r) { CellReference cr = new CellRefence(c); System.out.println("Cell " + cr.formatAsString() + " is " + fmt.formatCellValue(c) ); } } 
 //use this method for getting values from excel.It might help u. private String getCellValue(Cell cell) { if (cell == null) { return null; } if (cell.getCellType() == Cell.CELL_TYPE_STRING) { return cell.getStringCellValue(); } else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) { return cell.getNumericCellValue() + ""; } else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) { return cell.getBooleanCellValue() + ""; }else if(cell.getCellType() == Cell.CELL_TYPE_BLANK){ return cell.getStringCellValue(); }else if(cell.getCellType() == Cell.CELL_TYPE_ERROR){ return cell.getErrorCellValue() + ""; } else { return null; } }