[EasyPoi Practical Series] Spring Boot uses EasyPoi to dynamically control the exported columns – Part 471

Historical articles (articles accumulatively 460+)

“One of the most complete Spring Boot series in China”

“The Most Complete Spring Boot Series in China Part 2”

“The Most Complete Spring Boot Series in China Part Three”

“The Most Complete Spring Boot Series in China Part Four”

“The Five Most Complete Spring Boot Series in China”

“The Sixth Most Complete Spring Boot Series in China”

Use Midjourney to draw a beautiful woman, AI painting is too powerful! ! ! – Article 8

[EasyPoi Practical Series] Spring Boot uses EasyPoi annotations to make tables more beautiful and export pictures – Part 468

Recommend an idea god-level code plugin [Bito-ChatGPT] and it’s free! – Part 9

[EasyPoi Practical Series] Spring Boot uses EasyPoi to realize one-to-many export – Part 469

[EasyPoi Practical Series] Spring Boot uses EasyPoi to realize multi-Sheet export – Part 470

Wuxian: My head hurts, it’s really Barbie Q.

Master: Disciple, what’s the matter?

Wuxian: There is another wonderful demand for the product.

Master: What a wonderful method?

Wu Xian: The columns of the cells to be exported can be customized by the user. For example, if the user only wants to export the name and gender, only these two columns will be exported, and the others will not be exported.

Master: Then this is also very simple, you can configure the columns to be exported through code.

Wuxian: Nani, can you still be like this, master, please teach me.

Master: Get in the car, let’s go~

Guide

The export of annotations stipulates that we must write the model and the annotations well. The Excel exported each time is fixed, and the exported columns cannot be dynamically controlled. Although a case can be processed by id, the degree of freedom is far from enough. This section introduces another export method with full degrees of freedom.

Description: The exported entity classes of the examples in this section are based on the entity classes in the previous chapters, so those who are a bit confused can check the previous articles:

?EasyPoiPractical Series:

01. “[EasyPoi Actual Combat Series] Spring Boot Integrated EasyPoi – Chapter 467”

02. “[EasyPoi Practical Series] Spring Boot uses EasyPoi’s annotations to make tables more beautiful and export pictures – Chapter 468”

03. “[EasyPoi Practical Series] Spring Boot uses EasyPoi to realize one-to-many export – Chapter 469”

04. “[EasyPoi Actual Combat Series] Spring Boot uses EasyPoi to realize multi-Sheet export – Chapter 470”

?

1. Requirements

The export in the previous first section:

