基于Hutool技术Excel表格导出

news/2024/9/22 20:26:19

今天分享一下基于Hutool技术Excel表格导出,我们先看看导出Excel表格的样子

第1步:引入maven依赖

  <dependencies><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.3.3</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>5.2.2</version></dependency></dependencies>

第2步:controller接口

public void exportExcel2(HttpServletRequest req, HttpServletResponse resp) {try {String questionType = req.getParameter("questionType") == null ? "1" : req.getParameter("questionType");List<Map> list = exportDao.exportExcel2(questionType);List<Map> choiceList = exportDao.exportChoice();ExcelWriter writer = ExcelUtil.getWriter(true);writer.writeRow(Arrays.asList("序号","技术领域","考核类型","现有领域","试题类型","试题内容","试题答案","选项1","选项2","选项3","选项4","选项5","选项6","选项7","选项8"));String answer = "ABCDEFGH";if("1".equals(questionType) || "2".equals(questionType)) { // 多选题,单选题list.stream().forEach(item->{List<String> sList = new ArrayList<>();StringBuilder stringBuilder = new StringBuilder();choiceList.stream().forEach(choiceItem->{if(item.get("question_id").equals(choiceItem.get("question_id"))){sList.add((String) choiceItem.get("choice_content"));stringBuilder.append(choiceItem.get("is_right"));}});switch (sList.size()) {case 1:writer.writeRow(Arrays.asList(item.get("question_id"),item.get("field_name"),null,null,item.get("question_type"),item.get("question_content"),this.mapCharacters(stringBuilder.toString(), answer),sList.get(0),null,null,null,null,null,null,null));break;case 2:writer.writeRow(Arrays.asList(item.get("question_id"),item.get("field_name"),null,null,item.get("question_type"),item.get("question_content"),this.mapCharacters(stringBuilder.toString(), answer),sList.get(0),sList.get(1),null,null,null,null,null,null));break;case 3:writer.writeRow(Arrays.asList(item.get("question_id"),item.get("field_name"),null,null,item.get("question_type"),item.get("question_content"),this.mapCharacters(stringBuilder.toString(), answer),sList.get(0),sList.get(1),sList.get(2),null,null,null,null,null));break;case 4:writer.writeRow(Arrays.asList(item.get("question_id"),item.get("field_name"),null,null,item.get("question_type"),item.get("question_content"),this.mapCharacters(stringBuilder.toString(), answer),sList.get(0),sList.get(1),sList.get(2),sList.get(3),null,null,null,null));break;case 5:writer.writeRow(Arrays.asList(item.get("question_id"),item.get("field_name"),null,null,item.get("question_type"),item.get("question_content"),this.mapCharacters(stringBuilder.toString(), answer),sList.get(0),sList.get(1),sList.get(2),sList.get(3), sList.get(4),null,null,null));break;case 6:writer.writeRow(Arrays.asList(item.get("question_id"),item.get("field_name"),null,null,item.get("question_type"),item.get("question_content"),this.mapCharacters(stringBuilder.toString(), answer),sList.get(0),sList.get(1),sList.get(2),sList.get(3), sList.get(4), sList.get(5),null,null));break;case 7:writer.writeRow(Arrays.asList(item.get("question_id"),item.get("field_name"),null,null,item.get("question_type"),item.get("question_content"),this.mapCharacters(stringBuilder.toString(), answer),sList.get(0),sList.get(1),sList.get(2),sList.get(3), sList.get(4), sList.get(5), sList.get(6),null));break;case 8:writer.writeRow(Arrays.asList(item.get("question_id"),item.get("field_name"),null,null,item.get("question_type"),item.get("question_content"),this.mapCharacters(stringBuilder.toString(), answer),sList.get(0),sList.get(1),sList.get(2),sList.get(3), sList.get(4), sList.get(5), sList.get(6),sList.get(7)));break;}});} else if ("3".equals(questionType)) { // 填空题list.stream().forEach(item->{writer.writeRow(Arrays.asList(item.get("question_id"),item.get("field_name"),null,null,item.get("question_type"),item.get("question_content"),item.get("question_answer"),null,null,null,null,null,null,null,null));});} else if("4".equals(questionType)) { // 判断题list.stream().forEach(item->{String ans = item.get("question_answer").toString();ans = "1".equals(ans) ? "正确" : "错误";writer.writeRow(Arrays.asList(item.get("question_id"),item.get("field_name"),null,null,item.get("question_type"),item.get("question_content"),ans,null,null,null,null,null,null,null,null));});} else if("5".equals(questionType) || "6".equals(questionType)) { // 简答题 或 程序设计题list.stream().forEach(item->{writer.writeRow(Arrays.asList(item.get("question_id"),item.get("field_name"),null,null,item.get("question_type"),item.get("question_content"),item.get("question_answer"),null,null,null,null,null,null,null,null));});}// 设置响应头content-typeresp.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset:utf-8");// 设置下载文件名String fileName= URLEncoder.encode("成绩信息表","UTF-8");resp.setHeader("Content-Disposition","attachment;filename="+fileName+".xlsx");ServletOutputStream outputStream= resp.getOutputStream();// 输出excel文件writer.flush(outputStream, true);outputStream.close();writer.close();} catch (SQLException | IOException throwables) {throwables.printStackTrace();}}

关键技术点:

public void exportExcel2(HttpServletRequest req, HttpServletResponse resp) {try {// 省略数据获取的操作....ExcelWriter writer = ExcelUtil.getWriter(true);// 设置表格列writer.writeRow(Arrays.asList("序号","技术领域","考核类型","现有领域","试题类型","试题内容","试题答案","选项1","选项2","选项3","选项4","选项5","选项6","选项7","选项8"));String answer = "ABCDEFGH";// 数据需要循环写入....// 写入数据操作writer.writeRow(Arrays.asList(item.get("question_id"),item.get("field_name"),null,null,item.get("question_type"),item.get("question_content"),this.mapCharacters(stringBuilder.toString(), answer),sList.get(0),null,null,null,null,null,null,null));// 设置响应头content-typeresp.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset:utf-8");// 设置下载文件名String fileName= URLEncoder.encode("成绩信息表","UTF-8");resp.setHeader("Content-Disposition","attachment;filename="+fileName+".xlsx");ServletOutputStream outputStream= resp.getOutputStream();// 输出excel文件writer.flush(outputStream, true);outputStream.close();writer.close();} catch (SQLException | IOException throwables) {throwables.printStackTrace();}}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.ryyt.cn/news/57303.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

jekins--- 通过jekins执行远程机器的shell脚本

jekins和脚本部署的不是在同一个机器,简单的来说说如何用jekins通过SSH连接执行远程机器的shell脚本 1:简单的看一下脚本的内容,自己写的一个简单的测试脚本,执行了就会打印一个脚本已执行成功在日志中#!/bin/bash# 日志文件路径 LOG_FILE="/dongguangshu/logfile.log…

[C++ Daily] 何时调用copy构造与copy赋值

<Effective C++>:"如果一个新对象被定义,一定会有个构造函数被调用,不可能调用赋值操作.如果没有新对象被定义,就不会有构造函数被调用,那么当然是赋值操作被调用."结果:

速通spring与mybatis

Spring 1.什么叫线程安全:多个线程访问一个对象时,不需要额外的调度与交替执行也不需要额外的同步,调用这个对象的行为都可以获得正确的调度结果 如何保持线程安全:使用final修饰变量,让其只可读不可修改 使用局部变量,公共数据私有化:这样堆内读取的数据就会改成在栈内…

JS解析JSON数据到TABLE表格

效果图: 数据采用JSON,[{"时间段": "上午","XX小学班课表": [{"名称": "教师上班","时间": "8:00","星期一": "","星期二": "","星期三": "&q…

登陆微软账号,输入window密码后,提示「哎呀,出错了」

存在问题解决方案暂时关闭dai~Li 设置 账户 下滑,电子邮件和账户 添加Microsoft账户解决后的效果版权木有,侵权不究,欢迎转载

TDBC回顾 | 钛铂数据肖贝贝:TapData — 自主可控

7月,TapData CTO 肖贝贝出席“2024可信数据库发展大会”,分享了“TapData — 自主可控的实时数据平台”的主题演讲。本文为完整内容。2024年7月17日上午,“2024可信数据库发展大会”数据库生态与国际化分论坛在北京隆重召开。会上,深圳钛铂数据有限公司CTO肖贝贝重点与听众…

服务器时间同步

一、安装ntpddate #centos,redhat系列 yum install ntpdate#debian,ubuntu系列 apt install ntpdate二、在服务器都能联网情况下时间同步 #手动同步阿里云时间服务器时间 ntpdate ntp.aliyun.com 阿里云时间同步地址 ntp1.aliyun.com ntp2.aliyun.com ntp3.aliyun.com ntp4.ali…