亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

spring boot實現自動輸出word文檔功能的實例代碼

瀏覽:133日期:2022-06-24 14:20:25

spring boot實現自動輸出word文檔功能

本文用到Apache POI組件組件依賴在pom.xml文件中添加

<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.0</version></dependency><dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.0</version></dependency>

首先創建相關的實體類、編寫需要用到的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<MultiQuestion> findByIdAndType(Integer PaperId); @Select('select * from multi_question') IPage<MultiQuestion> 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<Integer> 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<FillQuestion> findByIdAndType(Integer paperId); @Select('select * from fill_question') IPage<FillQuestion> 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<Integer> 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<JudgeQuestion> findByIdAndType(Integer paperId); @Select('select * from judge_question') IPage<JudgeQuestion> 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<Integer> findBySubject(String subject,Integer pageNo);}

寫好mapper底層查詢后,需要創建service及其實現類來調用mapper底層。例如:

public interface JudgeQuestionService { List<JudgeQuestion> findByIdAndType(Integer paperId); IPage<JudgeQuestion> findAll(Page<JudgeQuestion> page); JudgeQuestion findOnlyQuestionId(); int add(JudgeQuestion judgeQuestion); List<Integer> findBySubject(String subject,Integer pageNo);}

@Servicepublic class JudgeQuestionServiceImpl implements JudgeQuestionService { @Autowired private JudgeQuestionMapper judgeQuestionMapper; @Override public List<JudgeQuestion> findByIdAndType(Integer paperId) {return judgeQuestionMapper.findByIdAndType(paperId); } @Override public IPage<JudgeQuestion> findAll(Page<JudgeQuestion> 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<Integer> 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<MultiQuestion> multiQuestionRes = multiQuestionService.findByIdAndType(paperId); //選擇題題庫 1List<FillQuestion> fillQuestionsRes = fillQuestionService.findByIdAndType(paperId); //填空題題庫 2List<JudgeQuestion> judgeQuestionRes = judgeQuestionService.findByIdAndType(paperId);//響應到客戶端XWPFDocument document= new XWPFDocument();//分頁XWPFParagraph firstParagraph = document.createParagraph();//格式化段落firstParagraph.getStyleID();XWPFRun run = firstParagraph.createRun();int i = 1;run.setText('一、選擇題' + 'rn'); //換行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 + 'rn'); run.setText('A. ' + str1 + 'rn'); run.setText('B. ' + str2 + 'rn'); run.setText('C. ' + str3 + 'rn'); run.setText('D. ' + str4 + 'rn'); i++;}run.setText('二、填空題' + 'rn');for (FillQuestion fillQuestion : fillQuestionsRes) { String str = fillQuestion.getQuestion(); run.setText(i + '. ' + str + 'rn'); i++;}run.setText('三、判斷題' + 'rn');for (JudgeQuestion judgeQuestion : judgeQuestionRes) { String str = judgeQuestion.getQuestion(); run.setText(i + '. ' + str + 'rn'); i++;}document.createTOC();try { //設置相應頭 this.setResponseHeader(response, res.getSource() + '試卷.doc'); //輸出流 OutputStream os = response.getOutputStream(); document.write(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();} }

效果:

spring boot實現自動輸出word文檔功能的實例代碼

到此這篇關于spring boot實現自動輸出word文檔功能的文章就介紹到這了,更多相關spring boot自動輸出word文檔內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: word
相關文章:
主站蜘蛛池模板: 亚洲精品午夜aaa级久久久久 | 国产精品国产三级国产专不∫ | 香蕉国产人午夜视频在线 | 欧美区日韩区 | 在线欧美v日韩v国产精品v | 亚洲精品欧美日韩 | 亚洲综合区小说区激情区噜噜 | 91国内视频在线观看 | 欧美一级毛片特黄大 | 黄色美女免费 | 午夜性色一区二区三区不卡视频 | a级粗大硬长爽猛视频免费 a级精品国产片在线观看 | 亚洲欧美激情综合首页 | 亚洲第一综合网站 | 亚洲黄色网址 | 亚洲国产精品婷婷久久久久 | 国产视频91在线 | 国产日韩久久久精品影院首页 | xxxxxx日本护士 | 日韩一级a毛片欧美一级 | 国产丰满主播丝袜勾搭秀 | 久久久免费的精品 | 欧美一级大黄 | 成年人黄色大片大全 | 日韩一区二区精品久久高清 | 日韩黄色一级毛片 | 免费大黄网站在线观看 | 国产精品久久久久久久久齐齐 | 国产三级在线观看 | 国外精品视频在线观看免费 | 国内精自线一二区 | 国产精品久久现线拍久青草 | 猫咪人成免费网站在线观看 | 成人亚欧网站在线观看 | 91久久精品午夜一区二区 | 国产成人久久777777 | 一级特黄特黄的大片免费 | 日本mv精品中文字幕 | 国产亚洲精品国产 | 国产成人深夜福利短视频99 | 亚洲成人美女 |