惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
SegmentFault 最新的问题
Spread Privacy
Spread Privacy
Google DeepMind News
Google DeepMind News
WordPress大学
WordPress大学
Blog — PlanetScale
Blog — PlanetScale
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Apple Machine Learning Research
Apple Machine Learning Research
SecWiki News
SecWiki News
腾讯CDC
P
Privacy International News Feed
Webroot Blog
Webroot Blog
J
Java Code Geeks
爱范儿
爱范儿
A
About on SuperTechFans
S
Secure Thoughts
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
D
DataBreaches.Net
Cloudbric
Cloudbric
Security Archives - TechRepublic
Security Archives - TechRepublic
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
C
Cyber Attacks, Cyber Crime and Cyber Security
P
Proofpoint News Feed
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Security Latest
Security Latest
Forbes - Security
Forbes - Security
小众软件
小众软件
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
Cybersecurity and Infrastructure Security Agency CISA
T
Threatpost
量子位
MongoDB | Blog
MongoDB | Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
月光博客
月光博客
W
WeLiveSecurity
P
Privacy & Cybersecurity Law Blog
Vercel News
Vercel News
Google Online Security Blog
Google Online Security Blog
云风的 BLOG
云风的 BLOG
GbyAI
GbyAI
S
Security @ Cisco Blogs
T
The Exploit Database - CXSecurity.com
Help Net Security
Help Net Security
V
Visual Studio Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
Application and Cybersecurity Blog
Application and Cybersecurity Blog
博客园 - 聂微东
P
Proofpoint News Feed
C
CERT Recently Published Vulnerability Notes
Attack and Defense Labs
Attack and Defense Labs

莫拉维克猫屋

4D Radar - A Novel Sensing Paradigm for 3D Object Detection 粗略的cs231n学习笔记 simulink仿真数字通信系统 基于开源的livego搭建直播服务器 EDA实验二 功能可调综合计时器 EDA实验一 指令运算单元设计——第一次用FPGA开发板 NodeMCU-ESP8266联网获取实时天气并使用lcd1602显示 Java网络编程-TCP通信 我要这差分放大电路有何用? Hello World
SSM框架:导出数据库内容到Excel表格
liuzengyun · 2020-08-20 · via 莫拉维克猫屋

经常遇到一些网站,尤其是管理员后台,有一个导出数据的功能。

在做暑期作业的时候,正需要这个功能,想起了之前用过的POI,查了一些资料发现POI吃内存比较严重,正好国内阿里改进了POI,形成了EasyExcel,并且已经开源。

官方说明文档

https://www.yuque.com/easyexcel/doc/easyexcel

功能预览

前端通过按钮,向后端接口发送post请求,实现文件下载。

image

image

接口实现

导入Maven依赖

1
2
3
4
5
6
7
8
9
10
11
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.6</version>
</dependency>
<dependency>
<groupId>org.projectlombok
</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>

Mapper.xml

1
2
3
4
<select id="selectAll"  resultMap="BaseResultMap">
select *
from uusers
</select>

Mapper接口声明

1
2
3
public interface UusersMapper {
List<Uusers> selectAll();
}

Uusers表实体类

继承自com.alibaba.excel.metadata.BaseRowModel类,每个属性前添加@ExcelProperty注解,声明导出Excel的表头。

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
public class Uusers extends BaseRowModel {

@ExcelProperty(value = "id",index = 0 )
private Integer id;

@ExcelProperty(value = "用户名",index = 1 )
private String username;

@ExcelProperty(value = "密码",index = 2 )
private String password;

@ExcelProperty(value = "身份",index = 3 )
private String identity;

@ExcelProperty(value = "手机号",index = 4 )
private String phone;

@ExcelProperty(value = "性别",index = 5 )
private String gender;

@ExcelProperty(value = "姓名",index = 6 )
private String name;

@ExcelProperty(value = "年龄",index = 7 )
private Integer age;

@ExcelProperty(value = "邮箱",index = 8 )
private String email;
}

Service层

1
2
3
4
5
6
@Autowired
private UusersMapper uusersMapper;
@Override
public List<Uusers> getAll() {
return uusersMapper.selectAll();
}

Controller层

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@RequestMapping("/downloadUsers")
@ResponseBody
public void downloadUsers(HttpServletResponse response) throws Exception {
List<Uusers> list = uusersService.getAll();
ServletOutputStream out = response.getOutputStream();
ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX, true);
String fileName = "用户信息表";
Sheet sheet = new Sheet(1, 0,Uusers.class);

sheet.setAutoWidth(Boolean.TRUE);

sheet.setSheetName("用户信息");
writer.write(list, sheet);

response.setHeader("Content-disposition", "attachment;filename=" + new String( fileName.getBytes("gb2312"), "ISO8859-1" ) + ".xlsx");
writer.finish();
response.setContentType("multipart/form-data");
response.setCharacterEncoding("utf-8");
out.flush();
out.close();
}