private static List<ImportViewModel> readExcelFile() { List<ImportViewModel> datas = new ArrayList<>(); FileInputStream inputStream = null; Workbook workbook = null; try { inputStream = new FileInputStream("/sdcard/Template/IC1001.xlsx"); workbook = WorkbookFactory.create(inputStream); Sheet sheet = workbook.getSheetAt(0); Iterator<Row> rowIterator = sheet.rowIterator(); if (rowIterator.hasNext()) { rowIterator.next(); while (rowIterator.hasNext()) { Row row = rowIterator.next(); Iterator<Cell> columnIterator = row.cellIterator(); ImportViewModel importViewModel = new ImportViewModel(); Cell cell = columnIterator.next(); importViewModel.setName("商品名称:1" + cell.getStringCellValue()); importViewModel.setType("商品类型:2" + cell.getStringCellValue()); importViewModel.setStyle("商品款式:3" + cell.getStringCellValue()); importViewModel.setInventoryNum("预期应有库存:4" + cell.getStringCellValue()); datas.add(importViewModel); } } } catch (IOException | InvalidFormatException e) { e.printStackTrace(); } finally { try { if (workbook != null) { workbook.close(); } if (inputStream != null) { inputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } return datas; } java.lang.IllegalStateException: Cannot get a text value from a numeric cell android怎么解决
时间: 2023-11-28 07:49:49 浏览: 175
这个错误是因为你尝试从一个数值类型的单元格中获取文本值。你可以使用 `Cell.getCellType()` 方法来判断单元格的类型,然后使用不同的方法来获取单元格的值。例如,如果单元格的类型是数值类型,你可以使用 `cell.getNumericCellValue()` 方法来获取数值类型的值。如果你想要获取文本值,你可以将数值类型的值转换为字符串类型,例如使用 `String.valueOf(cell.getNumericCellValue())` 方法。在你的代码中,你需要在获取单元格的值之前,先检查单元格的类型是否是字符串类型,例如:
```
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
Iterator<Cell> columnIterator = row.cellIterator();
ImportViewModel importViewModel = new ImportViewModel();
Cell cell = columnIterator.next();
if (cell.getCellType() == CellType.STRING) {
importViewModel.setName("商品名称:1" + cell.getStringCellValue());
} else if (cell.getCellType() == CellType.NUMERIC) {
importViewModel.setName("商品名称:1" + String.valueOf(cell.getNumericCellValue()));
}
// 同理,获取其他单元格的值
datas.add(importViewModel);
}
```
阅读全文