工具类
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import java.util.LinkedHashMap;
public class ExcelReaderUtils {
public static String readSpecificCell(String filePath, String sheetName, int rowIndex, int colIndex) {
SpecificCellListener specificCellListener = new SpecificCellListener(rowIndex, colIndex);
EasyExcel.read(filePath, specificCellListener).sheet(sheetName).doRead();
return specificCellListener.getSpecificCellValue();
}
static class SpecificCellListener extends AnalysisEventListener<LinkedHashMap<Integer, String>> {
private int targetRowIndex;
private int targetColIndex;
private String specificCellValue;
public SpecificCellListener(int targetRowIndex, int targetColIndex) {
this.targetRowIndex = targetRowIndex;
this.targetColIndex = targetColIndex;
}
@Override
public void invoke(LinkedHashMap<Integer, String> data, AnalysisContext context) {
if (context.readRowHolder().getRowIndex() == targetRowIndex && data != null && targetColIndex < data.size()) {
specificCellValue = data.get(targetColIndex).toString();
}
}
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
}
public String getSpecificCellValue() {
return specificCellValue;
}
}
}