引入jar
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.1.0</version>
</dependency>
代码
public static void main(String[] args) {
String fileName = "/Users/qweasdzxc/Downloads/text.xlsx";
ExcelWriter excelWriter = EasyExcelFactory.write(fileName).build();
List<List<String>> head1 =new ArrayList<>();
head1.add(Collections.singletonList("姓名"));
head1.add(Collections.singletonList("年龄"));
head1.add(Collections.singletonList("性别"));
List<List<Object>> data1 = new ArrayList<>();
data1.add(Arrays.asList("张三", 20, "男"));
data1.add(Arrays.asList("李四", 25, "女"));
WriteSheet writeSheet = EasyExcelFactory.writerSheet(0, "学生信息1").head(head1).automaticMergeHead(false).build();
excelWriter.write(data1, writeSheet);
List<List<String>> head2 =new ArrayList<>();
head2.add(Collections.singletonList("学科"));
head2.add(Collections.singletonList("分数"));
List<List<Object>> data2 = new ArrayList<>();
data2.add(Arrays.asList("数学", 90));
data2.add(Arrays.asList("英语", 85));
writeSheet = EasyExcelFactory.writerSheet(1, "学生信息2").head(head2).build();
excelWriter.write(data2, writeSheet);
excelWriter.finish();
}
效果