Jiaju Zhuang
5 years ago
28 changed files with 464 additions and 44 deletions
@ -0,0 +1,50 @@
|
||||
package com.alibaba.easyexcel.test.demo.read; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import com.alibaba.excel.context.AnalysisContext; |
||||
import com.alibaba.excel.event.AnalysisEventListener; |
||||
import com.alibaba.excel.metadata.CellData; |
||||
import com.alibaba.fastjson.JSON; |
||||
|
||||
/** |
||||
* 读取头 |
||||
* |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
public class CellDataDemoHeadDataListener extends AnalysisEventListener<CellDataReadDemoData> { |
||||
private static final Logger LOGGER = LoggerFactory.getLogger(CellDataDemoHeadDataListener.class); |
||||
/** |
||||
* 每隔5条存储数据库,实际使用中可以3000条,然后清理list ,方便内存回收 |
||||
*/ |
||||
private static final int BATCH_COUNT = 5; |
||||
List<CellDataReadDemoData> list = new ArrayList<CellDataReadDemoData>(); |
||||
|
||||
@Override |
||||
public void invoke(CellDataReadDemoData data, AnalysisContext context) { |
||||
LOGGER.info("解析到一条数据:{}", JSON.toJSONString(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("存储数据库成功!"); |
||||
} |
||||
} |
@ -0,0 +1,22 @@
|
||||
package com.alibaba.easyexcel.test.demo.read; |
||||
|
||||
import java.util.Date; |
||||
|
||||
import com.alibaba.excel.metadata.CellData; |
||||
|
||||
import lombok.Data; |
||||
|
||||
/** |
||||
* 基础数据类.这里的排序和excel里面的排序一致 |
||||
* |
||||
* @author Jiaju Zhuang |
||||
**/ |
||||
@Data |
||||
public class CellDataReadDemoData { |
||||
private CellData<String> string; |
||||
// 这里注意 虽然是日期 但是 类型 存储的是number 因为excel 存储的就是number
|
||||
private CellData<Date> date; |
||||
private CellData<Double> doubleData; |
||||
// 这里并不一定能完美的获取 有些公式是依赖性的 可能会读不到 这个问题后续会修复
|
||||
private CellData<String> formulaValue; |
||||
} |
@ -0,0 +1,80 @@
|
||||
package com.alibaba.easyexcel.test.demo.read; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import com.alibaba.excel.context.AnalysisContext; |
||||
import com.alibaba.excel.event.AnalysisEventListener; |
||||
import com.alibaba.excel.exception.ExcelDataConvertException; |
||||
import com.alibaba.fastjson.JSON; |
||||
|
||||
/** |
||||
* 读取转换异常 |
||||
* |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
public class DemoExceptionListener extends AnalysisEventListener<ExceptionDemoData> { |
||||
private static final Logger LOGGER = LoggerFactory.getLogger(DemoExceptionListener.class); |
||||
/** |
||||
* 每隔5条存储数据库,实际使用中可以3000条,然后清理list ,方便内存回收 |
||||
*/ |
||||
private static final int BATCH_COUNT = 5; |
||||
List<ExceptionDemoData> list = new ArrayList<ExceptionDemoData>(); |
||||
|
||||
/** |
||||
* 在转换异常 获取其他异常下会调用本接口。抛出异常则停止读取。如果这里不抛出异常则 继续读取下一行。 |
||||
* |
||||
* @param exception |
||||
* @param context |
||||
* @throws Exception |
||||
*/ |
||||
@Override |
||||
public void onException(Exception exception, AnalysisContext context) { |
||||
LOGGER.error("解析失败,但是继续解析下一行:{}", exception.getMessage()); |
||||
// 如果是某一个单元格的转换异常 能获取到具体行号
|
||||
// 如果要获取头的信息 配合invokeHeadMap使用
|
||||
if (exception instanceof ExcelDataConvertException) { |
||||
ExcelDataConvertException excelDataConvertException = (ExcelDataConvertException)exception; |
||||
LOGGER.error("第{}行,第{}列解析异常", excelDataConvertException.getRowIndex(), |
||||
excelDataConvertException.getColumnIndex()); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 这里会一行行的返回头 |
||||
* |
||||
* @param headMap |
||||
* @param context |
||||
*/ |
||||
@Override |
||||
public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) { |
||||
LOGGER.info("解析到一条头数据:{}", JSON.toJSONString(headMap)); |
||||
} |
||||
|
||||
@Override |
||||
public void invoke(ExceptionDemoData data, AnalysisContext context) { |
||||
LOGGER.info("解析到一条数据:{}", JSON.toJSONString(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("存储数据库成功!"); |
||||
} |
||||
} |
@ -0,0 +1,18 @@
|
||||
package com.alibaba.easyexcel.test.demo.read; |
||||
|
||||
import java.util.Date; |
||||
|
||||
import lombok.Data; |
||||
|
||||
/** |
||||
* 基础数据类.这里的排序和excel里面的排序一致 |
||||
* |
||||
* @author Jiaju Zhuang |
||||
**/ |
||||
@Data |
||||
public class ExceptionDemoData { |
||||
/** |
||||
* 用日期去接字符串 肯定报错 |
||||
*/ |
||||
private Date date; |
||||
} |
Binary file not shown.
Loading…
Reference in new issue