背景:
最近簡略的瞭解了一個關於HuTool的工具包,自己又對其進行封裝了一個關於操作Excel的工具類,準備分享出來。
好處:利用java反射機制,一定程度上減少代碼的冗餘量。
代碼講解:
java操作Excel數據是爲了將Excel表的數據與我們的javabean一一對應起來,hutool這個工具包確實爲我們提供瞭解決方案:
excelReader.addHeaderAlias("excel表名","javabean的屬性名");
但是如果這個excel的表頭很多呢,假設有個5,60個,那你就要寫對應個數的對應數量,也太麻煩了
所以我們可以利用Java的反射機制,給對應的javabean添加一個註解(我們可以自定義一個註解,javabean的屬性一個別名),我是自己想到可以利用Jackson的@JsonProperty註解,所以就拿來使用了,如下
先貼一個dto對象(excel表內數據對應的javabean)
package com.panpan.domain;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import java.util.Date;
@Data
public class User {
@JsonProperty(value = "編號",defaultValue = "1")
private int id;
@JsonProperty(value = "姓名",defaultValue = "張三")
private String name;
@JsonProperty(value = "手機號",defaultValue = "152XXXX3345")
private String phone;
@JsonProperty(value = "年齡",defaultValue = "18")
private int age;
@JsonProperty(value = "創建時間",defaultValue = "2019/10/01")
private Date createTime;
}
我封裝的工具包如下:
package com.panpan.utils;
import cn.hutool.poi.excel.*;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.panpan.domain.User;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Workbook;
import java.io.File;
import java.lang.reflect.Field;
import java.util.*;
public class HuToolExcel {
public static void main(String[] args) {
HuToolExcel huToolExcel = new HuToolExcel();
//下載模版
huToolExcel.downloadTemplate(User.class);
//導入數據
List<User> data = huToolExcel.importData(User.class);
//下載數據
huToolExcel.downloadData(data, User.class);
}
public <T> void downloadTemplate(Class<T> beanType) {
ExcelWriter writer = ExcelUtil.getWriter();
//下載模版
writer.setStyleSet(fontStyle(writer.getWorkbook()));
writer.writeRow(getHeadTitleAndRule(beanType), true);
File destFile = new File("/Users/admin/Desktop/user2.xlsx");
writer.flush(destFile);
System.out.println("write success!");
}
/**
* 默認註解使用JsonProperty
*
* @param beanType
* @return
*/
public <T> Map<Object, Object> getHeadTitleAndRule(Class<T> beanType) {
Field[] fields = beanType.getDeclaredFields();
Map<Object, Object> map = new HashMap<>();
for (Field field : fields) {
String alisaName = field.getDeclaredAnnotation(JsonProperty.class).value();
String ruleStr = field.getDeclaredAnnotation(JsonProperty.class).defaultValue();
map.put(alisaName, ruleStr);
}
return map;
}
/**
* 給規則設置樣式
*
* @param workbook
*/
public StyleSet fontStyle(Workbook workbook) {
StyleSet styleSet = new StyleSet(workbook);
Font redFont = workbook.createFont();
redFont.setColor(Font.COLOR_RED);
styleSet.setFont(redFont, true);
return styleSet;
}
//數據導入工具類
public <T> List<T> importData(Class<T> beanType) {
ExcelReader excelReader = ExcelUtil.getReader("/Users/admin/Desktop/user.xlsx");
Field[] fields = beanType.getDeclaredFields();
for (Field field : fields) {
String headName = field.getDeclaredAnnotation(JsonProperty.class).value();
String fieldName = field.getName();
excelReader.addHeaderAlias(headName, fieldName);
}
List<T> data = excelReader.readAll(beanType);
for (T t : data) {
System.out.println(t.toString());
}
return data;
}
//下載數據
public <T> void downloadData(List<T> data, Class<T> beanType) {
ExcelWriter excelWriter = ExcelUtil.getWriter();
Field[] fields = beanType.getDeclaredFields();
for (Field field : fields) {
String headName = field.getDeclaredAnnotation(JsonProperty.class).value();
String fieldName = field.getName();
excelWriter.addHeaderAlias(fieldName, headName);
}
excelWriter.write(data);
File destFile = new File("/Users/admin/Desktop/user2.xlsx");
excelWriter.flush(destFile);
System.out.println("download success!");
}
}
突然不想一個一個方法在進行講解了,獻上我的截圖吧。
這是我們下載的模版,我僅是巧用了@JsonProperty註解的默認值,作爲模版數據的例子,這一塊可以寫對應數據的規則,比如不能爲空等等
這是我們讀取/導入的數據:
下面是我們下載的數據: