返回十进制而不是字符串(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; } }
- 在应用PropertyTemplate边框时,Apache POI使用黑色而不是所需的自定义颜色填充XSSF单元格
- 使用Apache POI docx在MS Word文档中进行间距和边距设置
- 编写XSSFWorkbook时,使用JDBC在Spring Boot应用程序中创建损坏的.xlsx文档
- java.lang.NoClassDefFoundError:org / openxmlformats / schemas / spreadsheetml / x2006 / main / CTWorkbook $ Factory
- 从excel单元格中读取日期值作为String
- 使用HSSF的Apache POI比XSSF快得多 – 下一步是什么?
- 如何使用Apache Poi更改工作表的选项卡颜色
- 使用Apache Poi从Excel工作表中获取单元格值
- 将自定义形状添加到JTextpane并保存到新的Word文档