java实现导入word模板导入试题

最近有一个项目需要将一个word文档中的试题数据导入,并存储到数据库中。试题类型包括:单选题、多选题、判断题、填空题、简答题。支持图片导入(我的这篇是借鉴JAVA实现Excel、Word模板导入 - JAVA - 华仔部落,java poi 解析上传word试卷(题库管理系统) - 爱码网)这两位大神的。

废话不多说,进入正题,直接上代码。

一、word模板

3476f8cb4ce6405686589c21db8f6a19.png

要求:

1)、题目间隔:上一题与下一题之间必须换行,开始下一题;

2)、试题编辑的样式:[单选题][答案][章][节][考点][难度] [解析]是固定样式,不能删除。 如果试题无[解析],无[章][节][考点][难度],可以不填。

二、代码:

1、引入依赖

   
       org.apache.poi
       poi-scratchpad
       4.1.1
   
   
        org.apache.poi
        poi-ooxml
        4.1.1
   

2、创建VO对象TestQuestionsImportVo和TestQuestionsImportWordVo

/**
 * 试题导入对应字段
 */
@Data
public class TestQuestionsImportVo implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 试题类型1=单选2=多选3=判断4=填空5=简答,6=不定项,7=材料题
     */
    private Integer type;
    /**
     * 题目名称
     */
    private String name;
    /**
     * 答案
     */
    private String answer;

    /**
     * 选项A
     */
    private String optionA;

    /**
     * 选项B
     */
    private String optionB;

    /**
     * 选项C
     */
    private String optionC;

    /**
     * 选项D
     */
    private String optionD;

    /**
     * 选项E
     */
    private String optionE;

    /**
     * 选项F
     */
    private String optionF;

    /**
     * 选项G
     */
    private String optionG;

    /**
     * 选项H
     */
    private String optionH;

    /**
     * 选项I
     */
    private String optionI;

    /**
     * 选项J
     */
    private String optionJ;

    /**
     * 选项K
     */
    private String optionK;

    /**
     * 选项L
     */
    private String optionL;

    /**
     * 文字解析
     */
    private String analysis;

    /**
     * 难度,1=简单,2=一般,3=困难
     */
    private String difficulty;

    /**
     * 题库章名称
     */
    private String qbChapName;

    /**
     * 题库节名称
     */
    private String qbNodeName;

    /**
     * 题库考点名称
     */
    private String qbExsiName;
}
/**
 * 试题导入对应字段
 */
@Data
public class TestQuestionsImportWordVo extends TestQuestionsImportVo implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 材料题字试题
     */
    private List children;

    /**
     * 题目名称图片
     */
    private List questionNameXwpfPicture;

    /**
     * 选项A图片
     */
    private List optionXwpfPictureA;

    /**
     * 选项B图片
     */
    private List optionXwpfPictureB;

    /**
     * 选项C图片
     */
    private List optionXwpfPictureC;

    /**
     * 选项D图片
     */
    private List optionXwpfPictureD;

    /**
     * 选项E图片
     */
    private List optionXwpfPictureE;

    /**
     * 选项F图片
     */
    private List optionXwpfPictureF;

    /**
     * 选项G图片
     */
    private List optionXwpfPictureG;

    /**
     * 选项H图片
     */
    private List optionXwpfPictureH;

    /**
     * 选项I图片
     */
    private List optionXwpfPictureI;

    /**
     * 选项J图片
     */
    private List optionXwpfPictureJ;

    /**
     * 选项K图片
     */
    private List optionXwpfPictureK;

    /**
     * 选项L图片
     */
    private List optionXwpfPictureL;

}

3、word解析试题核心代码

/**
 * word导入处理
 */
public class ImportWordParagraphHandleUtil {

    // 该正则用来匹配一个大题(例如:二、多选题)
    private static String bigQuestionRegex = "([一|二|三|四|五|六|七|八|九|十]{1,3})([、.]{1})([\\u4E00-\\u9FA5\\s]+题)";
    // 该正则用来匹配一个选项(例如:A\B\C\D)
    private static String optionRegex = "([A|B|C|D|E|F|G|H|I|J|K|L]{1,3})";

