首页 > 其他分享 >thymeleaf+Springboot实现试卷导出为Word

thymeleaf+Springboot实现试卷导出为Word

时间:2024-06-18 18:01:16浏览次数:10  
标签:Font Word Springboot thymeleaf Paragraph new document public String

实习期间遇到了一个这样的需求,业务要求将试题详情进行导出。但是看到页面展示的试题详情后,发现是使用<div>等标签遍历的数据,这就需要在服务端将数据进行处理,以下为我的实现方式:

在html页面里,可以这样请求:

var url = "请求路径/方法名?"参数A="+参数A的值+"&参数B="+e参数B的值;
window.open(url, "批量导出试卷");

服务端代码如下:

有关文件操作的依赖:

<dependency>
            <groupId>com.lowagie</groupId>
            <artifactId>itext</artifactId>
            <version>2.0.8</version>
        </dependency>

controller层:

@GetMapping("/importExamPaper")
    public void importExamPaper(HttpServletResponse response,
                                @RequestParam(value = "simPaperID") Integer simPaperID,
                                @RequestParam(value = "userID") String userID,
                                @RequestParam(value = "courseName") String courseName,
                                @RequestParam(value = "identityCardId") String identityCardId,
                                @RequestParam(value = "clickTime") Date clickTime,
                                @RequestParam(value = "submitTime") Date submitTime,
                                @RequestParam(value = "spendTime") Integer spendTime,
                                @RequestParam(value = "memberName") String memberName){
        //做题记录详情(Comprehensive是你的实体类,记录考生信息和试卷信息)
        Comprehensive comprehensive = new Comprehensive();
        comprehensive.setSimPaperID(simPaperID);
        comprehensive.setUserID(userID);
        List<Comprehensive> list = XXXXService.getXXXDetail(comprehensive);
		//利用流式操作对 List<Comprehensive> 中的对象进行遍历,并为每个对象设置一个序号,序号从 1 开始递增。这样就可以为列表中的对象添加顺序信息,以便后续处理或展示时进行参考。
        AtomicInteger num = new AtomicInteger(1);
        list.stream().forEach(o->{
            o.setSequence(num.get());
            num.getAndIncrement();
        });

        try {
            //清空缓存
            response.reset();
            // 定义浏览器响应表头,并定义下载名
            String fileName = URLEncoder.encode(memberName+"做题详情.doc", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename="+fileName);
            //定义下载的类型,标明是word文件
            response.setContentType("application/msword;charset=UTF-8");
            //把创建好的word写入到输出流
            byte[] bytes1 = getExamPaperWordOutputBytes(memberName,identityCardId,courseName,clickTime,submitTime,spendTime,list);
            ByteArrayInputStream inStream = new ByteArrayInputStream(bytes1);
            // 循环取出流中的数据
            byte[] b = new byte[2048];
            int len;
            try (ServletOutputStream outStream = response.getOutputStream()) {
                while ((len = inStream.read(b)) > 0) {
                    outStream.write(b, 0, len);
                }
                outStream.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

调用方法:

/*试卷导出字节流*/
    public byte[] getExamPaperWordOutputBytes(String memberName, String identityCardId, String courseName, Date clickTime, Date submitTime, Integer spendTime, List<Comprehensive> list){
        byte[] outBytes=null;
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();//构建字节输出流
            Document document = new Document(PageSize.A4);  // 创建word文档,并设置纸张的大小
            RtfWriter2.getInstance(document, baos);
            //开始设置doc样式
            document.open();
            // 设置字体
            BaseFont font = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
            // 主标题字体风格
            Font titleFont = new Font(font, 20, Font.BOLD);
            // 副标题字体风格
            Font subTitleFont = new Font(font, 12, Font.BOLD);
            // 正文字体风格
            Font contextFont = new Font(font, 12, Font.NORMAL);
            //大标题
            Paragraph title = new Paragraph("试题详情");
            // 设置标题格式对齐方式
            title.setAlignment(Element.ALIGN_CENTER);
            title.setFont(titleFont);
            document.add(title);
            if (org.apache.commons.lang.StringUtils.isEmpty(identityCardId)) {
                identityCardId = " 暂无 ";
            }
            //日期格式转化
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String clickTimeStr = sdf.format(clickTime);
            String submitTimeStr = sdf.format(submitTime);
            //用户信息
            Paragraph userInfoTitle = new Paragraph("学生姓名:" + memberName + "  身份证号码:" + identityCardId + "  课程名称:" + courseName + "  开始做题时间:" + clickTimeStr + "  提交时间:" + submitTimeStr + "  做题时长:" + spendTime + "秒");
            // 设置标题格式对齐方式
            userInfoTitle.setAlignment(Element.ALIGN_CENTER);
            userInfoTitle.setFont(subTitleFont);
            document.add(userInfoTitle);
            Paragraph tabTitle = new Paragraph("");
            document.add(tabTitle);
            // 数据输出
            int i = 1;//每个题目的序号
            //每个题目开始遍历
            for (Comprehensive paperQuestion : list) {
                Paragraph questionTitle = new Paragraph(i + ". " + paperQuestion.getContent());
                // 设置标题格式对齐方式
                questionTitle.setAlignment(Element.ALIGN_LEFT);
                questionTitle.setFont(contextFont);
                document.add(questionTitle);
                List<Options> optionList = paperQuestion.getOptions();
                //开始每个题目的选项进行遍历
                for (Options questionOption : optionList) {
                    Paragraph optionTitle = new Paragraph(questionOption.getQuesValue() + ". " + questionOption.getQuesOption());
                    //如果是判断题只要“对”,“错“ 去掉”Y“,”N“
                    if ("Y".equals(questionOption.getQuesValue()) || "N".equals(questionOption.getQuesValue())) {
                        optionTitle = new Paragraph(questionOption.getQuesOption());
                    }
                    // 设置标题格式对齐方式
                    optionTitle.setAlignment(Element.ALIGN_LEFT);
                    if (null != paperQuestion.getUserAnswer() && questionOption.getQuesValue().equals(paperQuestion.getUserAnswer())) {
                        contextFont.setColor(Color.red);
                    }
                    optionTitle.setFont(contextFont);
                    document.add(optionTitle);
                    contextFont.setColor(Color.black);
                }
                Paragraph userAnswerTitle = new Paragraph("您的答案:" + paperQuestion.getUserAnswer());
                // 设置标题格式对齐方式
                userAnswerTitle.setAlignment(Element.ALIGN_LEFT);
                userAnswerTitle.setFont(contextFont);
                document.add(userAnswerTitle);

                Paragraph answerTitle = new Paragraph("正确答案:" + paperQuestion.getAnswer());
                // 设置标题格式对齐方式
                answerTitle.setAlignment(Element.ALIGN_LEFT);
                answerTitle.setFont(contextFont);
                document.add(answerTitle);

                Paragraph analysisTitle = new Paragraph("解析:" + paperQuestion.getAnalysis());
                // 设置标题格式对齐方式
                analysisTitle.setAlignment(Element.ALIGN_LEFT);
                analysisTitle.setFont(contextFont);
                document.add(analysisTitle);
                i++;
                document.add(new Paragraph(""));
            }
            document.close();
            //把创建好的word写入到输出流
            outBytes = baos.toByteArray();
        }catch (Exception e){
            e.printStackTrace();
        }
        return outBytes;
    }

实体类这里就不展示了,基本的属性,需要注意的是选项属性是一个集合形式的:

//选项
	private List<Options> options;
public class Options {
	//题目ID
	private String questionId;
	//选项内容
	private String quesOption;
	//选项值
	private String quesValue;
	//题号
	private Integer sequence;
	public String getQuestionId() {
		return questionId;
	}
	public void setQuestionId(String questionId) {
		this.questionId = questionId;
	}
	public String getQuesOption() {
		return quesOption;
	}
	public void setQuesOption(String quesOption) {
		this.quesOption = quesOption;
	}
	public String getQuesValue() {
		return quesValue;
	}
	public void setQuesValue(String quesValue) {
		this.quesValue = quesValue;
	}
	public Integer getSequence() {
		return sequence;
	}
	public void setSequence(Integer sequence) {
		this.sequence = sequence;
	}
	
}

这样就可以实现题目导出为Word的操作辣,如果有问题欢迎大家与我交流!

标签:Font,Word,Springboot,thymeleaf,Paragraph,new,document,public,String
From: https://blog.csdn.net/m0_62458640/article/details/139779490

相关文章

  • 毕业设计:人事管理系统,基于java+springboot+mysql
     一、前言介绍          困扰管理层的许多问题当中,人事管理是一定不敢忽视的一块。但是管理好人事又面临很多麻烦需要解决,例如有几个方面:第一,公司往往员工人数都比较多,如何保证能够管理到每一员工;第二,如何在工作琐碎,记录繁多的情况下将人事变动的情况反应......
  • 免费分享一套SpringBoot+Vue房地产销售管理系统【论文+源码+SQL脚本+PPT+开题报告】,帅
    大家好,我是java1234_小锋老师,看到一个不错的SpringBoot+Vue房地产销售管理系统,分享下哈。项目视频演示【免费】SpringBoot+Vue房地产销售管理系统Java毕业设计_哔哩哔哩_bilibili【免费】SpringBoot+Vue房地产销售管理系统Java毕业设计项目来自互联网,免费分享,仅供学习交流......
  • springboot 项目jacoco 覆盖率测试
    1.POM<dependency><groupId>org.jacoco</groupId><artifactId>jacoco-maven-plugin</artifactId><version>0.8.2</version></dependency> <plugin>......
  • Java项目:springboot优咪商城(计算机毕业设计)
    作者主页:Java毕设网 简介:Java领域优质创作者、Java项目、学习资料、技术互助文末获取源码一、项目介绍优咪网上购物体验系统1.该平台主要有两大功能:(1)浏览平台官方和认证作者提供的篮球相关信息,信息类型包括:视频,新闻,评论类文章,比赛结果(2)篮球周边商城,商品分类球......
  • springboot 集成shiro框架
    目录一、Shiro简介二、架构体系与专业术语三、Shiro与SpringSecurity的对比四、Shiro优缺点五、springboot集成Shiro一、Shiro简介        ApacheShiro是一个功能强大且易于使用的Java安全(权限)框架。Shiro可以完成:认证、授权、加密、会话管理、与Web集成......
  • 基于Java+SpringBoot+Vue+elementUI的学生宿舍管理平台的设计与开发
    第一章绪论1.1选题背景和意义1.2国内外学生宿舍管理平台现状第二章相关技术简介2.1开发工具介绍2.1.1IDEA2.1.2VSCode2.1.3Navicat2.1.4宝塔面板2.2关键技术介绍2.2.1Java2.2.2SpringBoot2.2.3Mybatis2.2.4Vue2.2.5MySQL2.2.6Redis2.2.7E......
  • springboot引入第三方jar包本地lib并打包
    springboot引入第三方jar包本地lib并打包亲测可用一、在项目根目录创建lib目录并放入第三方lib包--project----lib(放在这儿)----src----target二、pom中引入第三方lib<!--自定义引入jar--><dependency><groupId>commons-io</groupId><artifac......
  • springboot 集成 websocket
    1.首先添加maven依赖<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId></dependency>2.添加拦截器importcn.hutool.core.util.StrUtil;importcn.hutool.extra.spri......
  • Java毕业设计-基于springboot开发的图书商城管理系统-毕业论文(附毕设源代码)
    文章目录前言一、毕设成果演示(源代码在文末)二、毕设摘要展示1、开发说明2、需求/流程分析3、系统功能结构三、系统实现展示1、管理员功能介绍1.1图书列表1.2图书订单信息管理1.3图书类型管理四、毕设内容和源代码获取总结Java毕业设计-基于springboot开发的图书......
  • Java毕业设计-基于springboot开发的图书进销存管理系统-毕业论文(附毕设源代码)
    文章目录前言一、毕设成果演示(源代码在文末)二、毕设摘要展示1、开发说明2、需求/流程分析3、系统功能结构三、系统实现展示1、用户信息管理2、图书类型管理3、商品退货管理4、客户信息管理5、图书添加6、客户添加7、应收金额四、毕设内容和源代码获取总结Java......