赞
踩
本文目录
四、Content-Type 类型与MIME Type类型对照表
最近在做一个word导出功能,需求非常简单,只需要将内容导出到word文件里即可,对于word的格式并没有要求。功能不复杂,想快速实现这个功能,但是使用POI的话,比较麻烦。本文记录一下通过一个工具类即可实现简单的word导出的功能,上一篇文章讲的是通过这个工具类实现服用在本地,word文档下载到本地的功能。本文将会实现项目部署到服务器上,然后通过导出或者下载功能保存到本地电脑。
项目架构:Springboot + mybatis-plus + MySQL + Maven
添加依赖,同上一篇文章:【Java用法】使用Java导出word文档的解决方案(适用于Windows电脑)
直接上代码喽:
- @ApiOperation(value = "根据examId导出word文档")
- @RequestMapping(value = "/word", method = RequestMethod.POST)
- public void word(@RequestBody ExamExportDTO examExportDTO, HttpServletResponse response) {
-
- // 写出到客户端下载(即写出到Servlet)
- Word07Writer word07Writer = examService.word(examExportDTO);
-
- // response是HttpServletResponse对象
- response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document;charset=utf-8");
- // test.docx是弹出下载对话框的文件名,不能为中文,中文请自行编码
- response.setHeader("Content-Disposition", "attachment;filename=test.docx");
-
- ServletOutputStream out = null;
- try {
- out = response.getOutputStream();
- } catch (IOException e) {
- log.error("根据examId导出word文档失败!", e);
- }
-
- // 写出到目标流
- word07Writer.flush(out);
- // 关闭writer,释放内存
- word07Writer.close();
- // 关闭输出Servlet流
- IoUtil.close(out);
- }

- /**
- * 试卷导出word文档
- *
- * @param examExportDTO 试卷id和是否导出答案
- * @return 当前文件导出的路径位置
- */
- Word07Writer word(ExamExportDTO examExportDTO);
- @Override
- public Word07Writer word(ExamExportDTO examExportDTO) {
-
- Integer examId = examExportDTO.getExamId();
- Integer isExportAnswer = examExportDTO.getIsExportAnswer();
-
- Word07Writer word07Writer = new Word07Writer();
-
- // 1、得到当前试卷信息
- Exam exam = this.getById(examId);
- // 1.1 保存试卷名称
- word07Writer.addText(new Font("方正小标宋简体", Font.PLAIN, 22), "试卷名称:" + exam.getTitle());
-
- // 2、得到当前试卷的题目列表
- List<Integer> quIds = quExamService.listByExamId(examId);
- // 题目列表用
- int count = 1;
- for (Integer quId : quIds) {
-
- // 3、得到当前题目信息
- Question question = questionMapper.selectById(quId);
-
- // 4、保存到文件中:题目 格式为( 题目2. 多选题测试(20分))
- word07Writer.addText(new Font("宋体", Font.PLAIN, 15),
- "题目" + (count++) + ". " + question.getContent() + "(" + (question.getQuScore()) + "分)");
-
- // 5、得到每个题目的选项列表
- List<QuAnswerDTO> quAnswerDTOList = quAnswerService.listByQu(quId);
-
- // 6、答案和解析
- StringBuilder answers = new StringBuilder();
- // 题目类型 1 单选题 2 多选题 3 判断题 4 简答题 5 填空题
- Integer quType = question.getQuType();
- for (QuAnswerDTO quAnswerDTO : quAnswerDTOList) {
- // 添加题目选项
- word07Writer.addText(new Font("宋体", Font.PLAIN, 10),
- quAnswerDTO.getAbc() + "、" + quAnswerDTO.getContent());
-
- if (isExportAnswer == Constants.NUMBER_ONE) {
- // 得到题目选项的答案 1 正确答案,0 错误答案
- Integer isRight = quAnswerDTO.getIsRight();
- String dtoAbc = quAnswerDTO.getAbc();
-
- // 得到正确答案
- this.getAnswers(quType, isRight, answers, dtoAbc);
- }
- }
-
- if (isExportAnswer == Constants.NUMBER_ONE) {
- // 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));
- }
- }
-
- return word07Writer;
- }
-
- /**
- * 得到答案
- *
- * @param quType 题目类型 1 单选题 2 多选题 3 判断题 4 简答题 5 填空题
- * @param isRight 是否为正确答案 1 正确答案,0 错误答案
- * @param answers 得到的答案
- * @param dtoAbc 选项ABC
- */
- private void getAnswers(Integer quType, Integer isRight, StringBuilder answers, String dtoAbc) {
-
- if (quType == Constants.QU_TYPE_SINGLE && isRight == Constants.NUMBER_ONE) {
- answers.append(dtoAbc);
- }
- if (quType == Constants.QU_TYPE_MULTI && isRight == Constants.NUMBER_ONE) {
- answers.append(dtoAbc);
- }
- if (quType == Constants.QU_TYPE_JUDGE && isRight == Constants.NUMBER_ONE) {
- answers.append(dtoAbc);
- }
- if (quType == Constants.QU_TYPE_SHORT && isRight == Constants.NUMBER_ONE) {
- answers.append(dtoAbc);
- }
- if (quType == Constants.QU_TYPE_FILL && isRight == Constants.NUMBER_ONE) {
- answers.append(dtoAbc);
- }
- }

说明:与第一篇不同的是,将数据以流的形式通过Response返回给前端即可。浏览器会根据Response中的 content-type 类型去自动识别,然后进行下载或者保存的。
附:Content-Type 类型与MIME Type类型对照表(当然这只是 office 所有后缀的,还有其他类型,如音频、视频、图像、Html,Css等)
| 后缀 | MIME Type |
|---|---|
| .doc | application/msword |
| .dot | application/msword |
| .docx | application/vnd.openxmlformats-officedocument.wordprocessingml.document |
| .dotx | application/vnd.openxmlformats-officedocument.wordprocessingml.template |
| .docm | application/vnd.ms-word.document.macroEnabled.12 |
| .dotm | application/vnd.ms-word.template.macroEnabled.12 |
| .xls | application/vnd.ms-excel |
| .xlt | application/vnd.ms-excel |
| .xla | application/vnd.ms-excel |
| .xlsx | application/vnd.openxmlformats-officedocument.spreadsheetml.sheet |
| .xltx | application/vnd.openxmlformats-officedocument.spreadsheetml.template |
| .xlsm | application/vnd.ms-excel.sheet.macroEnabled.12 |
| .xltm | application/vnd.ms-excel.template.macroEnabled.12 |
| .xlam | application/vnd.ms-excel.addin.macroEnabled.12 |
| .xlsb | application/vnd.ms-excel.sheet.binary.macroEnabled.12 |
| .ppt | application/vnd.ms-powerpoint |
| .pot | application/vnd.ms-powerpoint |
| .pps | application/vnd.ms-powerpoint |
| .ppa | application/vnd.ms-powerpoint |
| .pptx | application/vnd.openxmlformats-officedocument.presentationml.presentation |
| .potx | application/vnd.openxmlformats-officedocument.presentationml.template |
| .ppsx | application/vnd.openxmlformats-officedocument.presentationml.slideshow |
| .ppam | application/vnd.ms-powerpoint.addin.macroEnabled.12 |
| .pptm | application/vnd.ms-powerpoint.presentation.macroEnabled.12 |
| .potm | application/vnd.ms-powerpoint.presentation.macroEnabled.12 |
| .ppsm | application/vnd.ms-powerpoint.slideshow.macroEnabled.12 |
完结!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。