    /**
     * 解析导入题目word模板
     *
     * @param file
     */
    public static List analysisImportQuestionSubjectWord(MultipartFile file) throws IOException {
        List questionsImportWordVoList = new ArrayList<>();
        // 获取文件流
        InputStream fileInputStream = file.getInputStream();
        // 解析文档
        XWPFDocument xd = new XWPFDocument(fileInputStream);
        // 获取全部的文本段落
        List xwPfParagraphList = xd.getParagraphs();
        // 过滤掉 大题(例如:二、多选题) 开头的段落
        xwPfParagraphList = xwPfParagraphList.stream().filter(xwpfParagraph -> !Pattern.compile(bigQuestionRegex).matcher(getObjectValue(xwpfParagraph.getText())).find()).collect(Collectors.toList());
        // 题目段落列表
        List> subjectParagraphList = generateSubjectParagraphList(xwPfParagraphList);
        if (CollectionUtil.isEmpty(subjectParagraphList)) {
            return questionsImportWordVoList;
        }
        for (List xwpfParagraphs : subjectParagraphList) {
            // 解析考试题目保存对象根据段落列表
            TestQuestionsImportWordVo testQuestionsImportWordVo = analysisQuestionsSubjectByParagraph(xwpfParagraphs);
            questionsImportWordVoList.add(testQuestionsImportWordVo);
        }
        return questionsImportWordVoList;
    }

    /**
     * 按照题目生成 段落列表
     * 每个题目一个段落列表
     *
     * @param xwPfParagraphLis
     * @return
     */
    private static List> generateSubjectParagraphList(List xwPfParagraphLis) {
        List> list = new ArrayList<>();
        if (CollectionUtil.isEmpty(xwPfParagraphLis)) {
            return list;
        }
        boolean isStart = false;
        list.add(new ArrayList());
        for (int i = 9; i < xwPfParagraphLis.size(); i++) {
            // 当前项
            XWPFParagraph xwPfParagraphLi = xwPfParagraphLis.get(i);
            // 获取文本内容
            String text = xwPfParagraphLi.getText();
            //获取图片内容
            List pictures = StrUtil.isEmpty(text) ? CollectionUtil.isNotEmpty(xwPfParagraphLi.getRuns()) ? xwPfParagraphLi.getRuns().get(0).getEmbeddedPictures() : null : null;
            // 以题目类型开头 算做起始段落
            if (MapUtil.isNotEmpty(getQuestionTypeMap(text))) {
                isStart = true;
            }
            // 开始标志 且 当前是空行
            if (isStart && StrUtil.isBlank(text) && CollectionUtil.isEmpty(pictures)) {
                // 获取最后一个题目段落列表
                List last = list.get(list.size() - 1);
                // 段落列表不为空
                if (last.size() > 0) {
                    // 算做结束段落
                    isStart = false;
                    // 空行不是最后一行时
                    if (i < xwPfParagraphLis.size() - 1) {
                        // 继续添加段落列表
                        list.add(new ArrayList());
                    }
                }
            }
            // 处于开始标志时
            if (isStart) {
                // 获取最后一个题目段落列表
                List last = list.get(list.size() - 1);
                // 为空处理
                last = CollectionUtil.isEmpty(last) ? new ArrayList<>() : last;
                // 将当前段落添加到列表中
                last.add(xwPfParagraphLi);
                list.set(list.size() - 1, last);
            }
        }
        if (CollectionUtil.isNotEmpty(list)) {
            // 如果最后一项是空的,就删除最后一项
            List tempList = list.get(list.size() - 1);
            if (tempList.size() == 0) {
                list.remove(list.size() - 1);
            }
        }

        return list;
    }

    /**
     * 解析考试题目保存对象根据段落列表
     *
     * @param xwpfParagraphs
     * @return
     */
    private static TestQuestionsImportWordVo analysisQuestionsSubjectByParagraph(List xwpfParagraphs) {
        // 创建题目保存对象
        TestQuestionsImportWordVo testQuestionsImportWordVo = new TestQuestionsImportWordVo();

       // 调用方法获取解析对象
      List questionsImportWordVoList = generateQuestionSubjectSaveDTOList(xwpfParagraphs);
     if (CollectionUtil.isNotEmpty(questionsImportWordVoList)) {
          testQuestionsImportWordVo = questionsImportWordVoList.get(0);
      }

        return testQuestionsImportWordVo;
    }

