This is the 28th day of my participation in the Novembermore Challenge.The final text challenge in 2021

Hello, I’m Grey Ape! A super bug writing program ape! The first two articles shared with you about using easyExcel technology to achieve eaxcel simple read and write operations, the summary is “simply not too simple!” So today in this article, I will continue to share with you some operations about using EasyExcel for data export. Experience the powerful charm of EasyExcel. \

Import required dependencies

Import the dependencies required by EasyExcel into the POM file.

<! Poi </groupId> <artifactId>poi</artifactId> <version>3.17</version> </dependency> <! --> <dependency> <groupId>org.apache.poi</groupId> <artifactId> Poi-ooxml </artifactId> <version>3.17</version>  </dependency> <! Alibaba </groupId> <artifactId>easyexcel</artifactId> <version>2.2.6</version>  </dependency>Copy the code

Export only the specified columns according to the parameters

Exporting only specified columns by parameter is useful if our data object has a lot of element attributes, but we don’t want to export all the attribute columns when exporting to Excel, then we can specify which columns to export only, or which columns not to export. The data objects are as follows:

/** * demodata */ @data public class Demodata {@excelProperty (value = "String title ") private String stringTitle; @excelProperty (value = "dateTitle ") private Date dateTitle; @columnWidth (50) @excelProperty (value = "value ") private int doubleTitle; }Copy the code

Specifies which columns are not included

Specifying which columns are not to be included in the export is actually quite simple; we simply write a set to which we write the names of the attributes that are not included. You can then pass in the set as a parameter. The code is as follows:

/** * excludeColumnWrite() {log.info(" excludeColumnWrite()) "); Set<String> excludeColumnNames = new HashSet<String>(); excludeColumnNames.add("dateTitle"); EasyExcel.write(FILEPATH + "testExcel_1.xlsx", Class) // Specifies which columns are excluded when exporting. ExcludeColumnFiledNames (excludeColumnNames).sheet(" Test table 1").dowrite (DemoData); Log.info (" Exported successfully ~~~"); }Copy the code

The effect is as follows:

Specifies which columns to include only

Specifying which columns to include is the same as specifying which columns to exclude; we simply pass a set of columns to includeColumnFiledNames() to specify the columns to write to. See the following example for details:

/** * include only specified columns */ public void includeColumnWrite() {log.info(" include only specified columns ~~~"); // Store column name attributes for only those columns Set<String> includeColumnNames = new HashSet<String>(); includeColumnNames.add("dateTitle"); EasyExcel. Write (FILEPATH + "testExcel_1.xlsx", Demodata.class) // Specify which columns to include when exporting. IncludeColumnFiledNames (includeColumnNames).sheet(" test table 1").dowrite (DemoData); Log.info (" Exported successfully ~~~"); }Copy the code

The effect is as follows:

Complex header write & merge table header

One of the most common situations you’ll encounter when writing Excel is when you need to merge some of the table’s header columns, and that’s when you need to write complex headers. In EasyExcel, complex headers can be written directly in the form of annotations.

For example, we want to combine “string heading”, “time heading”, and “number heading” at the top into “main heading”,

All we need to do is specify it in the @excelProperty annotation of the data object. The code is as follows:

/** * complex header write, */ Public class ComplexHeadData {@ExcelProperty({" master title "," String title "}) private String stringData; @excelProperty ({"主 名 "," 主 名 "}) private Date Date; @excelProperty ({" header "," header "}) private Double doubleData; }Copy the code

Test example:

Public void complexHeadWrite() {EasyExcel. Write(FILEPATH + "testExcel_w1.xlsx", Complexheaddata.class).sheet(" test 1").dowrite (demoData); }Copy the code

The effect is as follows:

So these are the two basic operations of using EasyExcel technology to operate Excel, and then when we write data to Excel, it will be much more convenient to use annotations.

Feel good remember to like attention yo! Then I will continue to share with you the practical tips of EasyExcel.

I’m Grey Ape, and I’ll see you next time!