mirror of https://github.com/alibaba/easyexcel
Jiaju Zhuang
3 years ago
committed by
GitHub
27 changed files with 281 additions and 265 deletions
@ -0,0 +1,23 @@ |
|||||||
|
package com.alibaba.excel.read.listener; |
||||||
|
|
||||||
|
import com.alibaba.excel.context.AnalysisContext; |
||||||
|
|
||||||
|
/** |
||||||
|
* Interface to listen for read results |
||||||
|
* |
||||||
|
* @author Jiaju Zhuang |
||||||
|
*/ |
||||||
|
public interface IgnoreExceptionReadListener<T> extends ReadListener<T> { |
||||||
|
|
||||||
|
/** |
||||||
|
* All listeners receive this method when any one Listener does an error report. If an exception is thrown here, the |
||||||
|
* entire read will terminate. |
||||||
|
* |
||||||
|
* @param exception |
||||||
|
* @param context |
||||||
|
* @throws Exception |
||||||
|
*/ |
||||||
|
@Override |
||||||
|
default void onException(Exception exception, AnalysisContext context) throws Exception {} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,30 @@ |
|||||||
|
package com.alibaba.easyexcel.test.core.exception; |
||||||
|
|
||||||
|
import java.util.ArrayList; |
||||||
|
import java.util.List; |
||||||
|
|
||||||
|
import com.alibaba.excel.context.AnalysisContext; |
||||||
|
import com.alibaba.excel.read.listener.ReadListener; |
||||||
|
|
||||||
|
import org.slf4j.Logger; |
||||||
|
import org.slf4j.LoggerFactory; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author Jiaju Zhuang |
||||||
|
*/ |
||||||
|
public class ExceptionThrowDataListener implements ReadListener<ExceptionData> { |
||||||
|
private static final Logger LOGGER = LoggerFactory.getLogger(ExceptionData.class); |
||||||
|
List<ExceptionData> list = new ArrayList<ExceptionData>(); |
||||||
|
|
||||||
|
@Override |
||||||
|
public void invoke(ExceptionData data, AnalysisContext context) { |
||||||
|
list.add(data); |
||||||
|
if (list.size() == 5) { |
||||||
|
int i = 5 / 0; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void doAfterAllAnalysed(AnalysisContext context) { |
||||||
|
} |
||||||
|
} |
@ -1,48 +1,48 @@ |
|||||||
package com.alibaba.easyexcel.test.demo.read; |
package com.alibaba.easyexcel.test.demo.read; |
||||||
|
|
||||||
import java.util.ArrayList; |
|
||||||
import java.util.List; |
import java.util.List; |
||||||
|
|
||||||
import com.alibaba.excel.context.AnalysisContext; |
import com.alibaba.excel.context.AnalysisContext; |
||||||
import com.alibaba.excel.event.AnalysisEventListener; |
import com.alibaba.excel.read.listener.ReadListener; |
||||||
|
import com.alibaba.excel.util.ListUtils; |
||||||
import com.alibaba.fastjson.JSON; |
import com.alibaba.fastjson.JSON; |
||||||
|
|
||||||
import org.slf4j.Logger; |
import lombok.extern.slf4j.Slf4j; |
||||||
import org.slf4j.LoggerFactory; |
|
||||||
|
|
||||||
/** |
/** |
||||||
* 读取头 |
* 读取头 |
||||||
* |
* |
||||||
* @author Jiaju Zhuang |
* @author Jiaju Zhuang |
||||||
*/ |
*/ |
||||||
public class CellDataDemoHeadDataListener extends AnalysisEventListener<CellDataReadDemoData> { |
@Slf4j |
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(CellDataDemoHeadDataListener.class); |
public class CellDataDemoHeadDataListener implements ReadListener<CellDataReadDemoData> { |
||||||
/** |
/** |
||||||
* 每隔5条存储数据库,实际使用中可以100条,然后清理list ,方便内存回收 |
* 每隔5条存储数据库,实际使用中可以100条,然后清理list ,方便内存回收 |
||||||
*/ |
*/ |
||||||
private static final int BATCH_COUNT = 100; |
private static final int BATCH_COUNT = 100; |
||||||
List<CellDataReadDemoData> list = new ArrayList<CellDataReadDemoData>(); |
|
||||||
|
private List<CellDataReadDemoData> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT); |
||||||
|
|
||||||
@Override |
@Override |
||||||
public void invoke(CellDataReadDemoData data, AnalysisContext context) { |
public void invoke(CellDataReadDemoData data, AnalysisContext context) { |
||||||
LOGGER.info("解析到一条数据:{}", JSON.toJSONString(data)); |
log.info("解析到一条数据:{}", JSON.toJSONString(data)); |
||||||
if (list.size() >= BATCH_COUNT) { |
if (cachedDataList.size() >= BATCH_COUNT) { |
||||||
saveData(); |
saveData(); |
||||||
list.clear(); |
cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT); |
||||||
} |
} |
||||||
} |
} |
||||||
|
|
||||||
@Override |
@Override |
||||||
public void doAfterAllAnalysed(AnalysisContext context) { |
public void doAfterAllAnalysed(AnalysisContext context) { |
||||||
saveData(); |
saveData(); |
||||||
LOGGER.info("所有数据解析完成!"); |
log.info("所有数据解析完成!"); |
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* 加上存储数据库 |
* 加上存储数据库 |
||||||
*/ |
*/ |
||||||
private void saveData() { |
private void saveData() { |
||||||
LOGGER.info("{}条数据,开始存储数据库!", list.size()); |
log.info("{}条数据,开始存储数据库!", cachedDataList.size()); |
||||||
LOGGER.info("存储数据库成功!"); |
log.info("存储数据库成功!"); |
||||||
} |
} |
||||||
} |
} |
||||||
|
@ -1,49 +1,49 @@ |
|||||||
package com.alibaba.easyexcel.test.demo.read; |
package com.alibaba.easyexcel.test.demo.read; |
||||||
|
|
||||||
import java.util.ArrayList; |
|
||||||
import java.util.List; |
import java.util.List; |
||||||
|
|
||||||
import com.alibaba.excel.context.AnalysisContext; |
import com.alibaba.excel.context.AnalysisContext; |
||||||
import com.alibaba.excel.event.AnalysisEventListener; |
import com.alibaba.excel.read.listener.ReadListener; |
||||||
|
import com.alibaba.excel.util.ListUtils; |
||||||
import com.alibaba.fastjson.JSON; |
import com.alibaba.fastjson.JSON; |
||||||
|
|
||||||
import org.slf4j.Logger; |
import lombok.extern.slf4j.Slf4j; |
||||||
import org.slf4j.LoggerFactory; |
|
||||||
|
|
||||||
/** |
/** |
||||||
* 模板的读取类 |
* 模板的读取类 |
||||||
* |
* |
||||||
* @author Jiaju Zhuang |
* @author Jiaju Zhuang |
||||||
*/ |
*/ |
||||||
public class ConverterDataListener extends AnalysisEventListener<ConverterData> { |
@Slf4j |
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(ConverterDataListener.class); |
public class ConverterDataListener implements ReadListener<ConverterData> { |
||||||
|
|
||||||
/** |
/** |
||||||
* 每隔5条存储数据库,实际使用中可以100条,然后清理list ,方便内存回收 |
* 每隔5条存储数据库,实际使用中可以100条,然后清理list ,方便内存回收 |
||||||
*/ |
*/ |
||||||
private static final int BATCH_COUNT = 5; |
private static final int BATCH_COUNT = 5; |
||||||
List<ConverterData> list = new ArrayList<ConverterData>(); |
private List<ConverterData> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT); |
||||||
|
|
||||||
@Override |
@Override |
||||||
public void invoke(ConverterData data, AnalysisContext context) { |
public void invoke(ConverterData data, AnalysisContext context) { |
||||||
LOGGER.info("解析到一条数据:{}", JSON.toJSONString(data)); |
log.info("解析到一条数据:{}", JSON.toJSONString(data)); |
||||||
list.add(data); |
cachedDataList.add(data); |
||||||
if (list.size() >= BATCH_COUNT) { |
if (cachedDataList.size() >= BATCH_COUNT) { |
||||||
saveData(); |
saveData(); |
||||||
list.clear(); |
cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT); |
||||||
} |
} |
||||||
} |
} |
||||||
|
|
||||||
@Override |
@Override |
||||||
public void doAfterAllAnalysed(AnalysisContext context) { |
public void doAfterAllAnalysed(AnalysisContext context) { |
||||||
saveData(); |
saveData(); |
||||||
LOGGER.info("所有数据解析完成!"); |
log.info("所有数据解析完成!"); |
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* 加上存储数据库 |
* 加上存储数据库 |
||||||
*/ |
*/ |
||||||
private void saveData() { |
private void saveData() { |
||||||
LOGGER.info("{}条数据,开始存储数据库!", list.size()); |
log.info("{}条数据,开始存储数据库!", cachedDataList.size()); |
||||||
LOGGER.info("存储数据库成功!"); |
log.info("存储数据库成功!"); |
||||||
} |
} |
||||||
} |
} |
||||||
|
@ -1,49 +1,48 @@ |
|||||||
package com.alibaba.easyexcel.test.demo.read; |
package com.alibaba.easyexcel.test.demo.read; |
||||||
|
|
||||||
import java.util.ArrayList; |
|
||||||
import java.util.List; |
import java.util.List; |
||||||
|
|
||||||
import com.alibaba.excel.context.AnalysisContext; |
import com.alibaba.excel.context.AnalysisContext; |
||||||
import com.alibaba.excel.event.AnalysisEventListener; |
import com.alibaba.excel.event.AnalysisEventListener; |
||||||
|
import com.alibaba.excel.util.ListUtils; |
||||||
import com.alibaba.fastjson.JSON; |
import com.alibaba.fastjson.JSON; |
||||||
|
|
||||||
import org.slf4j.Logger; |
import lombok.extern.slf4j.Slf4j; |
||||||
import org.slf4j.LoggerFactory; |
|
||||||
|
|
||||||
/** |
/** |
||||||
* 模板的读取类 |
* 模板的读取类 |
||||||
* |
* |
||||||
* @author Jiaju Zhuang |
* @author Jiaju Zhuang |
||||||
*/ |
*/ |
||||||
|
@Slf4j |
||||||
public class IndexOrNameDataListener extends AnalysisEventListener<IndexOrNameData> { |
public class IndexOrNameDataListener extends AnalysisEventListener<IndexOrNameData> { |
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(IndexOrNameDataListener.class); |
|
||||||
/** |
/** |
||||||
* 每隔5条存储数据库,实际使用中可以100条,然后清理list ,方便内存回收 |
* 每隔5条存储数据库,实际使用中可以100条,然后清理list ,方便内存回收 |
||||||
*/ |
*/ |
||||||
private static final int BATCH_COUNT = 5; |
private static final int BATCH_COUNT = 5; |
||||||
List<IndexOrNameData> list = new ArrayList<IndexOrNameData>(); |
private List<IndexOrNameData> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT); |
||||||
|
|
||||||
@Override |
@Override |
||||||
public void invoke(IndexOrNameData data, AnalysisContext context) { |
public void invoke(IndexOrNameData data, AnalysisContext context) { |
||||||
LOGGER.info("解析到一条数据:{}", JSON.toJSONString(data)); |
log.info("解析到一条数据:{}", JSON.toJSONString(data)); |
||||||
list.add(data); |
cachedDataList.add(data); |
||||||
if (list.size() >= BATCH_COUNT) { |
if (cachedDataList.size() >= BATCH_COUNT) { |
||||||
saveData(); |
saveData(); |
||||||
list.clear(); |
cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT); |
||||||
} |
} |
||||||
} |
} |
||||||
|
|
||||||
@Override |
@Override |
||||||
public void doAfterAllAnalysed(AnalysisContext context) { |
public void doAfterAllAnalysed(AnalysisContext context) { |
||||||
saveData(); |
saveData(); |
||||||
LOGGER.info("所有数据解析完成!"); |
log.info("所有数据解析完成!"); |
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* 加上存储数据库 |
* 加上存储数据库 |
||||||
*/ |
*/ |
||||||
private void saveData() { |
private void saveData() { |
||||||
LOGGER.info("{}条数据,开始存储数据库!", list.size()); |
log.info("{}条数据,开始存储数据库!", cachedDataList.size()); |
||||||
LOGGER.info("存储数据库成功!"); |
log.info("存储数据库成功!"); |
||||||
} |
} |
||||||
} |
} |
||||||
|
@ -1,50 +1,49 @@ |
|||||||
package com.alibaba.easyexcel.test.demo.read; |
package com.alibaba.easyexcel.test.demo.read; |
||||||
|
|
||||||
import java.util.ArrayList; |
|
||||||
import java.util.List; |
import java.util.List; |
||||||
import java.util.Map; |
import java.util.Map; |
||||||
|
|
||||||
import com.alibaba.excel.context.AnalysisContext; |
import com.alibaba.excel.context.AnalysisContext; |
||||||
import com.alibaba.excel.event.AnalysisEventListener; |
import com.alibaba.excel.event.AnalysisEventListener; |
||||||
|
import com.alibaba.excel.util.ListUtils; |
||||||
import com.alibaba.fastjson.JSON; |
import com.alibaba.fastjson.JSON; |
||||||
|
|
||||||
import org.slf4j.Logger; |
import lombok.extern.slf4j.Slf4j; |
||||||
import org.slf4j.LoggerFactory; |
|
||||||
|
|
||||||
/** |
/** |
||||||
* 直接用map接收数据 |
* 直接用map接收数据 |
||||||
* |
* |
||||||
* @author Jiaju Zhuang |
* @author Jiaju Zhuang |
||||||
*/ |
*/ |
||||||
|
@Slf4j |
||||||
public class NoModelDataListener extends AnalysisEventListener<Map<Integer, String>> { |
public class NoModelDataListener extends AnalysisEventListener<Map<Integer, String>> { |
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(NoModelDataListener.class); |
|
||||||
/** |
/** |
||||||
* 每隔5条存储数据库,实际使用中可以100条,然后清理list ,方便内存回收 |
* 每隔5条存储数据库,实际使用中可以100条,然后清理list ,方便内存回收 |
||||||
*/ |
*/ |
||||||
private static final int BATCH_COUNT = 5; |
private static final int BATCH_COUNT = 5; |
||||||
List<Map<Integer, String>> list = new ArrayList<Map<Integer, String>>(); |
private List<Map<Integer, String>> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT); |
||||||
|
|
||||||
@Override |
@Override |
||||||
public void invoke(Map<Integer, String> data, AnalysisContext context) { |
public void invoke(Map<Integer, String> data, AnalysisContext context) { |
||||||
LOGGER.info("解析到一条数据:{}", JSON.toJSONString(data)); |
log.info("解析到一条数据:{}", JSON.toJSONString(data)); |
||||||
list.add(data); |
cachedDataList.add(data); |
||||||
if (list.size() >= BATCH_COUNT) { |
if (cachedDataList.size() >= BATCH_COUNT) { |
||||||
saveData(); |
saveData(); |
||||||
list.clear(); |
cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT); |
||||||
} |
} |
||||||
} |
} |
||||||
|
|
||||||
@Override |
@Override |
||||||
public void doAfterAllAnalysed(AnalysisContext context) { |
public void doAfterAllAnalysed(AnalysisContext context) { |
||||||
saveData(); |
saveData(); |
||||||
LOGGER.info("所有数据解析完成!"); |
log.info("所有数据解析完成!"); |
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* 加上存储数据库 |
* 加上存储数据库 |
||||||
*/ |
*/ |
||||||
private void saveData() { |
private void saveData() { |
||||||
LOGGER.info("{}条数据,开始存储数据库!", list.size()); |
log.info("{}条数据,开始存储数据库!", cachedDataList.size()); |
||||||
LOGGER.info("存储数据库成功!"); |
log.info("存储数据库成功!"); |
||||||
} |
} |
||||||
} |
} |
||||||
|
Loading…
Reference in new issue