    /**
     * 生成题目保存对象列表
     *
     * @return
     */
    @SneakyThrows
    private static List generateQuestionSubjectSaveDTOList(List xwpfParagraphs) {
        List questionsImportWordVoList = new ArrayList<>();
        if (CollectionUtil.isEmpty(xwpfParagraphs)) {
            return questionsImportWordVoList;
        }
        boolean typeFlag = false;
        boolean contentFlag = false;
        boolean optionsFlag = false;
        for (int i = 0; i < xwpfParagraphs.size(); i++) {
            XWPFParagraph xwpfParagraph = xwpfParagraphs.get(i);
            if (ObjectUtil.isEmpty(xwpfParagraph)) {
                continue;
            }
            // 读取当前段落内容
            String text = getObjectValue(xwpfParagraph.getText()).trim();
            //试题类型1=单选2=多选3=判断4=填空5=简答
            Integer questionType = null;
            //题目名称
            String questionName = null;
            //题目名称图片
            List questionNameXwpfPicture = null;
            //试题选项
            String questionOption = null;
            //试题答案
            String questionAnswer = null;
            //试题章
            String questionChapter = null;
            //试题节
            String questionNode = null;
            //考点
            String questionExam = null;
            //难度
            String questionDifficulty = null;
            //解析
            String questionAnalysis = null;
            //保存的试题类
            TestQuestionsImportWordVo questionsImportWordVo = null;
            if (MapUtil.isNotEmpty(getQuestionTypeMap(text))) {
                questionsImportWordVo = new TestQuestionsImportWordVo();
                //处理试题类型
                questionType = Integer.valueOf(getQuestionTypeMap(text).get("questionType").toString());
                questionName = getQuestionTypeMap(text).get("content").toString();
                List xwpfPictureList = getXwpfPictureList(xwpfParagraph);
                questionNameXwpfPicture = CollectionUtil.isNotEmpty(xwpfPictureList) ? xwpfPictureList : new ArrayList<>();
                i++;
                //如果标签不是以[答案]或选项A.\A、结尾,该题目为多行一题
                if (questionType.intValue() == 5) {
                    while (!(xwpfParagraphs.get(i).getText().contains(ImportWordQuestionTypeEnum.ANSWER.getDesc()))) {
                        questionName += getObjectValue(xwpfParagraphs.get(i).getText());
                        if (CollectionUtil.isNotEmpty(getXwpfPictureList(xwpfParagraphs.get(i)))) {
                            questionNameXwpfPicture.addAll(getXwpfPictureList(xwpfParagraphs.get(i)));
                        }
                        i++;
                    }
                } else {
                    while (!(xwpfParagraphs.get(i).getText().contains("A"))) {
                        questionName += getObjectValue(xwpfParagraphs.get(i).getText());
                        if (CollectionUtil.isNotEmpty(getXwpfPictureList(xwpfParagraphs.get(i)))) {
                            questionNameXwpfPicture.addAll(getXwpfPictureList(xwpfParagraphs.get(i)));
                        }
                        i++;
                    }
                }

                typeFlag = true;
                contentFlag = true;
            }
            // 获取选项部分
            questionOption = getObjectValue(xwpfParagraphs.get(i).getText());
            if (typeFlag && contentFlag && !optionsFlag && questionOption.contains("A")) {
                // 处理选项
                List xwpfPictureList = getXwpfPictureList(xwpfParagraphs.get(i));
                if (CollectionUtil.isNotEmpty(xwpfPictureList)) {
                    questionsImportWordVo.setOptionXwpfPictureA(xwpfPictureList);
                }
                i++;
                //如果标签不是以[答案]结尾,该试题选项为多行
                while (!xwpfParagraphs.get(i).getText().contains(ImportWordQuestionTypeEnum.ANSWER.getDesc())) {
                    questionOption += getObjectValue(xwpfParagraphs.get(i).getText());
                    //匹配的模式
                    Matcher m = Pattern.compile(optionRegex).matcher(xwpfParagraphs.get(i).getText());
                    if (m.find()) {
                        Method method = TestQuestionsImportWordVo.class.getMethod("setOptionXwpfPicture" + m.group(1), List.class);
                        method.invoke(questionsImportWordVo, getXwpfPictureList(xwpfParagraphs.get(i)));
                    }
                    i++;
                }
            }
            // 获取回答部分
            questionAnswer = getObjectValue(xwpfParagraphs.get(i).getText()).trim();
            if (questionAnswer.startsWith(ImportWordQuestionTypeEnum.ANSWER.getDesc())) {
                // 处理答案
                questionAnswer = getStrContent(questionAnswer, ImportWordQuestionTypeEnum.ANSWER.getDesc()).trim();
                i++;
                //如果标签不是以[章]结尾,该答案为多行
                while (!xwpfParagraphs.get(i).getText().contains(ImportWordQuestionTypeEnum.CHAPTER.getDesc())) {
                    questionAnswer += getObjectValue(xwpfParagraphs.get(i).getText());
                    i++;
                }
                optionsFlag = true;
            }
            // 获取章部分
            questionChapter = getObjectValue(xwpfParagraphs.get(i).getText()).trim();
            if (questionChapter.startsWith(ImportWordQuestionTypeEnum.CHAPTER.getDesc())) {
                // 处理章
                questionChapter = getStrContent(questionChapter, ImportWordQuestionTypeEnum.CHAPTER.getDesc()).trim();
                i++;
                //如果标签不是以[节]结尾,该章为多行
                while (!xwpfParagraphs.get(i).getText().contains(ImportWordQuestionTypeEnum.NODE.getDesc())) {
                    questionChapter += getObjectValue(xwpfParagraphs.get(i).getText());
                    i++;
                }
                optionsFlag = true;
            }
            // 获取节部分
            questionNode = getObjectValue(xwpfParagraphs.get(i).getText()).trim();
            if (questionNode.startsWith(ImportWordQuestionTypeEnum.NODE.getDesc())) {
                // 处理节
                questionNode = getStrContent(questionNode, ImportWordQuestionTypeEnum.NODE.getDesc()).trim();
                i++;
                //如果标签不是以[考点]结尾,该节为多行
                while (!xwpfParagraphs.get(i).getText().contains(ImportWordQuestionTypeEnum.EXAMINATION.getDesc())) {
                    questionNode += getObjectValue(xwpfParagraphs.get(i).getText());
                    i++;
                }
                optionsFlag = true;
            }
            // 获取考点部分
            questionExam = getObjectValue(xwpfParagraphs.get(i).getText()).trim();
            if (questionExam.startsWith(ImportWordQuestionTypeEnum.EXAMINATION.getDesc())) {
                // 处理考点
                questionExam = getStrContent(questionExam, ImportWordQuestionTypeEnum.EXAMINATION.getDesc()).trim();
                i++;
                //如果标签不是以[难度]结尾,该考点为多行
                while (!xwpfParagraphs.get(i).getText().contains(ImportWordQuestionTypeEnum.DIFFICULTY.getDesc())) {
                    questionExam += getObjectValue(xwpfParagraphs.get(i).getText());
                    i++;
                }
                optionsFlag = true;
            }
            // 获取难度部分
            questionDifficulty = getObjectValue(xwpfParagraphs.get(i).getText()).trim();
            if (questionDifficulty.startsWith(ImportWordQuestionTypeEnum.DIFFICULTY.getDesc())) {
                // 处理难度
                questionDifficulty = getStrContent(questionDifficulty, ImportWordQuestionTypeEnum.DIFFICULTY.getDesc()).trim();
                i++;
                optionsFlag = true;
            }
            // 获取解析部分
            questionAnalysis = getObjectValue(xwpfParagraphs.get(i).getText()).trim();
            if (questionAnalysis.startsWith(ImportWordQuestionTypeEnum.ANALYSIS.getDesc())) {
                // 处理解析
                questionAnalysis = getStrContent(questionAnalysis, ImportWordQuestionTypeEnum.ANALYSIS.getDesc()).trim();
                i++;
                //如果不是最后一行,该解析为多行
                while (i != xwpfParagraphs.size()) {
                    questionAnalysis += getObjectValue(xwpfParagraphs.get(i).getText());
                    i++;
                }
                // 恢复完成标志
                typeFlag = false;
                contentFlag = false;
                optionsFlag = false;
            }
            if (ObjectUtil.isNotEmpty(questionType) && StrUtil.isNotBlank(questionName)) {
                questionsImportWordVo.setType(questionType);
                questionsImportWordVo.setName(questionName);
                questionsImportWordVo.setAnswer(questionAnswer);
                questionsImportWordVo.setAnalysis(questionAnalysis);
                questionsImportWordVo.setDifficulty(questionDifficulty);
                questionsImportWordVo.setQbChapName(questionChapter);
                questionsImportWordVo.setQbNodeName(questionNode);
                questionsImportWordVo.setQbExsiName(questionExam);
                if (CollectionUtil.isNotEmpty(questionNameXwpfPicture)) {
                    questionsImportWordVo.setQuestionNameXwpfPicture(questionNameXwpfPicture);
                }
                //试题类型1=单选2=多选3=判断4=填空5=简答
                if (questionType == 1 || questionType == 2 || questionType == 4 || questionType == 6) {
                    //处理选项
                    questionsImportWordVo = divideOption(questionsImportWordVo, questionOption);
                }
                questionsImportWordVoList.add(questionsImportWordVo);
            }
        }
        return questionsImportWordVoList;
    }

