Springboot怎么实现自动输出word文档功能
发表于:2025-11-15 作者:千家信息网编辑
千家信息网最后更新 2025年11月15日,这篇文章主要介绍了Springboot怎么实现自动输出word文档功能,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。spring bo
千家信息网最后更新 2025年11月15日Springboot怎么实现自动输出word文档功能
这篇文章主要介绍了Springboot怎么实现自动输出word文档功能,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
spring boot实现自动输出word文档功能
本文用到Apache POI组件
组件依赖在pom.xml文件中添加
org.apache.poi poi 4.1.0 org.apache.poi poi-ooxml 4.1.0 首先创建相关的实体类、编写需要用到的sql查询。
import lombok.Data;// 选择题实体@Datapublic class MultiQuestion { private Integer questionId; private String subject; private String section; private String answerA; private String answerB; private String answerC; private String answerD; private String question; private String level; private String rightAnswer; private String analysis; //题目解析 private Integer score; }import lombok.Data;//填空题实体类@Datapublic class FillQuestion { private Integer questionId; private String subject; private String question; private String answer; private Integer score; private String level; private String section; private String analysis; //题目解析 }import lombok.Data;//判断题实体类@Datapublic class JudgeQuestion { private Integer questionId; private String subject; private String question; private String answer; private String level; private String section; private Integer score; private String analysis; //题目解析}创建好要用到的实体类之后,利用mybatis写sql查询,可以分为两种:1、配置mapper.xml文件路径,在xml文件中编写sql语句。2、直接使用注解。本文使用方法为第二种。
@Mapperpublic interface MultiQuestionMapper { /** * select * from multiquestions where questionId in ( * select questionId from papermanage where questionType = 1 and paperId = 1001 * ) */ @Select("select * from multi_question where questionId in (select questionId from paper_manage where questionType = 1 and paperId = #{paperId})") List findByIdAndType(Integer PaperId); @Select("select * from multi_question") IPage findAll(Page page); /** * 查询最后一条记录的questionId * @return MultiQuestion */ @Select("select questionId from multi_question order by questionId desc limit 1") MultiQuestion findOnlyQuestionId(); @Options(useGeneratedKeys = true,keyProperty = "questionId") @Insert("insert into multi_question(subject,question,answerA,answerB,answerC,answerD,rightAnswer,analysis,section,level) " + "values(#{subject},#{question},#{answerA},#{answerB},#{answerC},#{answerD},#{rightAnswer},#{analysis},#{section},#{level})") int add(MultiQuestion multiQuestion); @Select("select questionId from multi_question where subject =#{subject} order by rand() desc limit #{pageNo}") List findBySubject(String subject,Integer pageNo);} //填空题@Mapperpublic interface FillQuestionMapper { @Select("select * from fill_question where questionId in (select questionId from paper_manage where questionType = 2 and paperId = #{paperId})") List findByIdAndType(Integer paperId); @Select("select * from fill_question") IPage findAll(Page page); /** * 查询最后一条questionId * @return FillQuestion */ @Select("select questionId from fill_question order by questionId desc limit 1") FillQuestion findOnlyQuestionId(); @Options(useGeneratedKeys = true,keyProperty ="questionId" ) @Insert("insert into fill_question(subject,question,answer,analysis,level,section) values " + "(#{subject,},#{question},#{answer},#{analysis},#{level},#{section})") int add(FillQuestion fillQuestion); @Select("select questionId from fill_question where subject = #{subject} order by rand() desc limit #{pageNo}") List findBySubject(String subject,Integer pageNo);} //判断题@Mapperpublic interface JudgeQuestionMapper { @Select("select * from judge_question where questionId in (select questionId from paper_manage where questionType = 3 and paperId = #{paperId})") List findByIdAndType(Integer paperId); @Select("select * from judge_question") IPage findAll(Page page); /** * 查询最后一条记录的questionId * @return JudgeQuestion */ @Select("select questionId from judge_question order by questionId desc limit 1") JudgeQuestion findOnlyQuestionId(); @Insert("insert into judge_question(subject,question,answer,analysis,level,section) values " + "(#{subject},#{question},#{answer},#{analysis},#{level},#{section})") int add(JudgeQuestion judgeQuestion); @Select("select questionId from judge_question where subject=#{subject} order by rand() desc limit #{pageNo}") List findBySubject(String subject,Integer pageNo);} 写好mapper底层查询后,需要创建service及其实现类来调用mapper底层。例如:
public interface JudgeQuestionService { List findByIdAndType(Integer paperId); IPage findAll(Page page); JudgeQuestion findOnlyQuestionId(); int add(JudgeQuestion judgeQuestion); List findBySubject(String subject,Integer pageNo);} @Servicepublic class JudgeQuestionServiceImpl implements JudgeQuestionService { @Autowired private JudgeQuestionMapper judgeQuestionMapper; @Override public List findByIdAndType(Integer paperId) { return judgeQuestionMapper.findByIdAndType(paperId); } @Override public IPage findAll(Page page) { return judgeQuestionMapper.findAll(page); } @Override public JudgeQuestion findOnlyQuestionId() { return judgeQuestionMapper.findOnlyQuestionId(); } @Override public int add(JudgeQuestion judgeQuestion) { return judgeQuestionMapper.add(judgeQuestion); } @Override public List findBySubject(String subject, Integer pageNo) { return judgeQuestionMapper.findBySubject(subject,pageNo); }} 最后将输出文件方法写在controller层:
@RequestMapping("/exam/exportWord") public void exportWord(int examCode, HttpServletResponse response) throws FileNotFoundException{ //由于题目应于考试信息对应 所以需要先查出考试信息后根据pageId来查找对应的组卷信息 ExamManage res = examManageService.findById(examCode); int paperId = res.getPaperId(); List multiQuestionRes = multiQuestionService.findByIdAndType(paperId); //选择题题库 1 List fillQuestionsRes = fillQuestionService.findByIdAndType(paperId); //填空题题库 2 List judgeQuestionRes = judgeQuestionService.findByIdAndType(paperId); //响应到客户端 XWPFDocument document= new XWPFDocument(); //分页 XWPFParagraph firstParagraph = document.createParagraph(); //格式化段落 firstParagraph.getStyleID(); XWPFRun run = firstParagraph.createRun(); int i = 1; run.setText("一、选择题" + ""); //换行 for (MultiQuestion multiQuestion : multiQuestionRes) { String str = multiQuestion.getQuestion(); String str1 = multiQuestion.getAnswerA(); String str2 = multiQuestion.getAnswerB(); String str3 = multiQuestion.getAnswerC(); String str4 = multiQuestion.getAnswerD(); run.setText(i + ". " + str + ""); run.setText("A. " + str1 + ""); run.setText("B. " + str2 + ""); run.setText("C. " + str3 + ""); run.setText("D. " + str4 + ""); i++; } run.setText("二、填空题" + ""); for (FillQuestion fillQuestion : fillQuestionsRes) { String str = fillQuestion.getQuestion(); run.setText(i + ". " + str + ""); i++; } run.setText("三、判断题" + ""); for (JudgeQuestion judgeQuestion : judgeQuestionRes) { String str = judgeQuestion.getQuestion(); run.setText(i + ". " + str + ""); i++; } document.createTOC(); try { //设置相应头 this.setResponseHeader(response, res.getSource() + "试卷.doc"); //输出流 OutputStream os = response.getOutputStream(); [xss_clean](os); os.flush(); os.close(); } catch (Exception e) { e.printStackTrace(); } } /** * 发送响应流方法 */ private void setResponseHeader(HttpServletResponse response, String fileName) { try { try { fileName = URLEncoder.encode(fileName, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } response.setContentType("application/octet-stream;charset=UTF-8"); response.setHeader("Content-Disposition", "attachment;filename="+ fileName); //遵守缓存规定 response.addHeader("Pargam", "no-cache"); response.addHeader("Cache-Control", "no-cache"); } catch (Exception ex) { ex.printStackTrace(); } } 感谢你能够认真阅读完这篇文章,希望小编分享的"Springboot怎么实现自动输出word文档功能"这篇文章对大家有帮助,同时也希望大家多多支持,关注行业资讯频道,更多相关知识等着你来学习!
查询
输出
实体
文件
篇文章
题目
功能
文档
信息
方法
选择题
选择
底层
组件
题库
考试
价值
使用方法
兴趣
同时
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
5G网络安全与智慧广电
u8安装以及数据库配置
网络安全重大事项报告表
个人防护服务器图片
网络安全场景介绍
长沙畅想网络技术工作室
如何远程检测服务器关机完成没有
维护网络安全成效显著
内蒙古智慧城管软件开发
常用数据库的安全措施有哪些
数据库怎么删除两个表的数据
小学网络安全知识讲座简报
联网控制的软件开发
山西省法律法规数据库
信息系统需要的软件开发
csgo搭建服务器valve
h3c服务器g3管理口ip配置
软件开发协议信息安全条款
软件与网络安全
广东超市电商软件开发多少钱
华为 服务器拒绝下载
加强网络安全保护的主体
广州软件开发哪家专业
谈一谈新一代数据库技术
sql 附加数据库只读
宏观经济数据库招标
蜜芽宝贝北京网络技术有限公司
南理工 网络安全学院
服务器ssd贵吗
数据库管理员薪水