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("商品名称:" + String.valueOf(cell.getNumericCellValue())); cell = columnIterator.next(); importViewModel.setType("商品类型:" + String.valueOf(cell.getNumericCellValue())); cell = columnIterator.next(); importViewModel.setStyle("商品款式:" + String.valueOf(cell.getNumericCellValue())); cell = columnIterator.next(); importViewModel.setInventoryNum("预期应有库存:" + String.valueOf(cell.getNumericCellValue())); 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 numeric value from a text cell怎么获取单元格中的字符串
时间: 2023-12-10 19:40:47 浏览: 138
你可以使用 `cell.getStringCellValue()` 方法来获取单元格中的字符串。在你的代码中,替换以下行:
```
importViewModel.setName("商品名称:" + String.valueOf(cell.getNumericCellValue()));
importViewModel.setType("商品类型:" + String.valueOf(cell.getNumericCellValue()));
importViewModel.setStyle("商品款式:" + String.valueOf(cell.getNumericCellValue()));
importViewModel.setInventoryNum("预期应有库存:" + String.valueOf(cell.getNumericCellValue()));
```
为:
```
importViewModel.setName("商品名称:" + cell.getStringCellValue());
columnIterator.next();
importViewModel.setType("商品类型:" + cell.getStringCellValue());
columnIterator.next();
importViewModel.setStyle("商品款式:" + cell.getStringCellValue());
columnIterator.next();
importViewModel.setInventoryNum("预期应有库存:" + cell.getStringCellValue());
```
这样就可以获取到单元格中的字符串了。
阅读全文