    /**
     * 将选项分隔出来
     *
     * @param questionsImportWordVo
     * @param questionOption        选项
     * @return
     */
    @SneakyThrows
    private static TestQuestionsImportWordVo divideOption(TestQuestionsImportWordVo questionsImportWordVo, String questionOption) {
        if (StrUtil.isNotBlank(questionOption)) {
            //选项,如:A,B,C
            String[] optionName = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"};
            for (int i = 0; i < optionName.length; i++) {
                String option = getOption(questionOption, optionName[i], optionName[i + 1]);
                if (StrUtil.isBlank(option)) {
                    break;
                }
                Method method = TestQuestionsImportWordVo.class.getMethod("setOption" + optionName[i], String.class);
                method.invoke(questionsImportWordVo, option);
            }
        }
        return questionsImportWordVo;
    }

    //判空,如果等于空返回空字符串
    private static String getObjectValue(String value) {
        return StrUtil.isBlank(value) ? "" : value;
    }

    //获取内容
    private static String getStrContent(String content, String questionTypeDesc) {
        return getObjectValue(content.substring(content.indexOf(questionTypeDesc) + questionTypeDesc.length()));
    }

    //获取图片
    private static List getXwpfPictureList(XWPFParagraph xwpfParagraph) {
        //图片
        List resultXwpfPictureList = new ArrayList<>();
        List runs = xwpfParagraph.getRuns();
        for (XWPFRun run : runs) {
            //判断是否存在图片,获取图片并上传
            if (CollectionUtil.isNotEmpty(run.getEmbeddedPictures())) {
                //获取图片
                List xwpfPictureList = run.getEmbeddedPictures();
                if (CollectionUtil.isNotEmpty(xwpfPictureList)) {
                    for (XWPFPicture xwpfPicture : xwpfPictureList) {
                        //获取图片类型
                        String fileExtension = xwpfPicture.getPictureData().suggestFileExtension();
                        //获取图片名称
                        String fileName = xwpfPicture.getPictureData().getFileName();
                        System.out.println("图片类型:" + fileExtension);
                        System.out.println("图片名称:" + fileName);
                    }
                    resultXwpfPictureList.addAll(xwpfPictureList);
                }
            }
        }
        return resultXwpfPictureList;
    }

