說明
easypoi功能如同名字easy,主打的功能就是容易,讓一個沒見接觸過poi的人員
就可以方便的寫出Excel導(dǎo)出,Excel模板導(dǎo)出,Excel導(dǎo)入,Word模板導(dǎo)出,通過簡單的注解和模板
語言(熟悉的表達式語法),完成以前復(fù)雜的寫法
教程所使用的工具
前端 : 無
后端 :SpringBoot + easyPOI + MyBatis + MyBatis-Plus + Lombok
數(shù)據(jù)庫 : MySQL
GitHub項目地址 : EasyPOI
查看實體類的模型
package com.inet.codebase.entity;
import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelTarget;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import java.time.LocalDateTime;
import java.io.Serializable;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
/**
* <p>
* 用戶實體類
* </p>
*
* @author HCY
* @since 2020-10-13
*/
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("tbl_user")
@ExcelTarget("users")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 用戶序號
*/
@TableId(value = "user_id", type = IdType.AUTO)
private Integer userId;
/**
* 用戶姓名
*/
@Excel(name = "姓名")
private String userName;
/**
* 用戶生日
*/
@Excel(name = "生日" , importFormat = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date userBirthday;
/**
* 用戶愛好
*/
@Excel(name = "愛好")
private String userHabby;
/**
* 用戶身份證
*/
@Excel(name = "身份證")
private String userIdentity;
/**
* 用戶住址
*/
@Excel(name = "住址")
private String userAddress;
}
關(guān)于實體類的繼承 Serializable
的問題,移步至
關(guān)于EasyPOI的注解說明
@Excel 作用到filed上面,是對Excel一列的一個描述
@ExcelCollection表示一個集合,主要針對一對多的導(dǎo)出,比如一個老師對應(yīng)多個科目,科目就可以用集合表示
@ExcelEntity 表示一個繼續(xù)深入導(dǎo)出的實體,但他沒有太多的實際意義,只是告訴系統(tǒng)這個對象里面同樣有導(dǎo)出的字段
@ExcelIgnore 和名字一樣表示這個字段被忽略跳過這個導(dǎo)出
@ExcelTarget 這個是作用于最外層的對象,描述這個對象的id,以便支持一個對象可以針對不同導(dǎo)出做出不同處理
關(guān)于注解的大多數(shù)的參數(shù),還是移步去至
導(dǎo)入的實例
/**
* 導(dǎo)入Excel
* @author HCY
* @since 2020-10-14
* @throws Exception
*/
@Test
void contextLoads1() throws Exception {
//文件的位置,Excel文件的說明需要和實體類所對應(yīng)
File file = new File("C:\\Users\\Administrator.DESKTOP-TSJVEJ5\\Desktop\\test\\test.xlsx");
//導(dǎo)入的標(biāo)題和說明的設(shè)置
ImportParams params = new ImportParams();
//設(shè)置標(biāo)題
params.setTitleRows(1);
//設(shè)置說明
params.setHeadRows(1);
//導(dǎo)入獲取集合
List<User> users = ExcelImportUtil.importExcel(file, User.class, params);
//遍歷集合
users.forEach(System.out :: println);
//進行批量添加
boolean batch = userService.saveBatch(users);
//輸出結(jié)果
System.out.println(batch);
}
導(dǎo)入的xlsx文件的實例樣子
導(dǎo)入的excel實例
導(dǎo)出的實例
/**
* 導(dǎo)出Excel
* @author HCY
* @since 2020-10-14
*/
@Test
void contextLoads2() throws Exception {
//查詢?nèi)? List<User> list = userService.list();
//設(shè)置Excel的描述文件
ExportParams exportParams = new ExportParams("用戶列表的所有數(shù)據(jù)", "用戶信息" , ExcelType.XSSF);
//進行導(dǎo)出的基本操作
Workbook workbook = ExcelExportUtil.exportExcel(exportParams, User.class, list);
//輸入輸出流地址
FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\Administrator.DESKTOP-TSJVEJ5\\Desktop\\test\\users.xlsx");
//進行輸出流
workbook.write(fileOutputStream);
//關(guān)流
fileOutputStream.close();
workbook.close();
}
導(dǎo)出的Excel實例
導(dǎo)出的xlsx文件