In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces Springboot how to achieve automatic output word document function, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.
Spring boot realizes the function of automatically outputting word documents
Apache POI components are used in this article
Component dependency is added in the pom.xml file
Org.apache.poi poi 4.1.0 org.apache.poi poi-ooxml 4.1.0
First, create the relevant entity classes and write the sql queries you need.
Import lombok.Data;// multiple choice question entity @ 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; / / topic analysis private Integer score;} import lombok.Data;// fill in the blank question entity class @ 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; / / topic resolution} import lombok.Data;// judgment question entity class @ 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; / / topic resolution}
After creating the entity class to be used, writing the sql query using mybatis can be divided into two types: 1. Configure the path of the mapper.xml file and write the sql statement in the xml file. 2. Use annotations directly. The second method is used in this paper.
@ 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) / * query the questionId * @ return MultiQuestion * / @ Select ("select questionId from multi_question order by questionId desc limit 1") MultiQuestion findOnlyQuestionId () of the last record 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);} / / fill in the blank @ 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) / * query the last 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);} / / judgment question @ 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) / * query the questionId * @ return JudgeQuestion * / @ Select ("select questionId from judge_question order by questionId desc limit 1") JudgeQuestion findOnlyQuestionId () of the last record @ 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);}
After writing the mapper underlying query, you need to create a service and its implementation class to invoke the mapper underlying. For example:
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);}}
Finally, the output file method is written in the controller layer:
@ RequestMapping ("/ exam/exportWord") public void exportWord (int examCode, HttpServletResponse response) throws FileNotFoundException {/ / because the questions correspond to the test information, you need to find out the test information first and then look up the corresponding test paper information ExamManage res = examManageService.findById (examCode) according to pageId; int paperId = res.getPaperId (); List multiQuestionRes = multiQuestionService.findByIdAndType (paperId); / / Select question bank 1 List fillQuestionsRes = fillQuestionService.findByIdAndType (paperId) / / fill in the blank question bank 2 List judgeQuestionRes = judgeQuestionService.findByIdAndType (paperId); / / respond to client XWPFDocument document= new XWPFDocument (); / / paging XWPFParagraph firstParagraph = document.createParagraph (); / / format paragraph firstParagraph.getStyleID (); XWPFRun run = firstParagraph.createRun (); int I = 1; run.setText ("one, multiple choice" + ") / / newline 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 +"); iTunes;} run.setText (" 2. Fill in the blanks "+") For (FillQuestion fillQuestion: fillQuestionsRes) {String str = fillQuestion.getQuestion (); run.setText (I + ". "+ str +"); run.setText ("III. Judgment questions" + "); for (JudgeQuestion judgeQuestion: judgeQuestionRes) {String str = judgeQuestion.getQuestion (); run.setText (I +". "+ str +"); iTunes;} document.createTOC (); try {/ / set the corresponding header this.setResponseHeader (response, res.getSource () + "Test Paper .doc"); / / output stream OutputStream os = response.getOutputStream (); [xss_clean] (os); os.flush () Os.close ();} catch (Exception e) {e.printStackTrace ();}} / * send response flow method * / 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); / / comply with the cache rules response.addHeader ("Pargam", "no-cache") Response.addHeader ("Cache-Control", "no-cache");} catch (Exception ex) {ex.printStackTrace () }} Thank you for reading this article carefully. I hope the article "how to automatically output word documents from Springboot" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.