多个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();
}
}