easypoi

多个sheet页导出,并且某个字段设置动态的注解
https://www.jb51.net/article/193901.htm

public class Cat {
    @Excel(name = "猫猫的名字")
    public String name;

    @Excel(name = "猫猫的年龄")
    public int age;

    public Cat() {
    }

    public Cat(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
public class Dog {

    @Excel(name = "狗狗的名字")
    public String name;

    @Excel(name = "狗狗的年龄")
    public int age;

    public Dog() {
    }

    public Dog(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.File;
import java.io.FileOutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MoreSheetExportTest {


    public static void main(String[] args) throws Exception {

        List<Map<String, Object>> list = new ArrayList<>();
        //狗狗
        HashMap<String, Object> dogMap = new HashMap<>();

        Dog dog = new Dog();
        Field field = Dog.class.getDeclaredField("name");
        Excel excel = field.getAnnotation(Excel.class);
        InvocationHandler h = Proxy.getInvocationHandler(excel);

        Field hField = h.getClass().getDeclaredField("memberValues");
        hField.setAccessible(true);
        Map memberValues = (Map) hField.get(h);
        memberValues.put("name", "姓名");


        //ExportParams
        ExportParams exportParams = new ExportParams("清单一", "狗狗清单");
        dogMap.put("title", exportParams);
        //Class
        dogMap.put("entity", Dog.class);

        //Collection<?> dataSet
        ArrayList<Dog> dogs = new ArrayList<>();
        dogs.add(new Dog("泰迪", 2));
        dogs.add(new Dog("哈士奇", 1));

        //加入到Map
        dogMap.put("data", dogs);


        list.add(dogMap);

        //狗狗
        HashMap<String, Object> dogMap2 = new HashMap<>();
        ExportParams exportParams2 = new ExportParams("清单一3", "狗狗清单3");
        dogMap2.put("title", exportParams2);
        //Class
        dogMap2.put("entity", Dog.class);

        //Collection<?> dataSet
        ArrayList<Dog> dogs2 = new ArrayList<>();
        dogs2.add(new Dog("泰迪", 2));
        dogs2.add(new Dog("哈士奇", 1));

        //加入到Map
        dogMap2.put("data", dogs2);


        list.add(dogMap2);

        //猫猫
        HashMap<String, Object> CatMap = new HashMap<>();

        //ExportParams
        ExportParams exportParams1 = new ExportParams("清单二", "猫猫清单");
        CatMap.put("title", exportParams1);

        //Class
        CatMap.put("entity", Cat.class);

        //Collection<?> dataSet
        ArrayList<Cat> cats = new ArrayList<>();
        cats.add(new Cat("小花", 1));
        cats.add(new Cat("小红", 2));

        CatMap.put("data", cats);

        list.add(CatMap);

        Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF);

        workbook.write(new FileOutputStream(new File("D:\\animal.xlsx")));

        workbook.close();

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值