基于Hutool技术Excel表格导出

news/2024/11/14 13:25:48/文章来源:https://www.cnblogs.com/liuyangjava/p/18400266

今天分享一下基于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.hqwc.cn/news/793083.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

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…

D35XT120-ASEMI新能源专用D35XT120

D35XT120-ASEMI新能源专用D35XT120编辑:ll D35XT120-ASEMI新能源专用D35XT120 型号:D35XT120 品牌:ASEMI 封装:DXT-5 批号:2024+ 现货:50000+ 正向电流(Id):35A 反向耐压(VRRM):1200V 正向浪涌电流:450A 正向电压(VF):1.10V 引脚数量:5 芯片个数:5 芯片尺寸:…

高等数学 1.1 映射与函数

目录一、映射映射概念逆映射与复合映射二、函数函数概念函数的几种特性(1)函数的有界性(2)函数的单调性(3) 函数的奇偶性(4)函数的周期性反函数与复合函数反函数复合函数函数的运算初等函数基本初等函数初等函数双曲函数与反双曲函数 一、映射 映射概念定义 设 \(X\) ,…

rfc编写

rfc检索:https://www.rfc-editor.org/rfc-index.html rfc绘图:https://asciiflow.com/ 绘图后保存成ascii art:

前端 - Failed to load module script 解决方案

Failed to load module script 解决方案 问题描述 打包好项目发布上传到 nginx 后,浏览器访问,出现一下报错信息:Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/html". Strict MIME ty…