读取 excel 数据
List<Map<String,String>> dataList = new ArrayList<>();
Map<Integer, String> headColMap = new HashMap<>();
EasyExcel.read(file.getInputStream())
.registerReadListener(new ColNameAndValueListener(dataList,headColMap))
.sheet("Sheet1").doRead();
ColNameAndValueListener
主要是将数据和表头填充
public class ColNameAndValueListener implements ReadListener<Map<Integer,String>> {
private Map<Integer, String> headColMap ;
private List<Map<String,String>> dataList;
public ColNameAndValueListener(List<Map<String,String>> dataList,Map<Integer, String> headColMap ) {
this.dataList = dataList;
this.headColMap = headColMap;
}
@Override
public void invoke(Map<Integer, String> map, AnalysisContext analysisContext) {
Map<String, String> dataMap = new HashMap<>(map.size());
for (Map.Entry<Integer, String> entry : map.entrySet()) {
Integer key = entry.getKey();
String colName = headColMap.get(key);
if (StringUtils.hasLength(colName)) {
dataMap.put(colName, entry.getValue());
}
}
if (!CollectionUtils.isEmpty(dataMap)) {
dataList.add(dataMap);
}
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
}
@Override
public void invokeHead(Map<Integer, ReadCellData<?>> headMap, AnalysisContext context) {
// 获取表头信息
headColMap = ConverterUtils.convertToStringMap(headMap, context);
}
}
- 本文链接: http://maltose.top/archives/easyexcel-gen-ju-biao-tou-xin-xi-huo-qu-shu-ju
- 版权声明: 本博客所有文章除特别声明外,均采用CC BY-NC-SA 3.0 许可协议。转载请注明出处!