表頭示例
文章來源:http://www.zghlxwxcb.cn/news/detail-742286.html
導(dǎo)入代碼
@Override
public void importExcel(InputStream inputStream) {
ItemExcelListener itemExcelListener = new ItemExcelListener();
EasyExcel.read(inputStream, ImportItem.class, itemExcelListener).headRowNumber(2).sheet().doRead();
}
@Slf4j
public class ItemExcelListener extends AnalysisEventListener<ImportItem> {
/**
* 定義100條數(shù)據(jù)存儲一次,然后清理list,方便內(nèi)存回收
*/
private static final int BATCH_COUNT = 300;
/**
* 記錄導(dǎo)入的總記錄數(shù)
*/
private Long listSize = 0L;
/**
* 緩存的數(shù)據(jù)
*/
private List<ImportItem> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
//標(biāo)記處理第一行表頭
private boolean firstRowProcessed = true;
private HashMap<Integer, HashMap<String,String>> dynamicInfoList;
/**
* 解析每一行表頭數(shù)據(jù)時調(diào)用
*/
@Override
public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
// 讀取表頭數(shù)據(jù)并構(gòu)造HashMap
if (firstRowProcessed){
HashMap<Integer, HashMap<String, String>> infoList = new HashMap<>();
for (Map.Entry<Integer, String> entry : headMap.entrySet()){
Integer key = entry.getKey();
String value = entry.getValue();
if (!Objects.equals(value, "固定數(shù)據(jù)")){
infoList.put(key,null);
}
}
dynamicInfoList = infoList;
firstRowProcessed = false;
}else {
for (Map.Entry<Integer, String> entry : headMap.entrySet()){
HashMap<String, String> info = new HashMap<>();
Integer key = entry.getKey();
String value = entry.getValue();
if (dynamicInfoList.containsKey(key)){
info.put(value,null);
dynamicInfoList.replace(key,info);
}
}
}
}
/**
* 每解析一條數(shù)據(jù)都會調(diào)用一次
*/
@Override
public void invoke(ImportItem importItem, AnalysisContext analysisContext) {
// 獲取實體類中不匹配的數(shù)據(jù)
ReadRowHolder readRowHolder = analysisContext.readRowHolder();
Map<Integer, Cell> cellMap = readRowHolder.getCellMap();
JSONObject dynamicInformation = new JSONObject();
for (Map.Entry<Integer, Cell> entry : cellMap.entrySet()){
Integer key = entry.getKey();
Cell entryValue = entry.getValue();
if (dynamicInfoList.containsKey(key)){
String string = JSON.toJSONString(entryValue);
JSONObject jsonObject = JSON.parseObject(string);
String value = jsonObject.getString("stringValue");
HashMap<String, String> info = dynamicInfoList.get(key);
for (String attribute : info.keySet()) {
dynamicInformation.put(attribute,value);
}
}
}
cachedDataList.add(importItem);
// 達到BATCH_COUNT了,需要去存儲一次數(shù)據(jù)庫,防止數(shù)據(jù)幾萬條數(shù)據(jù)在內(nèi)存,容易OOM
if (cachedDataList.size() >= BATCH_COUNT) {
saveData();
// 存儲完成清理 list
cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
}
}
/**
* 所有數(shù)據(jù)解析完成后調(diào)用
*/
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
if (!cachedDataList.isEmpty()){
saveData();
// 存儲完成清理 list
cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
}
}
public void saveData(){
//將數(shù)據(jù)存入數(shù)據(jù)庫
}
}
數(shù)據(jù)導(dǎo)出
參考文章:EasyExcel動態(tài)復(fù)雜表頭導(dǎo)出方法文章來源地址http://www.zghlxwxcb.cn/news/detail-742286.html
到了這里,關(guān)于EasyExcel復(fù)雜表頭數(shù)據(jù)導(dǎo)入的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!