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; }将返回值显示到前端页面上
时间: 2023-11-28 07:47:16 浏览: 65
假设你使用的是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页面上了。
阅读全文