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())); importViewModel.setType("商品类型:" + String.valueOf(cell.getNumericCellValue())); importViewModel.setStyle("商品款式:" + String.valueOf(cell.getNumericCellValue())); 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; }读取的是第一列的数据,没有读取下一位的数据,则怎么修改
时间: 2023-11-28 15:50:03 浏览: 28
在循环中,你只取了每行的第一个单元格的数据,而没有取出其他单元格的数据。所以,你需要使用`columnIterator.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);
}
```
这里使用`columnIterator.next()`来迭代每个单元格,以便获取整行数据。同时,在取单元格数据的时候,需要使用`cell = columnIterator.next()`来获取下一个单元格的数据。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![csv](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)