## 引包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);
}
}
java 导出word试题
于 2022-10-17 15:03:22 首次发布