当前位置:网站首页>Easyexcel exports excel tables to the browser, and exports excel through postman test [introductory case]
Easyexcel exports excel tables to the browser, and exports excel through postman test [introductory case]
2022-06-12 08:10:00 【Xiao Wang with hair loss】
One 、 Preface
Xiaobian recently received an export Excel The needs of , The demand is still very simple , Just export the information displayed in the table to Excel That's all right. , There are no complicated merge columns .
Common exports Excel Technology :
- hutool
- easyExcel
- poi
hutool and easyExcel All right. poi Encapsulation , More convenient to use ! Because companies use EasyExcel Export , Xiaobian can't be independent . If you want to see poi and hutool Export can see the article written before Xiaobian :
Use POI+hutool Import Excel
Use POI Export the queried data table data to Excel in , One watch, one sheet
Don't talk much , Let's go straight to .
Two 、 Import Maven rely on
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.0.5</version>
</dependency>
Particular attention :
The version here should be a little higher , Otherwise, we have the date when exporting jdk8 New characteristics LocalDateTime, Will not support !
It is used by colleagues before Xiaobian started to use it 2. A few The version does not support the problem , Everybody be careful !!
3、 ... and 、 Write exported entity classes
@Data
@ColumnWidth(20)
public class Test implements Serializable {
private static final long serialVersionUID = 337361630075002456L;
@TableId(type= IdType.ASSIGN_ID)
@ExcelProperty(value = "id",index = 0)
private long id;
@ExcelProperty(value = " name ",index = 1)
private String name;
@ExcelProperty(value = " Gender ",index = 2)
private String gender;
@ExcelProperty(value = " data ",index = 3)
private String data;
@ExcelProperty(value = " Creation time ",index = 4)
private LocalDateTime createDate;
@ExcelProperty(value = " Update time ",index = 5)
private LocalDateTime updateDate;
}
Parameter interpretation :@ExcelProperty: Add to the fields to be exported ,value Export the contents displayed in the header , If not, the field name will be used as the header .
After the small editor test : Exported entity class , It will be exported without this annotation , But the header is the field name !!
@ColumnWidth(20): Set the length of each header , The loading class is all of this length , The length of a header is added to the field !
If you need to merge , You need to specify your own policy , Inherit extends AbstractCellWriteHandler rewrite afterCellDispose, That's all right. ; I won't show you here , I will give you a detailed teaching when I have this demand later !!
Four 、 test
For convenience, Xiaobian directly in controller It's done in , Don't learn , The business is in service It's quite standard !
@RestController
@RequestMapping("/test")
@Slf4j
@RequiredArgsConstructor
public class TestController {
private final TestService testService;
@NonNull
private TestMapper testMapper;
@SneakyThrows
@GetMapping("/easyExcel")
public void easyExcel (HttpServletResponse response){
String fileName = new String(" export excel.xlsx".getBytes(), StandardCharsets.ISO_8859_1);
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf8");
response.setHeader("Content-disposition", "attachment;filename=" + fileName );
List<Test> list = testMapper.selectList(null);
ServletOutputStream outputStream = response.getOutputStream();
EasyExcel.write(outputStream, Test.class)
.sheet("sheet1")
.doWrite(list);
}
}
If the @RequiredArgsConstructor If you don't understand, you can take a look at this article !@RequiredArgsConstructor Instead of @Autowrired
5、 ... and 、 Browser Test
We now visit in the browser , Address :http://localhost:8089/test/easyExcel

6、 ... and 、Postman test
remind : Usually, when we develop at the enterprise level , All require login verification , At this time, we cannot access the test through the browser , We need to use Postman To test , At the beginning, what I compiled and exported were all random codes , Later, my friend told me , Export cannot be done directly Send, To click... Below Send And Download. Now let me show you :
7、 ... and 、 View the export file
We can see that there is no random code !
8、 ... and 、 summary
So we finished the test , Successfully exported Excel. This article is just a simple export test , Maybe your needs should be beautified , Please refer to the official website for details :easyExcel Official website
Those who are destined can see !!!
Click on the access ! Make up your own website , There are also many good articles in it !
边栏推荐
- 模型压缩 | TIP 2022 - 蒸馏位置自适应:Spot-adaptive Knowledge Distillation
- Pytorch installation (GPU) in Anaconda (step on pit + fill pit)
- Compiling principle on computer -- functional drawing language (I)
- Improvement of hash function based on life game (continued 2)
- Procedure execution failed 1449 exception
- Detailed explanation of Google open source sfmlearner paper combining in-depth learning slam -unsupervised learning of depth and ego motion from video
- Compiling principle on computer -- function drawing language (III): parser
- Numpy generates row vectors and column vectors (1024 water medal text)
- Leetcode notes: Weekly contest 277
- Three data exchange modes: line exchange, message exchange and message packet exchange
猜你喜欢

FPGA implementation of right and left flipping of 720p image

模型压缩 | TIP 2022 - 蒸馏位置自适应:Spot-adaptive Knowledge Distillation

ASP.NET项目开发实战入门_项目六_错误报告(自己写项目时的疑难问题总结)

Database connection pool and dbutils tool

Three data exchange modes: line exchange, message exchange and message packet exchange

MATLAB image processing - cosine noise removal in image (with code)

Mathematical knowledge - derivation - Basic derivation knowledge

Vision Transformer | CVPR 2022 - Vision Transformer with Deformable Attention

How to write simple music program with MATLAB

MinGW offline installation package (free, fool)
随机推荐
Learning notes (1): live broadcast by Dr. Lu Qi - face up to challenges and grasp entrepreneurial innovation opportunities - face up to challenges and grasp entrepreneurial innovation opportunities -1
2.1 链表——移除链表元素(Leetcode 203)
Alibaba cloud deploys VMware and reports an error
Three data exchange modes: line exchange, message exchange and message packet exchange
Discrete chapter I
Convolutional neural network CNN based cat dog battle picture classification (tf2.1 py3.6)
Vision Transformer | Arxiv 2205 - LiTv2: Fast Vision Transformers with HiLo Attention
(P21-P24)统一的数据初始化方式:列表初始化、使用初始化列表初始化非聚合类型的对象、initializer_lisy模板类的使用
uni-app用canvas截屏并且分享好友
从AC5到AC6转型之路(1)——补救和准备
Servlet
对企业来讲,MES设备管理究竟有何妙处?
Transformation from AC5 to AC6 (1) - remedy and preparation
Leetcode notes: Weekly contest 275
Group planning chapter I
Vscode的Katex问题:ParseError: KaTeX Parse Error: Can‘t Use Function ‘$‘ In Math Mode At Position ...
Visual studio code batch comment and uncomment
Improvement of hash function based on life game
StrVec类 移动拷贝
Improvement of hash function based on life game (continued 2)