当前位置:   article > 正文

java 导出word试题_word07writer

word07writer

## 引包POM

  <!-- https://mvnrepository.com/artifact/cn.hutool/hutool-all -->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>4.6.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.2</version>
        </dependency>

        <!--说明 hutool-4.x的poi-ooxml 版本需高于 3.17(别问我3.8版本为啥不行,
因为3.17 > 3.8 ) hutool-5.x的poi-ooxml 版本需高于 4.1.2 xercesImpl版本高于2.12.0-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.2</version>
        </dependency>

## ***

 - SpringBoot代码

***

/**
     * 查看考试详情
     * @return
     */
    @ApiOperation(value = "查找详情")
    @GetMapping("/test")
    public void test(HttpServletResponse response) throws IOException {
        word(null,response);
    }

    public void word(String paperId,HttpServletResponse response) throws IOException {
        paperId = "1579415877126451201";
        Word07Writer word07Writer = new Word07Writer();

        // 1、得到当前试卷信息
        Exam exam = baseService.getById("1572158462936948737");
        // 1.1 保存试卷名称
        word07Writer.addText(ParagraphAlignment.CENTER,new Font("方正小标宋简体", Font.PLAIN, 22), "试卷名称:" + exam.getTitle());
        // 查找题目列表
        List<com.yf.exam.modules.paper.dto.PaperQuDTO> list = paperQuService.listByPaper(paperId);
        // 添加段落(正文)
        word07Writer.addText(new Font("宋体", Font.PLAIN, 10), "一、单选题(共50题,每题1.00分)");
        List<com.yf.exam.modules.paper.dto.PaperQuDTO> radioList = list.stream().filter(item-> com.yf.exam.modules.qu.enums.QuType.RADIO.equals(item.getQuType())).collect(Collectors.toList());
        List<com.yf.exam.modules.paper.dto.PaperQuDTO> multiList = list.stream().filter(item-> com.yf.exam.modules.qu.enums.QuType.MULTI.equals(item.getQuType())).collect(Collectors.toList());
        List<com.yf.exam.modules.paper.dto.PaperQuDTO> judgeList = list.stream().filter(item-> com.yf.exam.modules.qu.enums.QuType.JUDGE.equals(item.getQuType())).collect(Collectors.toList());
        getWordType(paperId, word07Writer, radioList);
        word07Writer.addText(new Font("宋体", Font.PLAIN, 15), "二、多选题(共30题,每题1.00分)");
        getWordType(paperId, word07Writer, multiList);
        word07Writer.addText(new Font("宋体", Font.PLAIN, 15), "三、判断题(共20题,每题1.00分)");
        getWordType(paperId, word07Writer, judgeList);
        response.setContentType("application/msword");
        // 告诉浏览器,当前响应数据要求用户干预保存到文件中,以及文件名是什么 如果文件名有中文,必须URL编码
        response.setHeader( "Content-Disposition", "attachment;filename=" + URLEncoder.encode(String.valueOf(Math.round(Math.random() * 100)), "UTF-8"));
        response.setCharacterEncoding("utf-8");

        ServletOutputStream outputStream = response.getOutputStream();
        // 写出到文件(试卷名称.docx)
//        word07Writer.flush(FileUtil.file("C:\\Users\\detong\\Desktop\\" + Math.round(Math.random() * 100) + exam.getTitle() + ".docx"));
        word07Writer.flush(outputStream);
        // 关闭
        word07Writer.close();

    }

    private void getWordType(String paperId, Word07Writer word07Writer, List<com.yf.exam.modules.paper.dto.PaperQuDTO> list) {
        // 得到当前试卷的题目列表
        Integer count = 1;
        // 题目列表用
        for (com.yf.exam.modules.paper.dto.PaperQuDTO paperQuDTO : list) {

            // 3 得到当前题目信息
            Qu question = quService.getById(paperQuDTO.getQuId());
            //获取附件信息
            SysAttachment sysAttachment = new SysAttachment();
            sysAttachment.setBusinessId(question.getId());
            iSysAttachmentService.selectSysAttachmentList(sysAttachment);
            // 答案列表
            // 4、保存到文件中:格式为( 2. 多选题测试(20分))
            word07Writer.addText(new Font("宋体", Font.PLAIN, 15),
                    (count++) + ". " + question.getContent() + "(" + (paperQuDTO.getScore()) + "分)");
            // 5、得到每个题目的选项列表
            List<PaperQuAnswerExtDTO> quAnswerDTOList = paperQuAnswerService.listForExam(paperId, paperQuDTO.getQuId());
            // 6、答案和解析
            StringBuilder answers = new StringBuilder();
            // 题目类型 1 单选题 2 多选题 3 判断题 4 简答题 5 填空题
            Integer quType = question.getQuType();
            for (PaperQuAnswerExtDTO quAnswerDTO : quAnswerDTOList) {
                // 添加题目选项
                word07Writer.addText(new Font("宋体", Font.PLAIN, 10),
                        quAnswerDTO.getAbc() + "、" + quAnswerDTO.getContent());
                // 得到题目选项的答案 1 正确答案,0 错误答案
                Boolean isRight = quAnswerDTO.getIsRight();
                String dtoAbc = quAnswerDTO.getAbc();
                // 得到正确答案
                this.getAnswers(quType, isRight, answers, dtoAbc);
            }

            // 7、得到答案并保存到文件中
            word07Writer.addText(new Font("宋体", Font.PLAIN, 10), "答案:" + answers);
            // 8、得到题目整体解析
            String analysis = question.getAnalysis();
            word07Writer.addText(new Font("宋体", Font.PLAIN, 10),
                    "解析:" + (Objects.isNull(analysis) ? "暂无解析" : analysis));
        }
    }

    /**
     * 得到答案
     *
     * @param quType  题目类型 1 单选题 2 多选题 3 判断题 4 简答题 5 填空题
     * @param isRight 是否为正确答案 1 正确答案,0 错误答案
     * @param answers 得到的答案
     * @param dtoAbc  选项ABC
     */
    private void getAnswers(Integer quType, Boolean isRight, StringBuilder answers, String dtoAbc) {

        if (quType == 1 && isRight) {
            answers.append(dtoAbc);
        }
        if (quType == 2 && isRight) {
            answers.append(dtoAbc);
        }
        if (quType == 3 && isRight) {
            answers.append(dtoAbc);
        }
        if (quType == 4 && isRight) {
            answers.append(dtoAbc);
        }
        if (quType == 5 && isRight) {
            answers.append(dtoAbc);
        }
    } 


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/43561
推荐阅读
相关标签
  

闽ICP备14008679号