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);Unable to start activity ComponentInfo{com.ives.upcrfid_zebra/com.ives.upcrfid_zebra.MainActivity}: java.lang.IllegalArgumentException: Sheet index (4) is out of range (0..0)
时间: 2023-11-28 14:48:09 浏览: 124
根据错误提示,Sheet index (4) is out of range (0..0),说明您正在尝试读取一个超出范围的表格索引,这里只有一个表格,因此可用的索引值为0。您需要将以下代码中的索引值4更改为0:
```java
Sheet sheet = workbook.getSheetAt(4);
```
修改为:
```java
Sheet sheet = workbook.getSheetAt(0);
```
相关问题
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 文件中的一行数据。
阅读全文