首页 > 其他分享 >考试题库通过excel导入试题

考试题库通过excel导入试题

时间:2023-11-16 14:22:16浏览次数:30  
标签:String ExcelProperty excel value ApiModelProperty 导入 private import 题库

ps:最近在做的考试试题导入,留个痕

本人有点菜,实现方式有些简单,各位有什么优化也可以拿去改,0.0;

这个方法可以获取对象试题字段的字段值 和字段名 并且可以根据字段上的注解判断字段是否要处理 有类似的业务还是可以用用的

一.demo测试代码:


public static void main(String[] args) {
AssetsApply assetsApply = new AssetsApply();
Class<?> clazz = assetsApply.getClass();
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
String fieldName = field.getName();
if (field.isAnnotationPresent(ApiModelProperty.class) && fieldName.contains("ageName")) {
field.setAccessible(true);
try {
String fieldValue = (String) field.get(assetsApply);
//字段值不为空
if (StringUtils.isNotBlank(fieldName)) {
System.out.println("fieldName = " + fieldName);
System.out.println("fieldValue = " + fieldValue);
String last = fieldName.substring(fieldName.length() - 1);
System.out.println("last = " + last);
ArrayList<String> strList = new ArrayList<>();
strList.add("a");
System.out.println("strList.contains(last) = " + strList.contains(last));
strList.add("b");
System.out.println("strList.contains(last) = " + strList.contains(last));
strList.add("e");
System.out.println("strList.contains(last) = " + strList.contains(last));
strList.add("c");
System.out.println("strList.contains(last) = " + strList.contains(last));
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
























二.业务逻辑代码
我个人主要是使用的easyExcel加上hutu和ruoyi的工具类直接做的比较方便
1.service代码
/**
* 模板导入试题
* @param file 导入excel文件
* @return
*/
@Override
@Transactional(rollbackFor = Exception.class)
public Boolean importQuestions(MultipartFile file, String repoId) {
Assert.isTrue(!file.isEmpty(), "导入数据为空");
importQuestionsListener.setQuestions(new LinkedList<>());
importQuestionsListener.setRepoId(repoId);
String filename = file.getOriginalFilename();
// 判断文件类型
Assert.isTrue(StringUtils.endsWith(filename, "xlsx") || StringUtils.endsWith(filename, "xls"),
"只支持.xlsx或者.xls类型文件导入");
try {
EasyExcel.read(file.getInputStream(), ExamQuestionDto.class, importQuestionsListener)
.sheet().doRead();
return true;
} catch (IOException e) {
return false;
}
}













2.easyExcel的监听器


package com.zy.learn.exam.listener;

import cn.hutool.core.util.IdUtil;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.zy.common.dto.learn.exam.ExamQuestionDto;
import com.zy.common.entity.learn.exam.ExamAnswer;
import com.zy.common.entity.learn.exam.ExamQuestion;
import com.zy.common.enums.learn.exam.ExamQuestionCategoryEnum;
import com.zy.common.utils.StringUtils;
import com.zy.learn.exam.service.ExamAnswerService;
import com.zy.learn.exam.service.ExamQuestionService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* 导入试题监听器
*
* @Name ImportQuestionslistener
* @Author hhx
* @Date 2023-11-15 10:28
*/
@Slf4j
@Component
@Scope("prototype")
@RequiredArgsConstructor
public class ImportQuestionsListener extends AnalysisEventListener<ExamQuestionDto> {

private final ExamQuestionService questionService;

private final ExamAnswerService answerService;

private List<ExamQuestion> questions;
private String repoId;

public void setQuestions(List<ExamQuestion> questions) {
this.questions = questions;
}

public List<ExamQuestion> getQuestions() {
return questions;
}

public void setRepoId(String repoId) {
this.repoId = repoId;
}

public String getRepoId() {
return repoId;
}

public static Map<String, Object> map = new HashMap<>();


/**
* 读取
*
* @param questionDto
* @param analysisContext
*/
@Override
@Transactional(rollbackFor = Exception.class)
public void invoke(ExamQuestionDto questionDto, AnalysisContext analysisContext) {
ExamQuestion question = new ExamQuestion();
//处理读取到的答案
List<ExamAnswer> answers = handAnswers(questionDto);
//先都不给问题和答案id
question.setCategory(ExamQuestionCategoryEnum.getByValue(questionDto.getCategory()).getCategory())
.setText(questionDto.getQuestionText())
.setRemark(questionDto.getRemark())
.setAnswers(answers)
.setRepoId(getRepoId());
this.questions.add(question);
}


/**
* 执行完毕
* 保存试卷
*
* @param analysisContext
*/
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
//1.准备答案集合
ArrayList<ExamAnswer> examAnswers = new ArrayList<>();
//2.处理问题/答案 完善实体
questions.stream().forEach(question -> {
String questionId = IdUtil.getSnowflakeNextIdStr();
question.setQuestionId(questionId);
List<ExamAnswer> answers = question.getAnswers();
if (answers.size() > 0) {
answers.stream().forEach(answer -> {
answer.setAnswerId(IdUtil.getSnowflakeNextIdStr())
.setQuestionId(questionId);
});
examAnswers.addAll(answers);
}

});

//3.保存
//保存试题
questionService.saveBatch(questions);
//保存答案
answerService.saveBatch(examAnswers);


//执行完毕后,清空数据
setQuestions(null);
setRepoId(null);

log.info("试题导入成功");
}

/**
* 根据读取数据生成答案list
*
* @param questionDto
* @return
*/
private List<ExamAnswer> handAnswers(ExamQuestionDto questionDto) {
//1.准备答案集合
ArrayList<ExamAnswer> examAnswers = new ArrayList<>();
//正确答案集合
String correctOption = questionDto.getCorrectOption();
//2.通过反射获取对象字段相关信息
Class<?> clazz = questionDto.getClass();
Field[] fields = clazz.getDeclaredFields();

for (Field field : fields) {
ExamAnswer answer = new ExamAnswer();
String filedName = field.getName();
//3.读取字段中带option 且有easyExcel注解的
if (field.isAnnotationPresent(ExcelProperty.class) && filedName.contains("option")) {
//4.满足上述条件修改字段为可操作
field.setAccessible(true);
try {
String fieldValue = (String) field.get(questionDto);
//5.字段值不为空
if (StringUtils.isNotBlank(fieldValue)) {
//6.封装实体对象
//获取选项是A,B,C.....中的哪个 我的字段名optionA,B,C....
String option = filedName.substring(filedName.length() - 1);
//6.1选项ABC字段
answer.setAbc(option);
//6.2 是否正确答案 选项A,B,C在正确答案集合correctOption内为正确答案
if (correctOption.contains(option)) {
answer.setIsRight(1);
}
//6.3 答案文本
answer.setText(fieldValue);
examAnswers.add(answer);
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
return examAnswers;
}


}































































































































3.导入的模板和实体

 

 @Data
public class ExamQuestionDto {

@ExcelProperty(value = "题干", index = 1)
private String questionText;

@ExcelProperty(value = "试题类型", index = 2)
private String category;

@ExcelProperty(value = "正确答案", index = 3)
private String correctOption;

@ExcelProperty(value = "题解", index = 4)
private String remark;

@ExcelProperty(value = "选项A", index = 5)
private String optionA;

@ExcelProperty(value = "选项B", index = 6)
private String optionB;

@ExcelProperty(value = "选项C", index = 7)
private String optionC;

@ExcelProperty(value = "选项D", index = 8)
private String optionD;

@ExcelProperty(value = "选项E", index = 9)
private String optionE;

@ExcelProperty(value = "选项F", index = 10)
private String optionF;

@ExcelProperty(value = "选项G", index = 11)
private String optionG;

@ExcelProperty(value = "选项H", index = 12)
private String optionH;

@ExcelProperty(value = "选项I", index = 13)
private String optionI;

@ExcelProperty(value = "选项J", index = 14)
private String optionJ;

@ExcelProperty(value = "选项K", index = 15)
private String optionK;

@ExcelProperty(value = "选项L", index = 16)
private String optionL;

@ExcelProperty(value = "选项M", index = 17)
private String optionM;

@ExcelProperty(value = "选项N", index = 18)
private String optionN;

@ExcelProperty(value = "选项O", index = 19)
private String optionO;


}
















































4.数据库表实体

@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("t_learn_exam_answer")
@ApiModel(value="ExamAnswer对象", description="学习考试答案表")
public class ExamAnswer extends BaseEntity {

private static final long serialVersionUID = 1L;

@ApiModelProperty(value = "答案id")
@TableId(value = "answer_id", type = IdType.ASSIGN_ID)
private String answerId;

@ApiModelProperty(value = "答案内容")
@TableField("text")
private String text;

@ApiModelProperty(value = "是否是正确答案(0否/1是)")
@TableField("is_right")
private Integer isRight;

@ApiModelProperty(value = "问题id")
@TableField("question_id")
private String questionId;

@ApiModelProperty(value = "选项(A,B,C....)")
@TableField("abc")
private String abc;

@ApiModelProperty(value = "答案图片url")
@TableField("url")
private String url;

@ApiModelProperty(value = "删除标志(0否/1是)")
@TableField("del_flag")
private Integer delFlag;

@ApiModelProperty(value = "所属公司(备用字段)")
@TableField("company")
private String company;
}
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("t_learn_exam_question")
@ApiModel(value="ExamQuestion对象", description="学习考试试题表")
public class ExamQuestion extends BaseEntity {

private static final long serialVersionUID = 1L;

@ApiModelProperty(value = "试题id")
@TableId(value = "question_id", type = IdType.ASSIGN_ID)
private String questionId;

@ApiModelProperty(value = "题库id")
@TableField("repo_id")
@NotBlank
private String repoId;

@ApiModelProperty(value = "题干")
@TableField("text")
@NotBlank
private String text;

@ApiModelProperty(value = "试题类型(0单选题/1多选题/2判断题)")
@TableField("category")
private Integer category;

@ApiModelProperty(value = "答案要点")
@TableField("remark")
private String remark;

@ApiModelProperty(value = "试题图片附件url")
@TableField("url")
private String url;

@ApiModelProperty(value = "删除标志(0未删除/1已删除)")
@TableField("del_flag")
private Integer delFlag;

@ApiModelProperty(value = "所属公司(备用字段)")
@TableField("company")
private String company;

@ApiModelProperty(value = "答案集合")
@TableField(exist = false)
private List<ExamAnswer> answers;

@ApiModelProperty(value = "正确答案")
@TableField(exist = false)
private String correctOption;

@ApiModelProperty(value = "题库名称")
@TableField(exist = false)
private String repoName;


}








































标签:String,ExcelProperty,excel,value,ApiModelProperty,导入,private,import,题库
From: https://www.cnblogs.com/hhx07007/p/17834070.html

相关文章

  • ReportViewer (RDLC)导出Excel或PDF无法打开问题
    WebForms的ReportViewer提供了导出成EXCEL或者PDF的报表。(ReportingServices)但是导出时报表会另存为.xls[1]或者.pdf[1],导致文件无法直接打开。这需要给ReportViewer设定一个DisplayName解决。ReportViewer1.LocalReport.DisplayName......
  • excel公式 提取文件路径
    =SUBSTITUTE(LEFT(@CELL("filename",A1),FIND("[",@CELL("filename",A1))-1),"[","")=SUBSTITUTE(LEFT(@CELL("filename",A1),FIND("[",@CELL("filename",A1))-1),"[","&quo......
  • DJango 域账号信息批量导入后台
    DJango域账号信息批量导入后台从域账号导入用户信息,因为配置了ldap用户密码,所以执行脚本将用户批量导入DJango管理后台即可。执行脚本 pythonmanage.pyldap_sync_users 注意:导入的每个用户需要单独的设置权限......
  • Golang把文件写到excel
    最近有个需求是把看广告的日志转成excelpackagemainimport( "bufio" "encoding/json" "flag" "fmt" "github.com/xuri/excelize/v2" "os" "time")//Ad广告typeAdstruct{ OpenIdstring`json:&quo......
  • Python读取pdf、word、excel、ppt、csv和txt文件提取所有文本
    前言本文对使用python读取pdf、word、excel、ppt、csv、txt等常用文件,并提取所有文本的方法进行分享和使用总结。可以读取不同文件的库和方法当然不止下面分享的这些,本文的代码主要目标都是:方便提取文件中所有文本的实现方式。这些库的更多使用方法,请到官方文档中查阅。读取PD......
  • 做数据分析,我们需要懂多少excel知识?
    数据分析所需的Excel知识详解在进行数据分析工作时,Excel是一个非常常用且强大的数据处理工具。以下是数据分析中常用的Excel知识点和技巧的详细描述。1.基本操作在使用Excel进行数据分析之前,首先需要掌握Excel的基本操作,包括单元格的选择、复制粘贴、插入行列、删除等。这些......
  • 打造基于Excel表格数据驱动系统
    策划喜欢用Excel,里面有很多计算公式,非常方便,策划写好的数据,程序手写到代码里面,每次修改比较麻烦,所以我们做一个模块,能自动将Excel数据转成程序能直接使用的数据。每次更改数据后,程序很快就能使用起来。1:定义一个Excel的格式让策划和程序沟通    程序要解析Excel,......
  • 抖音私信群发工具cookie,批量导入UID安全码,易语言谷歌模版开源
    用精易浏览器的谷歌模版开发的工具,最主要的是可以多账号登录抖音号,而且可以导入COOKIE,也能提取cookie,就实现了一种多账号私信的效果,可以搭配代理IP效果的,这个我没加入,我就分享下源码,开源就行了,软件基础都设计好了,可以实现的功能就是可以多账号登录【cookie写入本地txt分割】然后导......
  • Excel文件导入
    前端<divclass="panel-body"><spanclass="glyphiconglyphicon-th-list"aria-hidden="true"></span>批量上传</div>后端defdepart_multi(request): '''导入Excel文件''......
  • python tkinter treeview 仿 excel表格
    代码:fromtkinterimportttkfromtkinterimport*root=Tk()#初始框的声明columns=("姓名","IP地址")treeview=ttk.Treeview(root,height=18,show="headings",columns=columns)#表格treeview.column("姓名",width=100,a......