多数据分组场景,初始模板在sheet0,根据分组数据动态扩展sheet,每个组一个sheet,都套用sheet0的模板。现在的实现思路是,自己根据分组复制sheet0之后再单独填充,框架能否支持或者支持了我没发现?
举例:A组:{name:xxx,age:18};B组:{name:yyy,age:88}excel生成两个sheet名分别是:A组和B组,数据分别填充到对应sheet
bkkx9g8r1#
import com.alibaba.excel.EasyExcel; import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.write.metadata.WriteSheet; import java.util.ArrayList; import java.util.List; public class WriteMultipleSheets { public static void main(String[] args) { String fileName = "output.xlsx"; // 准备数据 List<Person> groupA = new ArrayList<>(); groupA.add(new Person("John", 30)); List<Person> groupB = new ArrayList<>(); groupB.add(new Person("Alice", 25)); // 写入 Excel 文件 try (ExcelWriter excelWriter = EasyExcel.write(fileName).build()) { WriteSheet sheetA = EasyExcel.writerSheet(0, "A组").head(Person.class).build(); excelWriter.write(groupA, sheetA); WriteSheet sheetB = EasyExcel.writerSheet(1, "B组").head(Person.class).build(); excelWriter.write(groupB, sheetB); } } // POJO 类 public static class Person { private String name; private int age; // 构造函数、getter 和 setter 方法 } }
1条答案
按热度按时间bkkx9g8r1#