mirror of https://github.com/alibaba/easyexcel
Jiaju Zhuang
5 years ago
4 changed files with 97 additions and 8 deletions
@ -0,0 +1,22 @@
|
||||
package com.alibaba.easyexcel.test.temp; |
||||
|
||||
import lombok.Data; |
||||
|
||||
/** |
||||
* 基础数据类.这里的排序和excel里面的排序一致 |
||||
* |
||||
* @author Jiaju Zhuang |
||||
**/ |
||||
@Data |
||||
public class LockData { |
||||
private String string0; |
||||
private String string1; |
||||
private String string2; |
||||
private String string3; |
||||
private String string4; |
||||
private String string5; |
||||
private String string6; |
||||
private String string7; |
||||
private String string8; |
||||
|
||||
} |
@ -0,0 +1,50 @@
|
||||
package com.alibaba.easyexcel.test.temp; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import com.alibaba.easyexcel.test.demo.read.DemoDataListener; |
||||
import com.alibaba.excel.context.AnalysisContext; |
||||
import com.alibaba.excel.event.AnalysisEventListener; |
||||
import com.alibaba.fastjson.JSON; |
||||
|
||||
/** |
||||
* 模板的读取类 |
||||
* |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
public class LockDataListener extends AnalysisEventListener<LockData> { |
||||
private static final Logger LOGGER = LoggerFactory.getLogger(DemoDataListener.class); |
||||
/** |
||||
* 每隔5条存储数据库,实际使用中可以3000条,然后清理list ,方便内存回收 |
||||
*/ |
||||
private static final int BATCH_COUNT = 5; |
||||
List<LockData> list = new ArrayList<LockData>(); |
||||
|
||||
@Override |
||||
public void invoke(LockData data, AnalysisContext context) { |
||||
LOGGER.info("解析到一条数据:{}", JSON.toJSONString(data)); |
||||
list.add(data); |
||||
if (list.size() >= BATCH_COUNT) { |
||||
saveData(); |
||||
list.clear(); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void doAfterAllAnalysed(AnalysisContext context) { |
||||
saveData(); |
||||
LOGGER.info("所有数据解析完成!"); |
||||
} |
||||
|
||||
/** |
||||
* 加上存储数据库 |
||||
*/ |
||||
private void saveData() { |
||||
LOGGER.info("{}条数据,开始存储数据库!", list.size()); |
||||
LOGGER.info("存储数据库成功!"); |
||||
} |
||||
} |
Loading…
Reference in new issue