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(4); Iterator<Row> rowIterator = sheet.rowIterator(); while (rowIterator.hasNext()) { Row row = rowIterator.next(); ImportViewModel importViewModel = new ImportViewModel(); Iterator<Cell> columnIterator = row.cellIterator(); while (columnIterator.hasNext()) { importViewModel.setName("商品名称:" + columnIterator.next().getStringCellValue()); importViewModel.setType("商品类型:" + columnIterator.next().getStringCellValue()); importViewModel.setStyle("商品款式:" + columnIterator.next().getStringCellValue()); importViewModel.setInventoryNum("预期应有库存:" + columnIterator.next().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; }@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView listView = findViewById(R.id.list_view); List<ImportViewModel> data = readExcelFile(); // 调用您的 readExcelFile() 方法获取数据 // ImportViewModelAdapter adapter = new ImportViewModelAdapter(data); // listView.setAdapter(adapter); RfApplication.toneGenerator = new ToneGenerator(streamType, percantageVolume);IllegalStateException: Cannot get a text value from a numeric cell
时间: 2023-11-28 18:48:16 浏览: 127
这个异常是因为你试图从一个数字单元格中获取文本值,Excel中的单元格可以是数字、文本或其他类型,因此您需要根据单元格的类型来获取单元格的值。您可以使用以下代码来获取单元格的值:
```java
while (columnIterator.hasNext()) {
Cell cell = columnIterator.next();
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
importViewModel.setName("商品名称:" + cell.getStringCellValue());
break;
case Cell.CELL_TYPE_NUMERIC:
importViewModel.setInventoryNum("预期应有库存:" + cell.getNumericCellValue());
break;
// handle other cell types as needed
}
// set the values for other fields in ImportViewModel
datas.add(importViewModel);
}
```
这个代码片段使用了 `getCellType()` 方法来获取单元格的类型,然后根据单元格的类型来获取单元格的值并设置相应的字段值。这样就可以避免从数字单元格中获取文本值的异常了。
阅读全文