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怎么解决
时间: 2023-11-28 15:49:45 浏览: 135
这个异常是因为在读取Excel文件时,程序试图从一个数字单元格中获取文本值,而不是数字值。你需要使用Cell.getCellType()方法来判断单元格的类型,然后再根据单元格的类型来获取对应的值。以下是修改后的代码示例:
```
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
Iterator<Cell> columnIterator = row.cellIterator();
ImportViewModel importViewModel = new ImportViewModel();
Cell cell = columnIterator.next();
if (cell.getCellType() == CellType.NUMERIC) {
importViewModel.setName("商品名称:1" + cell.getNumericCellValue());
} else {
importViewModel.setName("商品名称:1" + cell.getStringCellValue());
}
// 同样的方式处理其他单元格
datas.add(importViewModel);
}
```
这样就可以避免这个异常了。
相关问题
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; }将返回值显示到前端页面上
假设你使用的是Java Spring框架,可以使用Thymeleaf模板引擎将返回值显示到前端页面上。首先在Controller中定义一个请求处理方法,将读取Excel文件的结果存入Model中,然后返回对应的HTML页面:
```java
@GetMapping("/excel")
public String showExcelData(Model model) {
List<ImportViewModel> datas = readExcelFile();
model.addAttribute("datas", datas);
return "excelData";
}
```
然后在HTML页面中使用Thymeleaf标签来渲染数据:
```html
<table>
<thead>
<tr>
<th>商品名称</th>
<th>商品类型</th>
<th>商品款式</th>
<th>预期应有库存</th>
</tr>
</thead>
<tbody>
<tr th:each="data : ${datas}">
<td th:text="${data.name}"></td>
<td th:text="${data.type}"></td>
<td th:text="${data.style}"></td>
<td th:text="${data.inventoryNum}"></td>
</tr>
</tbody>
</table>
```
这样就可以将读取Excel文件的结果以表格的形式展示在HTML页面上了。
private List<String> readExcelFile() { List<String> datas = new ArrayList<>(); try { FileInputStream inputStream = new FileInputStream("/sdcard/Template/IC1001.xlsx"); Workbook workbook = WorkbookFactory.create(inputStream); Sheet sheet = workbook.getSheetAt(0); for (Row row : sheet) { for (Cell cell : row) { datas.add(cell.toString()); } } workbook.close(); } catch (IOException | InvalidFormatException e) { e.printStackTrace(); } return datas; }读取不到excel中的数据,怎么解决
首先,确保你的文件路径是正确的,且文件存在。另外,这个方法只会将 Excel 文件中的每个单元格的数据都添加到列表中,而不会将它们按行添加。如果你想要按行读取 Excel 文件中的数据,可以尝试下面的代码:
```
private List<List<String>> readExcelFile() {
List<List<String>> datas = new ArrayList<>();
try {
FileInputStream inputStream = new FileInputStream("/sdcard/Template/IC1001.xlsx");
Workbook workbook = WorkbookFactory.create(inputStream);
Sheet sheet = workbook.getSheetAt(0);
for (Row row : sheet) {
List<String> rowData = new ArrayList<>();
for (Cell cell : row) {
rowData.add(cell.toString());
}
datas.add(rowData);
}
workbook.close();
} catch (IOException | InvalidFormatException e) {
e.printStackTrace();
}
return datas;
}
```
这样,你就可以获取到一个二维列表,其中每个子列表对应 Excel 文件中的一行数据。
阅读全文