SpringBoot integrates easyExcel to import and export Excel files

Keywords: Java easyexcel

Introduce dependency

<!--easyExcel-->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>easyexcel</artifactId>
    <version>2.2.6</version>
</dependency>

Foreground code

<a href="${ctx}/itemCategory/daoExcel">
	export Excel
</a>

sql statement

<select id="list" resultType="ItemCategory">
    select * from item_category
</select>

Annotation details

Define title name

@ExcelProperty

Backend implementation

Add comments directly on the entity class field and set the title name. value can be ignored and not written.

//Set title to category name
@ExcelProperty(value="Category name")
private String name;

Front page

Sets the number of columns in which the title is located

@ExcelProperty(index= )

Backend implementation

Add comments directly on the entity class field and set the title name. value cannot be ignored and must be written.

//Set the title to the first column
@ExcelProperty(value="Category name",index=0)
private String name;

Front page

Sets the length of the column

@ColumnWith

Backend implementation

Add comments directly on the entity class field and set the column length

//Set the column length to 100
@ColumnWidth(100)
private String name;

Front page

Set title height

@HeadRowHeight

Backend implementation

Add annotations directly on the entity class and set the height of the title

//Set the title height to 50
@HeadRowHeight(50)
public class ItemCategory implements Serializable {}

Front page

Set title style

@HeadStyle all parameters

parametermeaning
dataFormatDate format
hiddenSets the cell to be hidden using this style
lockedSet the cell to lock using this style
quotePrefixAdd a 'symbol in front of the cell, and the number or formula will be displayed in the form of string
horizontalAlignmentSets whether to center horizontally
wrappedSets whether text should wrap. Set this flag to true to make all the contents of the cell visible by displaying on multiple rows
verticalAlignmentSets whether to center vertically
rotationSets the rotation angle of the text in the cell. The rotation angle interval of Excel version 03 is - 90 ° 90 °, and the rotation angle interval of Excel version 07 is 0 ° 180 °
indentSets the number of spaces for indented text in cells
borderLeftStyle left border
borderRightSet right border style
borderTopSet top border style
borderBottomSet bottom border style
leftBorderColorSet left border color
rightBorderColorSet right border color
topBorderColorSet top border color
bottomBorderColorSet bottom border color
fillPatternTypeSet fill type
fillBackgroundColorSet background color
fillForegroundColorset foreground color
shrinkToFitSet automatic cell size

Set title background color

@HeadStyle

Backend implementation

Add annotations directly on the entity class and set the background color of the title

// Set the header background to red IndexedColors.RED.getIndex()
@HeadStyle(fillPatternType = FillPatternType.SOLID_FOREGROUND, fillForegroundColor = 10)
public class ItemCategory implements Serializable {}

Front page

Set text line background color

@ContentStyle

Backend implementation

Add annotations directly on the entity class and set the height of the content

// Set the background of the content to green IndexedColors.GREEN.getIndex() 
@ContentStyle(fillPatternType = FillPatternType.SOLID_FOREGROUND, fillForegroundColor = 17)
public class ItemCategory implements Serializable { }

Front page

Set the background color of the content under a column heading

Backend implementation

Add comments directly to the field and set the background color of the content

// The background of the contents of the string is set to sky blue IndexedColors.SKY_BLUE.getIndex()
@ContentStyle(fillPatternType = FillPatternType.SOLID_FOREGROUND, fillForegroundColor = 40)
private String name;

Front page

Set title font style

Set title font color

@HeadFontStyle

Backend implementation

Add comments directly on the entity class and set the font color of the title

//Set the title color to cyan
@HeadFontStyle(color = 11)
public class ItemCategory implements Serializable { }

Front page

Sets the font color for a single title

@HeadFontStyle

Backend implementation

Add comments directly to the field and set the font color of a single title

//Set the title color to blue
@HeadFontStyle(color = 12)
private String name;

Front page

Set title font size

@HeadFontStyle

Add comments directly on the entity class and set the font size of the title

//Set the title font size to 30
@HeadFontStyle(fontHeightInPoints=30)
public class ItemCategory implements Serializable { }

Front page

Set the font size of a single title

@HeadFontStyle

Add comments directly to the field and set the font size of the title

//Set the title font size to 30
@HeadFontStyle(fontHeightInPoints=30)
private String name;

Front page

Text content style

Sets the font size of the contents under all column headings

@ContentFontStyle

Backend implementation

Directly add comments on the entity class and set the font size of the content

// The content font of the string is set to 30
@ContentFontStyle(fontHeightInPoints = 30)
public class ItemCategory implements Serializable {}

Front page

Sets the font size of the content under a column heading

@ContentFontStyle

Backend implementation

Add comments directly on the field and set the font size of the content

// The content font of the string is set to 30
@ContentFontStyle(fontHeightInPoints = 30)
private String name;

Front page

Text line height

@ContentRowHeight

Backend implementation

Add annotations directly on the entity class and set the height of the content

//Set the height of the text line to 60
@ContentRowHeight(60)
public class ItemCategory implements Serializable { }

Front page

Content centered

@ContentRowHeight

Backend implementation

Add annotations directly on the entity class and set the center

//Set center
@ContentStyle(horizontalAlignment = HorizontalAlignment.CENTER)
public class ItemCategory implements Serializable { }

Front page

@ContentRowHeight

Backend implementation

Add annotations directly on the entity class and set the center

//Set center
@ContentStyle(horizontalAlignment = HorizontalAlignment.CENTER)
private String name;

Front page

Entity class

Annotation @ ExcelProperty("category name") / / the contents in brackets are the header information of the generated Excel file

/**
 * Category
 */
@Data
public class ItemCategory implements Serializable {

    /**
     * Primary key id
     */
    @ExcelProperty("ID")  
    private Integer id;

    /**
     * Price
     */
    @ExcelProperty("Price title")
    private String name;
    
    /**
     * Proportion size
     */
    //Expressed as a percentage
    @NumberFormat("#.##%")
    @ExcelProperty("Proportion title")
    private String name;

    /**
     * date
     */
    //Use the format of mm / DD / yyyy
    @DateTimeFormat("yyyy year MM month dd day HH Time mm branch ss second")
    @ExcelProperty("Time title")
    private Date date;

Controller layer

//File generation address
String PATH = "D://";
//Export Excel
//Generate excel using large files (sxssfworkbook)
@RequestMapping("/daoExcel")
public String simpleWrite1() {

    //Query all categories
    List<ItemCategory> list = itemCategoryService.list();

    String fileName = PATH + "Transcript.xlsx";
    // Here, you need to specify which class to write, and then write to the first sheet with the name of template, and then the file stream will be closed automatically
    // If you want to use 03 here, you can pass in the excel type parameter
    EasyExcel.write(fileName, ItemCategory.class).sheet("Template sheet").doWrite(list);

    System.out.println("Data export succeeded");

    return "redirect:/itemCategory/findAll";

}


         Everyone's heart will retain a pessimistic color. Only because of the cruelty of reality and the unpredictability of people's hearts, they will cover their true side with layers of masks. No one can calmly face all experiences, and no one can calmly deal with all encounters.

Posted by Fahid on Tue, 21 Sep 2021 02:01:52 -0700