    /**
     * 获取选项
     *
     * @param content 内容
     * @param begin   开始条件
     * @param end     结束条件
     * @return
     */
    private static String getOption(String content, String begin, String end) {
        if (content.contains(end)) {
            return content.substring(content.indexOf(begin), content.indexOf(end));
        } else if (content.contains(begin)) {
            return content.substring(content.indexOf(begin));
        } else {
            return null;
        }
    }

    //获取试题类型
    private static Map getQuestionTypeMap(String text) {
        Map resultMap = new HashMap<>();
        if (text.contains(ImportWordQuestionTypeEnum.SINGLE_CHOICE_QUESTION.getDesc())) {
            resultMap.put("questionType", ImportWordQuestionTypeEnum.SINGLE_CHOICE_QUESTION.getCode());
            resultMap.put("content", text.substring(text.indexOf(ImportWordQuestionTypeEnum.SINGLE_CHOICE_QUESTION.getDesc()) + ImportWordQuestionTypeEnum.SINGLE_CHOICE_QUESTION.getDesc().length()));
            return resultMap;
        } else if (text.contains(ImportWordQuestionTypeEnum.MULTIPLE_CHOICE_QUESTION.getDesc())) {
            resultMap.put("questionType", ImportWordQuestionTypeEnum.MULTIPLE_CHOICE_QUESTION.getCode());
            resultMap.put("content", text.substring(text.indexOf(ImportWordQuestionTypeEnum.MULTIPLE_CHOICE_QUESTION.getDesc()) + ImportWordQuestionTypeEnum.MULTIPLE_CHOICE_QUESTION.getDesc().length()));
            return resultMap;
        } else if (text.contains(ImportWordQuestionTypeEnum.JUDGE_QUESTION.getDesc())) {
            resultMap.put("questionType", ImportWordQuestionTypeEnum.JUDGE_QUESTION.getCode());
            resultMap.put("content", text.substring(text.indexOf(ImportWordQuestionTypeEnum.JUDGE_QUESTION.getDesc()) + ImportWordQuestionTypeEnum.JUDGE_QUESTION.getDesc().length()));
            return resultMap;
        } else if (text.contains(ImportWordQuestionTypeEnum.GAP_FILLING_QUESTION.getDesc())) {
            resultMap.put("questionType", ImportWordQuestionTypeEnum.GAP_FILLING_QUESTION.getCode());
            resultMap.put("content", text.substring(text.indexOf(ImportWordQuestionTypeEnum.GAP_FILLING_QUESTION.getDesc()) + ImportWordQuestionTypeEnum.GAP_FILLING_QUESTION.getDesc().length()));
            return resultMap;
        } else if (text.contains(ImportWordQuestionTypeEnum.QA_QUESTION.getDesc())) {
            resultMap.put("questionType", ImportWordQuestionTypeEnum.QA_QUESTION.getCode());
            resultMap.put("content", text.substring(text.indexOf(ImportWordQuestionTypeEnum.QA_QUESTION.getDesc()) + ImportWordQuestionTypeEnum.QA_QUESTION.getDesc().length()));
            return resultMap;
        } else if (text.contains(ImportWordQuestionTypeEnum.INDEFINITE_TERM_QUESTION.getDesc())) {
            resultMap.put("questionType", ImportWordQuestionTypeEnum.INDEFINITE_TERM_QUESTION.getCode());
            resultMap.put("content", text.substring(text.indexOf(ImportWordQuestionTypeEnum.INDEFINITE_TERM_QUESTION.getDesc()) + ImportWordQuestionTypeEnum.INDEFINITE_TERM_QUESTION.getDesc().length()));
            return resultMap;
        } else if (text.contains(ImportWordQuestionTypeEnum.MATERIAL_QUESTION.getDesc())) {
            resultMap.put("questionType", ImportWordQuestionTypeEnum.MATERIAL_QUESTION.getCode());
            resultMap.put("content", text.substring(text.indexOf(ImportWordQuestionTypeEnum.MATERIAL_QUESTION.getDesc()) + ImportWordQuestionTypeEnum.MATERIAL_QUESTION.getDesc().length()));
            return resultMap;
        } else {
            return null;
        }
    }
}

