EasyExcel创建动态表头,一份Sheet中创建多份子表头
作者:互联网
1、引入EasyExcel
<dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>2.2.8</version> </dependency>
2、准备表格头与表格数据
//表格头 ArrayList<List<String>> head = new ArrayList<>(); List<String> head1 = new ArrayList<>(); head1.add("序号"); head.add(head1); List<String> head2 = new ArrayList<>(); head2.add("姓名"); head.add(head2); List<String> head3 = new ArrayList<>(); head3.add("年龄"); head.add(head3); List<String> head4 = new ArrayList<>(); head4.add("地址"); head.add(head4); List<String> head5 = new ArrayList<>(); head5.add("时间"); head.add(head5); //内容 List<List<String>> bodyList = new ArrayList<>(); List<String> body = new ArrayList<>(); body.add("1"); body.add("张三"); body.add("15"); body.add("北京"); body.add("2022-09-02"); bodyList.add(body); List<String> body1 = new ArrayList<>(); body1.add("2"); body1.add("李四"); body1.add("20"); body1.add("天津"); body1.add("2022-09-03"); bodyList.add(body1);
3、使用表格创建多份子表并设置表格头
String file = "D:/Demo/excel"; File dir = new File(file); dir.mkdirs(); String fileName = file+"/demo.xlsx"; //创建EXCEL对象 ExcelWriterBuilder builder = EasyExcel.write(fileName); //设置处理器,合并单元格,列宽处理器等 builder.registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()); //获取writer对象 ExcelWriter writer= builder.build(); WriteSheet sheet = EasyExcel.writerSheet(0, "个人信息").build(); //创建两个表格并写入表头 WriteTable tableOne = EasyExcel.writerTable(0).needHead(Boolean.TRUE).build(); tableOne.setHead(head); WriteTable tableTwo = EasyExcel.writerTable(1).needHead(Boolean.TRUE).build(); tableTwo.setHead(head); //写入两次数据,每次会自动生成一个表头 writer.write(bodyList,sheet,tableOne); writer.write(bodyList,sheet,tableTwo); writer.finish();
4、完整函数
public static void generateExcel(){ //表格头 ArrayList<List<String>> head = new ArrayList<>(); List<String> head1 = new ArrayList<>(); head1.add("序号"); head.add(head1); List<String> head2 = new ArrayList<>(); head2.add("姓名"); head.add(head2); List<String> head3 = new ArrayList<>(); head3.add("年龄"); head.add(head3); List<String> head4 = new ArrayList<>(); head4.add("地址"); head.add(head4); List<String> head5 = new ArrayList<>(); head5.add("时间"); head.add(head5); //内容 List<List<String>> bodyList = new ArrayList<>(); List<String> body = new ArrayList<>(); body.add("1"); body.add("张三"); body.add("15"); body.add("北京"); body.add("2022-09-02"); bodyList.add(body); List<String> body1 = new ArrayList<>(); body1.add("2"); body1.add("李四"); body1.add("20"); body1.add("天津"); body1.add("2022-09-03"); bodyList.add(body1); String file = "D:/Demo/excel"; File dir = new File(file); dir.mkdirs(); String fileName = file+"/demo.xlsx"; //创建EXCEL对象 ExcelWriterBuilder builder = EasyExcel.write(fileName); //设置处理器,合并单元格,列宽处理器等 builder.registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()); //获取writer对象 ExcelWriter writer= builder.build(); WriteSheet sheet = EasyExcel.writerSheet(0, "个人信息").build(); //创建两个表格并写入表头 WriteTable tableOne = EasyExcel.writerTable(0).needHead(Boolean.TRUE).build(); tableOne.setHead(head); WriteTable tableTwo = EasyExcel.writerTable(1).needHead(Boolean.TRUE).build(); tableTwo.setHead(head); //写入两次数据,每次会自动生成一个表头 writer.write(bodyList,sheet,tableOne); writer.write(bodyList,sheet,tableTwo); writer.finish(); }
5、生成的最后效果
标签:body,head,Sheet,创建,ArrayList,List,表头,add,new 来源: https://www.cnblogs.com/sun-in-sky/p/16668859.html