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(); while (rowIterator.hasNext()) { Row row = rowIterator.next(); ImportViewModel importViewModel = new ImportViewModel(); Iterator<Cell> columnIterator = row.cellIterator(); while (columnIterator.hasNext()) { Cell cell= columnIterator.next(); 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.util.NoSuchElementException怎么修改
时间: 2023-11-28 07:48:50 浏览: 59
java.util.NoSuchElementException异常通常发生在当你试图访问一个没有下一个元素的迭代器时。在你的代码中,这可能是因为你每次迭代器循环时都调用了两次`columnIterator.next()`方法,而在某些情况下,这会导致迭代器尝试读取不存在的下一个元素。要修复这个问题,你可以将两个方法调用合并为一个方法调用,如下所示:
```
while (columnIterator.hasNext()) {
Cell cell = columnIterator.next();
importViewModel.setName("商品名称:" + cell.getStringCellValue());
importViewModel.setType("商品类型:" + cell.getStringCellValue());
importViewModel.setStyle("商品款式:" + cell.getStringCellValue());
importViewModel.setInventoryNum("预期应有库存:" + cell.getStringCellValue());
datas.add(importViewModel);
}
```
这样,每次迭代时只会调用一次`columnIterator.next()`方法,从而避免了可能导致`NoSuchElementException`异常的问题。
相关问题
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 文件中的一行数据。
阅读全文