@GetMapping("/exportExcel")</code><code>public void exportExcel(HttpServletResponse response) throws IOException {<!-- --></code><code> //Query the data to be exported</code><code> //List<UserExportVO> users = userService.getUserExportList();</code><code> // simulated data</code><code> List<UserExportVO> users = new ArrayList<>() ;</code><code> // read picture</code><code> File picture = ResourceUtils.getFile("classpath:static/img/001.png");</code><code> InputStream pictureStream = new FileInputStream(picture);</code><code> byte[] bytes = IOUtils.toByteArray(pictureStream);</code><code> users.add(new UserExportVO("Wuxian",1,new Date() ,"18688888888","[email protected]",bytes,"Public Account SpringBoot"));</code><code> users.add(new UserExportVO("Master",1,new Date(),"18666666666 ","[email protected]",bytes,"Public Account SpringBoot"));</code><code>?</code><code> ExcelUtil.exportExcelX(users, "Test export table", "sheet1" , UserExportVO.class, "Test export table.xlsx", response);</code><code>}</code><code>?

The name, gender, date of birth, mobile phone number, email address, company logo, and description are exported here.

Now the requirement of the product is to export an Excel table without a mobile phone number. To meet this requirement, if you use annotations, you need to copy a UserExportVO, and then remove the phone field.

If the current product requirements are like this, and the exported columns are selected by the user at the front end, then this method of annotation obviously cannot meet the requirements.

In response to such requirements, it is necessary to use code configuration to export.

Based on the export of List, ExcelExportEntity is an entity class that has been processed and translated into annotations. The two are almost a pair, so if we want to dynamically customize the export column, we only need to dynamically assemble ExcelExportEntity

2. Dynamically control the exported columns

To realize the dynamically exported columns, it is necessary to implement the previously annotated part through code. Here, ExcelExportEntity is used. The two most important configurations for ExcelExportEntity are name and key; among them, name is the name displayed in the column, and key is the corresponding key in the map.

2.1 The method of extending ExcelUtil

Dynamic export needs to use the method ExcelExportUtil.exportExcel(ExportParams entity, List entityList, Collection dataSet).

Let’s expand the method of ExcelUtil and add the following method:

/**</code><code> * Dynamically export the method corresponding to the table column.</code><code> * @param exportParams</code><code> * @param entityList</code><code> * @param dataSet</code><code> * @param fileName</code><code> * @param response</code><code> */</code><code>public static void exportExcel(ExportParams exportParams , List<ExcelExportEntity> entityList, Collection<?> dataSet,String fileName, HttpServletResponse response){<!-- --></code><code> Workbook workbook = ExcelExportUtil.exportExcel(exportParams,entityList,dataSet);</code><code> if (workbook != null) {<!-- --></code><code> downLoadExcel(fileName, response, workbook);</code><code> }</code><code>}</code><code>?

2.2 Building Data

The build data is the same as before, without any differences:

List<UserExportVO> users = new ArrayList<>();</code><code>// read pictures</code><code>File picture = ResourceUtils.getFile("classpath:static/img/001 .png");</code><code>InputStream pictureStream = new FileInputStream(picture);</code><code>byte[] bytes = IOUtils.toByteArray(pictureStream);</code><code>users.add (new UserExportVO("Wuxian",1,new Date(),"18688888888","[email protected]",bytes,"Public Account SpringBoot"));</code><code>users.add(new UserExportVO("Master",1,new Date(),"18666666666","[email protected]",bytes,"Public Account SpringBoot"));</code><code>?

2.3 Building tables

Use ExcelExportEntity to build cell configuration information, which is equivalent to the annotation @Excel:

//Construct ExcelExportEntity list</code><code>List<ExcelExportEntity> beanList = new ArrayList<ExcelExportEntity>();</code><code>//Construction object is equivalent to @Excel</code><code>beanList.add(new ExcelExportEntity("name", "realName"));</code><code>beanList.add(new ExcelExportEntity("gender", "sex"));</code><code>beanList .add(new ExcelExportEntity("mobile number", "phone"));</code><code>beanList.add(new ExcelExportEntity("email", "email"));</code><code>beanList. add(new ExcelExportEntity("remark", "remark"));</code><code>boolean needBirthday = false;</code><code>if(needBirthday){<!-- --></code> <code> beanList .add(new ExcelExportEntity("date of birth", "birthday"));</code><code>}</code><code>?

2.4 Call the exported method

Finally call the exported method:

ExcelUtil.exportExcel(new ExportParams("User List", "User Information"), beanList ,users,"User Report.xlsx",response);

The final whole code is as follows:

/**</code><code> * Dynamically export tables.</code><code> * /demo/exportExcel3</code><code> * @param response</code><code> */ </code><code>@GetMapping("/exportExcel3")</code><code>public void exportExcel3(HttpServletResponse response) throws IOException {<!-- --></code><code> // simulated data </code><code> List<UserExportVO> users = new ArrayList<>();</code><code> // read pictures</code><code> File picture = ResourceUtils.getFile("classpath:static /img/001.png");</code><code> InputStream pictureStream = new FileInputStream(picture);</code><code> byte[] bytes = IOUtils.toByteArray(pictureStream);</code><code> users.add(new UserExportVO("Wuxian",1,new Date(),"18688888888","[email protected]",bytes,"Official Account SpringBoot"));</code><code> users .add(new UserExportVO("Master",1,new Date(),"18666666666","[email protected]",bytes,"Public Account SpringBoot"));</code><code>?</code><code> //Construct ExcelExportEntity list</code><code> List<ExcelExportEntity> beanList = new ArrayList<ExcelExportEntity>();</code><code> //Construction object is equivalent to @Excel</code><code> beanList.add(new ExcelExportEntity("name", "realName"));</code><code> beanList.add(new ExcelExportEntity("gender", "sex"));</code><code> beanList.add(new ExcelExportEntity("mobile number", "phone"));</code><code> beanList.add(new ExcelExportEntity("mailbox", "email"));</code><code> beanList .add(new ExcelExportEntity("remark", "remark"));</code><code> boolean needBirthday = false;</code><code> if(needBirthday){<!-- --></code><code> beanList .add(new ExcelExportEntity("date of birth", "birthday"));</code><code> }</code><code>?</code><code> ExcelUtil.exportExcel(new ExportParams("User List", "User Information"), beanList ,users,"User Report.xlsx",response);</code><code>}</code><code>?

The advantage of this kind of code is that the data is the same, then the corresponding ExcelExportEntity will be exported, otherwise it will not be exported, such as birthday above, if needBirthday is false, no configuration will be added ExcelExportEntity(“date of birth”, “birthday”); then the information will not be exported.

For the fields to be exported, users can make self-selection at the front end.

2.5 Code Test

Run the code and visit the link:

http://127.0.0.1:8080/demo/exportExcel3

Welcome to the AI+ Community! Here, you can unlock more skills, connect with more people, and get more sideline opportunities. As a community with artificial intelligence, we are committed to realizing the seamless integration of people and technology to help you grow in your personal and professional career.

Join AI+, you will get the following advantages and opportunities:

(1) Acquire more skills: AI+ brings together many domain experts and technical experts, who will share their experience and skills with you to help you master new knowledge and skills.

(2) Expand your network: AI+ is a large and diverse community, where you will have the opportunity to meet talents and entrepreneurs in different fields, expand your network, and gain more opportunities in your career.

(3) Obtain side business opportunities: There are a variety of side business opportunities on the AI + platform, where you can cooperate with others to create more value and increase your own income.

In addition, AI+ will provide the latest information, the hottest topics, the most exciting activities, etc. every day, so that you can continue to learn and communicate in the community, and expand your horizons and cognition.

Click on the link: https://t.zsxq.com/0etPXiQ28

Join AI + to start an interesting and fulfilling study and career!

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Java skill treeHomepageOverview 118676 people are studying systematically