使用HuTool工具二次封装下载Excel模版、上传excel数据、下载excel数据

背景:
最近简略的了解了一个关于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注解的默认值,作为模版数据的例子,这一块可以写对应数据的规则,比如不能为空等等

这是我们读取/导入的数据

下面是我们下载的数据:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章