easyexcel 读取指定行和列的单个数据
时间: 2024-12-10 11:29:18 浏览: 498
EasyExcel 是阿里巴巴开源的一个 Excel 阅读库,它简化了 Excel 文件的导入操作。如果你想要从 EasyExcel 中读取特定行和列的数据,你可以使用 `reader.read()` 方法配合自定义的 `DataReader` 或者 `RowMapper` 来处理。
例如,假设你想读取第 3 行第 4 列(注意,中国人的习惯是从0开始计数,所以第 3 行对应的是索引 2),可以这样做:
```java
// 创建 DataReader 或 RowMapper 对象
Map<String, Object> cellData = new HashMap<>();
cellData.put("columnHeader", "第四列标题"); // 标记列名
List<RowData> rowDataList = reader.read(2, (rowIndex, sheet) -> { // rowIndex 从 0 开始
if (rowIndex == 2) {
for (int i = 3; i < sheet.getLastCellNum(); i++) { // 第 3 到最后一列
cellData.put(sheet.getCell(i, 0).getStringCellValue(), sheet.getCell(i, 3).getStringCellValue()); // 获取值
}
return cellData;
} else {
return null; // 如果不是目标行则返回null,避免错误
}
}, Collections.singletonList(cellData)); // 每次只读一行数据
if (rowDataList != null && !rowDataList.isEmpty()) {
Map<String, String> singleData = rowDataList.get(0); // 提取第一行的数据
System.out.println("第 3 行第 4 列数据: " + singleData.get("第四列标题"));
}
```
阅读全文
相关推荐
















