最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

spring boot實現(xiàn)自動輸出word文檔功能的實例代碼

 更新時間:2021年04月20日 14:44:01   作者:離人散  
這篇文章主要介紹了spring boot實現(xiàn)自動輸出word文檔功能的實例代碼,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

spring boot實現(xiàn)自動輸出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>

首先創(chuàng)建相關的實體類、編寫需要用到的sql查詢。

import lombok.Data;

// 選擇題實體
@Data
public 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;

//填空題實體類
@Data
public 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;

//判斷題實體類
@Data
public 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; //題目解析
}

創(chuàng)建好要用到的實體類之后,利用mybatis寫sql查詢,可以分為兩種:1、配置mapper.xml文件路徑,在xml文件中編寫sql語句。2、直接使用注解。本文使用方法為第二種。

@Mapper
public 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);


}
//填空題
@Mapper
public 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);
}
//判斷題

@Mapper
public 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底層查詢后,需要創(chuàng)建service及其實現(xiàn)類來調用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);
}
@Service
public 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{
    //由于題目應于考試信息對應 所以需要先查出考試信息后根據(jù)pageId來查找對應的組卷信息
        ExamManage res = examManageService.findById(examCode);
        int paperId = res.getPaperId();
        List<MultiQuestion> multiQuestionRes = multiQuestionService.findByIdAndType(paperId);   //選擇題題庫 1
        List<FillQuestion> fillQuestionsRes = fillQuestionService.findByIdAndType(paperId);     //填空題題庫 2
        List<JudgeQuestion> judgeQuestionRes = judgeQuestionService.findByIdAndType(paperId);
        //響應到客戶端
        XWPFDocument document= new XWPFDocument();
        //分頁
        XWPFParagraph firstParagraph = document.createParagraph();
        //格式化段落
        firstParagraph.getStyleID();
        XWPFRun run = firstParagraph.createRun();
        int i = 1;
        run.setText("一、選擇題" + "\r\n"); //換行
        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 + "\r\n");
            run.setText("A. " + str1 + "\r\n");
            run.setText("B. " + str2 + "\r\n");
            run.setText("C. " + str3 + "\r\n");
            run.setText("D. " + str4 + "\r\n");
            i++;
        }
        run.setText("二、填空題" + "\r\n");
        for (FillQuestion fillQuestion : fillQuestionsRes) {
            String str = fillQuestion.getQuestion();
            run.setText(i + ". " + str + "\r\n");
            i++;
        }
        run.setText("三、判斷題" + "\r\n");
        for (JudgeQuestion judgeQuestion : judgeQuestionRes) {
            String str = judgeQuestion.getQuestion();
            run.setText(i + ". " + str + "\r\n");
            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();
        }
    }

    /**
     * 發(fā)送響應流方法
     */
    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);
            //遵守緩存規(guī)定
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

效果:

在這里插入圖片描述

到此這篇關于spring boot實現(xiàn)自動輸出word文檔功能的文章就介紹到這了,更多相關spring boot自動輸出word文檔內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • SpringBoot日期格式轉換之配置全局日期格式轉換器的實例詳解

    SpringBoot日期格式轉換之配置全局日期格式轉換器的實例詳解

    這篇文章主要介紹了SpringBoot日期格式轉換之配置全局日期格式轉換器的實例詳解,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-12-12
  • springboot熱部署知識點總結

    springboot熱部署知識點總結

    在本篇文章里小編給大家整理了關于springboot熱部署的知識點內容,有興趣的朋友們參考學習下。
    2019-06-06
  • Springboot如何通過yml配置文件為靜態(tài)成員變量賦值

    Springboot如何通過yml配置文件為靜態(tài)成員變量賦值

    這篇文章主要介紹了Springboot如何通過yml配置文件為靜態(tài)成員變量賦值,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • 查看Java所支持的語言及相應的版本信息

    查看Java所支持的語言及相應的版本信息

    Java語言作為第一種支持國際化的語言,在Internet從一開始就具有其他語言無與倫比的國際化的本質特性,查看Java所支持的語言及相應的版本信息可以采用以下代碼進行查詢
    2014-01-01
  • Java Math.round(),Math.ceil(),Math.floor()的區(qū)別詳解

    Java Math.round(),Math.ceil(),Math.floor()的區(qū)別詳解

    這篇文章主要介紹了Java Math.round(),Math.ceil(),Math.floor()的區(qū)別詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-08-08
  • spring的jdbctemplate的crud的基類dao

    spring的jdbctemplate的crud的基類dao

    本文主要介紹了使用spring的jdbctemplate進行增刪改查的基類Dao的簡單寫法,需要的朋友可以參考下
    2014-02-02
  • Spring服務注解有哪些

    Spring服務注解有哪些

    這篇文章主要介紹了Spring服務注解,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2016-11-11
  • Java創(chuàng)建和填充PDF表單域方法

    Java創(chuàng)建和填充PDF表單域方法

    在本篇文章中小編給大家分享了關于Java創(chuàng)建和填充PDF表單域方法和步驟,有需要的朋友們學習下。
    2019-01-01
  • VsCode搭建Spring Boot項目并進行創(chuàng)建、運行、調試

    VsCode搭建Spring Boot項目并進行創(chuàng)建、運行、調試

    這篇文章主要介紹了VsCode搭建Spring Boot項目并進行創(chuàng)建、運行、調試 ,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-05-05
  • 詳解Java深拷貝,淺拷貝和Cloneable接口

    詳解Java深拷貝,淺拷貝和Cloneable接口

    這篇文章主要為大家詳細介紹了Java中Cloneable接口以及深拷貝與淺拷貝的相關知識,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-08-08

最新評論

扎兰屯市| 托克托县| 历史| 新乐市| 修文县| 兴海县| 乌鲁木齐县| 漯河市| 修水县| 乐至县| 临沂市| 克山县| 时尚| 和平县| 蓝山县| 云霄县| 垦利县| 余江县| 方正县| 镇原县| 庆阳市| 普安县| 蒙自县| 舒城县| 准格尔旗| 芷江| 凤冈县| 定襄县| 衡东县| 邢台县| 拜城县| 仪征市| 山东| 鹰潭市| 中宁县| 阿城市| 兴安县| 博客| 大庆市| 枣庄市| 仁寿县|