1.需求场景描述
2.问题分析与实现方案
ExamInfo.java
public class ExamMsg {
@ExcelProperty("题目描述")
private String answer;
@ExcelProperty("考试答案")
private String stem;
@ExcelProperty("考试答案分析")
private String analyse;
@ExcelProperty("题目单项分数")
private Integer singleScore;
@ExcelProperty("选项")
private String choiceType;
@ExcelProperty("选项内容")
private String optionContent;
// 省略get/set
}
目标数据组装类(入表之前的最终数据格式):
public class ExamInfo {
private String answer;
private String stem;
private String analyse;
private Integer singleScore;
private List<EaxmChoiceInfo> eaxmChoiceInfoList=new ArrayList<>();
// 省略get/set
}
每个选择题的四个选择项实体类:
public class EaxmChoiceInfo {
private String choiceType;
private String optionContent;
// 省略get/set
}
自定义监听器
public class ExamListener implements ReadListener<ExamMsg> {
// 自定义消费者函数接口用于自定义监听器中数据组装
private final Consumer<List<ExamInfo>> consumer;
public ExamListener(Consumer<List<ExamInfo>> consumer) {
this.consumer = consumer;
}
// easy excel读取参数
private List<ExamMsg> examMsgList=new ArrayList<>();
// 封装读取对象
private List<ExamInfo> examInfoList=new ArrayList<>();
// 每行读取完成之后会执行
@Override
public void invoke(ExamMsg data, AnalysisContext context) throws IllegalAccessException {
// 按照格式组装数据
if(data.getStem() != null){
ExamInfo examInfo = new ExamInfo();
BeanUtils.copyProperties(data,examInfo);
examInfo.getEaxmChoiceInfoList().add(new EaxmChoiceInfo(data.getChoiceType(),data.getOptionContent()));
examInfoList.add(examInfo);
}else {
// 倒序添加选择题信息,只对最后一个进行添加选项数据信息
examInfoList.get(examInfoList.size() - 1).getEaxmChoiceInfoList().add(new EaxmChoiceInfo(data.getChoiceType(),data.getOptionContent()));
}
}
// 每行读取完成之后执行
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
if (CollectionUtils.isNotEmpty(examInfoList)) {
consumer.accept(examInfoList);
}
}
}
文件读取测试类:
public class PersonalExcel {
public static void main(String[] args) {
List<ExamInfo> examInfoList=new ArrayList<>();
String fileName = "E:\\项目汇总\\选择题录入模板" + ".xlsx";
EasyExcel.read(fileName, ExamMsg.class, new ExamListener(examInfos -> {
for (ExamInfo examInfo : examInfos) {
examInfoList.add(examInfo);
}
})).sheet().doRead();
System.out.println(examInfoList);
}
}
控制台打印读取数据:
[ExamInfo{answer='新冠肺炎起源于哪一年?', stem='A', analyse='经验得知', singleScore=2,
eaxmChoiceInfoList=[EaxmChoiceInfo{choiceType='A', optionContent='2019'},
EaxmChoiceInfo{choiceType='B', optionContent='2022'},
EaxmChoiceInfo{choiceType='C', optionContent='2021'},
EaxmChoiceInfo{choiceType='D', optionContent='2003'}]},
ExamInfo{answer='刘备的最好的朋友是谁?', stem='B', analyse='猜的', singleScore=2,
eaxmChoiceInfoList=[EaxmChoiceInfo{choiceType='A', optionContent='关羽'},
EaxmChoiceInfo{choiceType='B', optionContent='诸葛亮'},
EaxmChoiceInfo{choiceType='C', optionContent='张飞'}, EaxmChoiceInfo{choiceType='D', optionContent='曹操'}]}]
以上为处理合并单元格文件读取的实现方案,如果对你有帮助欢迎点赞收藏!
更多easy excel实战文章:
Easy excel实战:读取文件自定义转化器不生效问题处理方案
EasyExcel实战:实现导入文件参数校验并记录异常信息