欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

spring boot实现自动输出word文档功能的实例代码

程序员文章站 2022-03-17 22:41:00
spring boot实现自动输出word文档功能本文用到apache poi组件组件依赖在pom.xml文件中添加

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;

// 选择题实体
@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; //题目解析
}

创建好要用到的实体类之后,利用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底层查询后,需要创建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);
}
@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{
    //由于题目应于考试信息对应 所以需要先查出考试信息后根据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();
        }
    }

    /**
     * 发送响应流方法
     */
    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文档内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!