SpringBoot: SpringBoot里面创建导出Excel的接口(亲测)
在Web项目中,难免需要导出Excel这样的功能,后端接口怎么实现呢,Controller代码在下面,复制到项目的Controller中即可使用:
首先加入Excel的依赖,本例中我们用apache的poi:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
1
2
3
4
5
后台导出Excel的Controller接口代码:
import org.apache.poi.hssf.usermodel.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Controller
public class ExcelController {
/**
* Excel表格导出接口
* http://localhost:8080/ExcelDownload
* @param response response对象
* @throws IOException 抛IO异常
*/
@RequestMapping("/ExcelDownload")
public void excelDownload(HttpServletResponse response) throws IOException {
//表头数据
String[] header = {"ID", "姓名", "性别", "年龄", "地址", "分数"};
//数据内容
String[] student1 = {"1", "小红", "女", "23", "成都青羊区", "96"};
String[] student2 = {"2", "小强", "男", "26", "成都金牛区", "91"};
String[] student3 = {"3", "小明", "男", "28", "成都武侯区", "90"};
//声明一个工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
//生成一个表格,设置表格名称为"学生表"
HSSFSheet sheet = workbook.createSheet("学生表");
//设置表格列宽度为10个字节
sheet.setDefaultColumnWidth(10);
//创建第一行表头
HSSFRow headrow = sheet.createRow(0);
//遍历添加表头(下面模拟遍历学生,也是同样的操作过程)
for (int i = 0; i < header.length; i++) {
//创建一个单元格
HSSFCell cell = headrow.createCell(i);
//创建一个内容对象
HSSFRichTextString text = new HSSFRichTextString(header[i]);
//将内容对象的文字内容写入到单元格中
cell.setCellValue(text);
}
//模拟遍历结果集,把内容加入表格
//模拟遍历第一个学生
HSSFRow row1 = sheet.createRow(1);
for (int i = 0; i < student1.length; i++) {
HSSFCell cell = row1.createCell(i);
HSSFRichTextString text = new HSSFRichTextString(student1[i]);
cell.setCellValue(text);
}
//模拟遍历第二个学生
HSSFRow row2 = sheet.createRow(2);
for (int i = 0; i < student2.length; i++) {
HSSFCell cell = row2.createCell(i);
HSSFRichTextString text = new HSSFRichTextString(student2[i]);
cell.setCellValue(text);
}
//模拟遍历第三个学生
HSSFRow row3 = sheet.createRow(3);
for (int i = 0; i < student3.length; i++) {
HSSFCell cell = row3.createCell(i);
HSSFRichTextString text = new HSSFRichTextString(student3[i]);
cell.setCellValue(text);
}
//准备将Excel的输出流通过response输出到页面下载
//八进制输出流
response.setContentType("application/octet-stream");
//这后面可以设置导出Excel的名称,此例中名为student.xls
response.setHeader("Content-disposition", "attachment;filename=student.xls");
//刷新缓冲
response.flushBuffer();
//workbook将Excel写入到response的输出流中,供页面下载
workbook.write(response.getOutputStream());
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
然后访问接口,弹出页面:
下载该Excel,打开后如下图:
至此为止,SpringBoot的后台Controller接口导出Excel数据表,已成功实现!
后来我封装了一个静态方法,可以在项目中作为工具类使用:
import org.apache.poi.hssf.usermodel.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
/**
* Excel工具类
*/
public class ExcelUtil {
/**
* Excel表格导出
* @param response HttpServletResponse对象
* @param excelData Excel表格的数据,封装为List<List<String>>
* @param sheetName sheet的名字
* @param fileName 导出Excel的文件名
* @param columnWidth Excel表格的宽度,建议为15
* @throws IOException 抛IO异常
*/
public static void exportExcel(HttpServletResponse response,
List<List<String>> excelData,
String sheetName,
String fileName,
int columnWidth) throws IOException {
//声明一个工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
//生成一个表格,设置表格名称
HSSFSheet sheet = workbook.createSheet(sheetName);
//设置表格列宽度
sheet.setDefaultColumnWidth(columnWidth);
//写入List<List<String>>中的数据
int rowIndex = 0;
for(List<String> data : excelData){
//创建一个row行,然后自增1
HSSFRow row = sheet.createRow(rowIndex++);
//遍历添加本行数据
for (int i = 0; i < data.size(); i++) {
//创建一个单元格
HSSFCell cell = row.createCell(i);
//创建一个内容对象
HSSFRichTextString text = new HSSFRichTextString(data.get(i));
//将内容对象的文字内容写入到单元格中
cell.setCellValue(text);
}
}
//准备将Excel的输出流通过response输出到页面下载
//八进制输出流
response.setContentType("application/octet-stream");
//设置导出Excel的名称
response.setHeader("Content-disposition", "attachment;filename=" + fileName);
//刷新缓冲
response.flushBuffer();
//workbook将Excel写入到response的输出流中,供页面下载该Excel文件
workbook.write(response.getOutputStream());
//关闭workbook
workbook.close();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
以上方法调用示例:
/**
* Excel表格导出接口
* http://localhost:8080/ExcelDownload
* @param response response对象
* @throws IOException 抛IO异常
*/
@RequestMapping("/ExcelDownload")
public void excelDownload(HttpServletResponse response) throws IOException {
List<List<String>> excelData = new ArrayList<>();
List<String> head = new ArrayList<>();
head.add("第一列");
head.add("第二列");
head.add("第三列");
List<String> data1 = new ArrayList<>();
data1.add("123");
data1.add("234");
data1.add("345");
List<String> data2 = new ArrayList<>();
data2.add("abc");
data2.add("bcd");
data2.add("cde");
excelData.add(head);
excelData.add(data1);
excelData.add(data2);
String sheetName = "测试";
String fileName = "ExcelTest.xls";
ExcelUtil.exportExcel(response, excelData, sheetName, fileName, 15);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
本文参考文章(感谢原作者):https://blog.csdn.net/panpan96/article/details/76566475
————————————————
版权声明:本文为CSDN博主「李先森LeeCode」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_39448458/article/details/83013677
SpringBoot: SpringBoot里面创建导出Excel的接口(亲测)相关推荐
- springboot中使用poi导出excel文件(亲测实现了第一个功能)
1.POI简介 Jakarta POI 是一套用于访问微软格式文档的Java API. 组件HWPF用于操作Word的; 组件HSSF用于操作Excel格式文件. 2.常用组件 HSSFWorkboo ...
- php把数据创建一个表格,PHP创建(导出Excel数据表格)
/** * 创建(导出Excel数据表格) * @param array $list 要导出的数组格式的数据 * @param string $filename 导出的Excel表格数据表的 ...
- Springboot 指定自定义模板导出Excel文件
前言 之前写过一篇极其简单的excel导入导出,是单个文件的: Springboot 最简单的结合MYSQL数据实现EXCEL表格导出及数据导入_小目标青年的博客-CSDN博客 还写过一篇单个,多个 ...
- springboot整合autopoi-web文件导出Excel,使用注解配置一多
1.导入maven依赖 <dependency><groupId>org.jeecgframework</groupId><artifactId>aut ...
- C++环境下读取excel表格,亲测可用
在VS2015环境中,个人对以下程序和步骤进行了测试,结果表明完全可以读取excel数据. 相关的完整程序可以在https://download.csdn.net/download/cxd3341/1 ...
- SpringBoot中使用POI导出Excel时怎样循环数据库数据赋值
场景 SpringBoot中使用POI实现自定义Excel布局式导出: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/894977 ...
- springboot整合easyexcel 导入导出excel案例及文件无法打开
前端请求 vue3+axios+typescript downloadByUrlStream({url:process.env.VUE_APP_API_URL1+"/MeshDevice/u ...
- Springboot 整合 easyexcel导入导出excel
1. 引入核心依赖 <dependency><groupId>com.alibaba</groupId><artifactId>easyexcel< ...
- SpringBoot集成阿里EasyExcel导出excel高级实战
目录 参考 一.引入包 二.导出到文件并输出到后台 三.过滤字段 方式1:类上加注解 @ExcelIgnoreUnannotated,过滤属性没有@ExcelProperty注解的字段 方式2:指定字 ...
最新文章
- CloudCompare的介绍
- 在Python3.4中实现opencv3.1.0的安装配置
- 2021中青杯数学建模A题 汽车组装车间流水线物料配送问题
- 最短路常用的四种模板(poj1847)
- CE3和UE3在多线程渲染方面的简单对比
- 011 Android TabLayout+ViewPager实现顶部滑动效果(多个页面)
- Miniflter中 NPInstanceSetup调查
- Linux(五、六)文本处理,文件内容显示和文件其他操作学习
- android实现接口的原则,《Android之大话设计模式》--设计原则 第一章:针对接口编程 不要针对实现编程...
- Cross the Wall UVALive - 5097 (贪心+斜率dp)
- 身份证读取设备开发解决方案:2、Android下通过usb转串口读取身份证信息
- html自动拨打电话,javascript之网页点击直接拨打电话和发送短信
- linux 脚本编写 -eq,关于shell脚本基础编程第四篇
- JavaEE笔记——设计模式
- LQ-Nets: Learned Quantization for Highly Accurate and Compact Deep Neural Networks
- python 开源cms内容管理系统_30 个很棒的 PHP 开源 CMS 内容管理系统
- 【Git从青铜到王者】第二篇:Git的初始
- 阿里云域名购买与域名解析使用教程(图文教程)
- 2022年秋季学期人工神经网络第五次作业
- OpenCV:如何获取摄像头的型号以及USB设备的VID和PID信息?
热门文章
- 计算机辅助测试英语缩写,“CAT”是“Computer-Aided Testing”的缩写,意思是“计算机辅助测试”...
- python高阶函数 动态_Python进阶内容(一)--- 高阶函数 High order function
- python数据库教程_Python连接mysql数据库及简单增删改查操作示例代码
- python interpreter 中没有torch_PyTorch扩展自定义PyThon/C++(CUDA)算子的若干方法总结
- linux高级编程有作用吗,Linux 高级编程
- pycharm下配置jupyter_在 Pycharm 中安装及使用 Jupyter (图文详解)
- 1、solr包结构介绍,solrCore的安装配置,solr部署到Tomcat,多solrCore配置,配置中文分词器,界面功能介绍,Schema.xml的基本使用,数据导入
- Android代码(Handler的运用),HttpURLConnection的应用,将url图片地址转换成图片。
- Struts2 控制台不打印异常的解决方案
- 用python简单处理图片(1):打开\显示\保存图像