本文目录
一、Controller
二、Service 接口类
三、ServiceImpl 实现类
四、Content-Type 类型与MIME Type类型对照表
最近在做一个word导出功能,需求非常简单,只需要将内容导出到word文件里即可,对于word的格式并没有要求。功能不复杂,想快速实现这个功能,但是使用POI的话,比较麻烦。本文记录一下通过一个工具类即可实现简单的word导出的功能,上一篇文章讲的是通过这个工具类实现服用在本地,word文档下载到本地的功能。本文将会实现项目部署到服务器上,然后通过导出或者下载功能保存到本地电脑。
项目架构:Springboot + mybatis-plus + MySQL + Maven
添加依赖,同上一篇文章:【Java用法】使用Java导出word文档的解决方案(适用于Windows电脑)
直接上代码喽:
一、Controller
@ApiOperation(value = "根据examId导出word文档")
@RequestMapping(value = "/word", method = )
public void word(@RequestBody ExamExportDTO examExportDTO, HttpServletResponse response) {
// 写出到客户端下载(即写出到Servlet)
Word07Writer word07Writer = (examExportDTO);
// response是HttpServletResponse对象
("application/;charset=utf-8");
// 是弹出下载对话框的文件名,不能为中文,中文请自行编码
("Content-Disposition", "attachment;filename=");
ServletOutputStream out = null;
try {
out = ();
} catch (IOException e) {
("根据examId导出word文档失败!", e);
}
// 写出到目标流
(out);
// 关闭writer,释放内存
();
// 关闭输出Servlet流
(out);
}
二、Service 接口类
/**
* 试卷导出word文档
*
* @param examExportDTO 试卷id和是否导出答案
* @return 当前文件导出的路径位置
*/
Word07Writer word(ExamExportDTO examExportDTO);
三、ServiceImpl 实现类
@Override
public Word07Writer word(ExamExportDTO examExportDTO) {
Integer examId = ();
Integer isExportAnswer = ();
Word07Writer word07Writer = new Word07Writer();
// 1、得到当前试卷信息
Exam exam = (examId);
// 1.1 保存试卷名称
(new Font("方正小标宋简体", , 22), "试卷名称:" + ());
// 2、得到当前试卷的题目列表
List<Integer> quIds = (examId);
// 题目列表用
int count = 1;
for (Integer quId : quIds) {
// 3、得到当前题目信息
Question question = (quId);
// 4、保存到文件中:题目 格式为( 题目2. 多选题测试(20分))
(new Font("宋体", , 15),
"题目" + (count++) + ". " + () + "(" + (()) + "分)");
// 5、得到每个题目的选项列表
List<QuAnswerDTO> quAnswerDTOList = (quId);
// 6、答案和解析
StringBuilder answers = new StringBuilder();
// 题目类型 1 单选题 2 多选题 3 判断题 4 简答题 5 填空题
Integer quType = ();
for (QuAnswerDTO quAnswerDTO : quAnswerDTOList) {
// 添加题目选项
(new Font("宋体", , 10),
() + "、" + ());
if (isExportAnswer == Constants.NUMBER_ONE) {
// 得到题目选项的答案 1 正确答案,0 错误答案
Integer isRight = ();
String dtoAbc = ();
// 得到正确答案
(quType, isRight, answers, dtoAbc);
}
}
if (isExportAnswer == Constants.NUMBER_ONE) {
// 7、得到答案并保存到文件中
(new Font("宋体", , 10), "答案:" + answers);
// 8、得到题目整体解析
String analysis = ();
(new Font("宋体", , 10),
"解析:" + ((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) {
(dtoAbc);
}
if (quType == Constants.QU_TYPE_MULTI && isRight == Constants.NUMBER_ONE) {
(dtoAbc);
}
if (quType == Constants.QU_TYPE_JUDGE && isRight == Constants.NUMBER_ONE) {
(dtoAbc);
}
if (quType == Constants.QU_TYPE_SHORT && isRight == Constants.NUMBER_ONE) {
(dtoAbc);
}
if (quType == Constants.QU_TYPE_FILL && isRight == Constants.NUMBER_ONE) {
(dtoAbc);
}
}
说明:与第一篇不同的是,将数据以流的形式通过Response返回给前端即可。浏览器会根据Response中的 content-type 类型去自动识别,然后进行下载或者保存的。
四、Content-Type 类型与MIME Type类型对照表
附:Content-Type 类型与MIME Type类型对照表(当然这只是 office 所有后缀的,还有其他类型,如音频、视频、图像、Html,Css等)
后缀 | MIME Type |
---|---|
.doc | application/msword |
.dot | application/msword |
.docx | application/ |
.dotx | application/ |
.docm | application/.12 |
.dotm | application/.12 |
.xls | application/-excel |
.xlt | application/-excel |
.xla | application/-excel |
.xlsx | application/ |
.xltx | application/ |
.xlsm | application/.12 |
.xltm | application/.12 |
.xlam | application/.12 |
.xlsb | application/.12 |
.ppt | application/-powerpoint |
.pot | application/-powerpoint |
.pps | application/-powerpoint |
.ppa | application/-powerpoint |
.pptx | application/ |
.potx | application/ |
.ppsx | application/ |
.ppam | application/.12 |
.pptm | application/.12 |
.potm | application/.12 |
.ppsm | application/.12 |
完结!