4、service层代码:

 public Result importWordTestQuestions(TestQuestionsImportDto testQuestionsImportDto) {
        try {
            List questionsImportWordVoList = ImportWordParagraphHandleUtil.analysisImportQuestionSubjectWord(testQuestionsImportDto.getFile());
            if (CollectionUtil.isEmpty(questionsImportWordVoList)) {
                return Result.failed(ResultCode.SUBJECT_NOT_EXIST);
            }
            //试题类型为空数量
            long typeIsEmptyCount = questionsImportWordVoList.stream().filter(testQuestionsImportVo -> ObjectUtil.isEmpty(testQuestionsImportVo.getType())).count();
            if (typeIsEmptyCount > 0) {
                throw new BaseException(ResultCode.SUBJECT_TYPE_IS_EMPTY);
            }
            //存储试题id列表
            List subjectIdList = Lists.newArrayList();
            questionsImportWordVoList.stream().forEach(importWord -> {
                Subject subject = new Subject();
                StringBuffer sbf = new StringBuffer();
                //题目名称
                sbf.append(importWord.getName());
                if (CollectionUtil.isNotEmpty(importWord.getQuestionNameXwpfPicture())) {
                    for (XWPFPicture xwpfPicture : importWord.getQuestionNameXwpfPicture()) {
                        XWPFPictureData pictureData = xwpfPicture.getPictureData();
                        try {
                            MockMultipartFile multipartFile = new MockMultipartFile(pictureData.getFileName(), pictureData.getFileName(), ContentType.APPLICATION_OCTET_STREAM.toString(), pictureData.getPackagePart().getInputStream());
                            //oss上传图片
                            String excelUrl = uploadOSSService.uploadFile(multipartFile, 1);
                            System.out.println("试题图片地址===" + excelUrl);
                            sbf.append(";");
                            sbf.append(excelUrl);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
                subject.setName(sbf.toString());
                subject.setType(importWord.getType());
                //试题类型1=单选2=多选3=判断4=填空5=简答
                if (importWord.getType().intValue() == 2 || importWord.getType().intValue() == 6) {
                    String answers = importWord.getAnswer().chars().mapToObj(b -> (char) b).map(answer -> String.valueOf(answer).toUpperCase()).collect(Collectors.joining(","));
                    subject.setAnswer(answers);
                } else if (importWord.getType().intValue() == 4) {
                    StringBuffer sbu = new StringBuffer();
                    for (int i = 0; i < optionName.length; i++) {
                        try {
                            Method method = importWord.getClass().getMethod("getOption" + optionName[i]);
                            Object o = method.invoke(importWord);
                            if (ObjectUtil.isEmpty(o)) {
                                break;
                            }
                            sbu.append(o.toString().substring(o.toString().indexOf(optionName[i]) + 2)).append(";");
                        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
                            e.printStackTrace();
                        }
                    }
                    subject.setAnswer(sbu.substring(0, sbu.length() - 1));
                } else {
                    subject.setAnswer(importWord.getAnswer());
                }
                if (StrUtil.isNotBlank(importWord.getAnalysis())) {
                    subject.setAnalysis(importWord.getAnalysis());
                }

                if (StrUtil.isNotBlank(importWord.getDifficulty())) {
                    if ("简单".equals(importWord.getDifficulty())) {
                        subject.setDifficulty(1);
                    } else if ("一般".equals(importWord.getDifficulty())) {
                        subject.setDifficulty(2);
                    } else if ("困难".equals(importWord.getDifficulty())) {
                        subject.setDifficulty(3);
                    }
                }

                subject.setReviewerTime(LocalDateTime.now());
                subject.setCreateTime(LocalDateTime.now());
                subject.setUpdateTime(LocalDateTime.now());
                int count = baseMapper.insert(subject);
                if (count > 0) {
                    subjectIdList.add(subject.getId());
                    //添加试题选项
                    //试题类型1=单选2=多选3=判断4=填空5=简答
                    if (subject.getType() == 1 || subject.getType() == 2 || subject.getType() == 6) {
                        List

5、controller代码:

 /**
     * word导入试题
     *
     * @param file    上传文件
     * @return
     */
    @PostMapping("/importWordTestQuestions")
    public Result importWordTestQuestions(MultipartFile file) {
        String filename = file.getOriginalFilename();
        if (StrUtil.isEmpty(filename)) {
            return Result.failed(ResultCode.PARAM_IS_NULL);
        }
        try {
            TestQuestionsImportDto testQuestionsImportDto = new TestQuestionsImportDto();
            testQuestionsImportDto.setFile(file);
            return subjectService.importWordTestQuestions(testQuestionsImportDto);
        } catch (BaseException e) {
            e.printStackTrace();
            return Result.failed(e.getResultCode());
        }
    }

至此代码完毕,还是有很多需要改进的,希望大神评论指点。

你可能感兴趣的:(java实现导入word模板导入试题)