easypoi
easypoi是為了方便我們編寫excel和word的模板導入和導出,是對poi的封裝的一個工具類
(將Java中的對象作為excel表中一行一行記錄導出)
easypoi 父包--作用大家都懂得
easypoi-annotation 基礎注解包,作用與實體對象上,拆分后方便maven多工程的依賴管理
easypoi-base 導入導出的工具包,可以完成Excel導出,導入,Word的導出,Excel的導出功能
easypoi-web 耦合了spring-mvc 基於AbstractView,極大的簡化spring-mvc下的導出功能
sax 導入使用xercesImpl這個包(這個包可能造成奇怪的問題哈),word導出使用poi-scratchpad,都作為可選包了
導出
1.導入pom依賴
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>4.3.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>4.3.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>4.3.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
注解介紹
easypoi起因就是Excel的導入導出,最初的模板是實體和Excel的對應,model--row,filed--col 這樣利用注解我們可以和容易做到excel到導入導出 經過一段時間發展,現在注解有5個類分別是
@Excel 作用到filed上面,是對Excel一列的一個描述
- 用在filed(屬性)上面,是對Excel一列的一個描述
- 常用屬性:
name : [String][生成Excel表格中列名]
needMerge: [boolean][是否需要縱向合並單元格(用於含有list中,單個的單元格,合並list創建的多個row)]
orderNum : [String][指定生成Excel中列的順序,按照數字自然順序排序]
savePath : [String][指定導入Excel中圖片的保存路徑]
type : [String][導出類型 1 是文本 2 是圖片,3 是函數,10 是數字 默認是文本]
width : [Double][指定導出Excel時列的寬度]
isImportField: [boolean][是否是導入字段,如果沒有說明是錯誤的Excel]
exportFormat: [String][導出Excel的時間格式]
importFormat: [String][導入Excel的時間格式]
format : [String][相當於同時設置了exportFormat和importFormat]
imageType: [int][導出類型 1 從file讀取 2 是從數據庫中讀取 默認是文件 同樣導入也是一樣的]
suffix : [String][文字后綴,如% 90 變成90%]
@ExcelCollection 表示一個集合,主要針對一對多的導出,比如一個老師對應多個科目,科目就可以用集合表示
- 一對多的集合注解,用以標記集合是否被數據以及集合的整體排序
- 常用屬性:
name : [String][定義集合列名]
orderNum: [int][用來指定導出excel集合內列的順序]
type : [Class\<?>][用來指定導出是創建對象類型]
@ExcelEntity 表示一個繼續深入導出的實體,但他沒有太多的實際意義,只是告訴系統這個對象里面同樣有導出的字段
- 標記是不是導出excel 標記為實體類,一遍是一個內部屬性類,標記是否繼續穿透
- 常用屬性:
name: [String][定義唯一標識]
@ExcelIgnore 和名字一樣表示這個字段被忽略跳過這個導導出
# 1.說明
- 用在屬性上,導出Excel時忽略這個屬性
@ExcelTarget 這個是作用於最外層的對象,描述這個對象的id,以便支持一個對象可以針對不同導出做出不同處理
- 用在實體類上標識是一個可以通過EasyPOI導入導出的實體類
- 相關屬性:
value: [String][定義id唯一標識,不能重復] `常用`
height: [Double][定義單元格高度]
fontSize: [short ][定義單元格字體大小]
User.java
@Data
@AllArgsConstructor
@NoArgsConstructor
@ExcelTarget("user1")//這個是標識這個對象,里面的值沒有影響
public class User implements Serializable {
@Excel(name = "編號")
private String id;
@Excel(name = "姓名",width = 10)
private String name;
@Excel(name = "年齡",replace = {"10歲_10","11歲_11"})
private Integer age;
@Excel(name = "生日",exportFormat = "yyyy-MM-dd",width = 16)
private Date bir;
}
Testpoi.java
public class Testpoi {
public List<User> getUsers(){
List<User> users = new ArrayList<>();
for (int i = 0; i < 10; i++) {
User user = new User();
user.setId(String.valueOf(i));
user.setName("小余"+i);
user.setAge(5+i);
user.setBir(new Date());
users.add(user);
}
return users;
}
@Test
public void testExport() throws IOException {
//獲取數據
List<User> users = getUsers();
//導出excel
Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("用戶信息列表", "用戶信息"), User.class, users);
//將excel寫入指定位置
FileOutputStream outputStream = new FileOutputStream("C:/Users/MSI-PC/Desktop/aa.xls");
workbook.write(outputStream);
outputStream.close();
workbook.close();
}
}
這個代碼中都只是展示了一部分屬性的用法,在學習中我們可以去舉一反三來提高學習效率。
導入
public void testImport() throws Exception {
ImportParams params = new ImportParams();
params.setTitleRows(1);//標題占的行數
params.setHeadRows(1);//header占的行數
List<Object> excel = ExcelImportUtil.importExcel(new FileInputStream("C:/Users/MSI-PC/Desktop/aa.xls"), User.class, params);
excel.forEach(System.out::println);
}