thymeleaf+Springboot实现试卷导出为Word

实习期间遇到了一个这样的需求,业务要求将试题详情进行导出。但是看到页面展示的试题详情后,发现是使用<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的操作辣,如果有问题欢迎大家与我交流!

智能网联汽车的安全员高级考试涉及多个方面的专业知识,包括但不限于自动驾驶技术原理、车辆传感器融合、网络安全防护以及法律法规等内容。以下是针对该主题的一些核心知识解析: ### 关于智能网联车安全员高级考试的核心内容 #### 1. 自动驾驶分级标准 国际自动机工程师学会(SAE International)定义了六个级别的自动驾驶等级,从L0到L5[^1]。其中,L3及以上级别需要安全员具备更高的应急处理能力。 #### 2. 车辆感知系统的组成与功能 智能网联车通常配备多种传感器,如激光雷达、毫米波雷达、摄像头和超声波传感器等。这些设备协同工作以实现环境感知、障碍物检测等功能[^2]。 #### 3. 数据通信与网络安全 智能网联车依赖V2X(Vehicle-to-Everything)技术进行数据交换,在此过程中需防范潜在的网络攻击风险,例如中间人攻击或恶意软件入侵[^3]。 #### 4. 法律法规要求 不同国家和地区对于无人驾驶测试及运营有着严格的规定,考生应熟悉当地交通法典中有关自动化驾驶部分的具体条款[^4]。 ```python # 示例代码:模拟简单决策逻辑 def decide_action(sensor_data): if sensor_data['obstacle'] and not sensor_data['emergency']: return 'slow_down' elif sensor_data['pedestrian_crossing']: return 'stop_and_yield' else: return 'continue_driving' example_input = {'obstacle': True, 'emergency': False, 'pedestrian_crossing': False} action = decide_action(example_input) print(f"Action to take: {action}") ``` 需要注意的是,“同学”作为特定平台上的学习资源名称,并不提供官方认证的标准答案集;建议通过正规渠道获取教材并参加培训课程来准备此类资格认证考试
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值