Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use easyexcel to export Excel in SpringBoot

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about how to use easyexcel to export Excel in SpringBoot, many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

First of all, we create a springboot (version is 2.1.4.RELEASE) project, which is not too verbose. After we have created it, we first need to introduce the maven coordinates of easyexcel.

Com.alibaba easyexcel 1.1.2-beta5

After the import, we need to create an exported template class. First, we need to integrate BaseRowModel,set and get omission. Value in the @ ExcelProperty annotation is the header information, and index is in which column. Unannotated ones will not be exported.

Public class OrderExcelBO extends BaseRowModel {@ ExcelProperty (value = {"order ID"}, index = 0) private String id; / * order description * / @ ExcelProperty (value = {"order description"}, index = 2) private String description; / * order corresponding product id * / @ ExcelProperty (value = {"product ID"}, index = 2) private Integer productId / * payment method description, such as: apple pay * / @ ExcelProperty (value = {"payment method"}, index = 3) private String payMethod; / * create_time * / @ ExcelProperty (value = {"time"}, index = 4) private String createTime; / * update_time * / private String updateTime / * the user who generated the order * / @ ExcelProperty (value = {"user ID"}, index = 5) private Integer userId; / * payment status: 0 unpaid, 1 payment completed successfully,-1 payment failed * / @ ExcelProperty (value = {"payment status"}, index = 6) private String status / * order source description, such as: ios Android * / @ ExcelProperty (value = {"phone model"}, index = 7) private String platform; / * order pipelining * / @ ExcelProperty (value = {"order pipeline number"}, index = 8) private String flowNum / * * order amount * / @ ExcelProperty (value = {"amount"}, index = 9) private BigDecimal price; / / @ ExcelProperty (value = {"receipt field"}, index = 10) private String receipt; @ ExcelProperty (value = {"APP Source"}, index = 10) private String sources;}

After the exported template is defined, it is followed by calls to some encapsulated utility classes

Find out the data we need to export

Generate Excel file name and sheet name

Just call the encapsulated tool class export file directly.

Let's take a look at the export effect.

If your header is complex, you can also define it according to your requirements, such as how to set a complex header such as the following

First of all, to modify the template class, if the maximum size of the merged cells is 2, then all the tables need to be set to 2, the unmerged cells will be filled with empty strings, and the merged cells will be written with the same name. And the serial numbers of the arrangement should be continuous and cannot be separated.

Let's take a look at the effect of the export, so that we can meet our usual development needs of the excel export function. Simple and easy to use.

Tool class:

Import com.alibaba.excel.context.AnalysisContext;import com.alibaba.excel.event.AnalysisEventListener;import java.util.ArrayList;import java.util.List;public class ExcelListener extends AnalysisEventListener {/ * is customized to temporarily store data. * you can obtain this value through the instance * / private List datas = new ArrayList (); / * you can also obtain the current sheet and current row data through the AnalysisContext object * / @ Override public void invoke (Object object, AnalysisContext context) {/ / data is stored in list for batch processing or subsequent business logic processing. Datas.add (object); / / self-do something doSomething () according to the business; / * if the data is too large, you can carry out quantitative batch processing if (datas.size ())

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report