EasyExcel导出包含多个sheet页的Excel
1.引入依赖
引入如下的EasyExcel的依赖,或直接下载jar包
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.1.1</version>
</dependency>
2.构建数据封装bean(测试数据封装bean,除了类中的注解,其他信息无需过多关注)
2.1User类
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.annotation.write.style.ContentStyle;
import com.alibaba.excel.enums.poi.HorizontalAlignmentEnum;
import com.alibaba.excel.enums.poi.VerticalAlignmentEnum;
// @ColumnWidth作用:设置全局列宽为20
@ColumnWidth(20)
// @ContentStyle作用:设置全局内容居中
@ContentStyle(verticalAlignment = VerticalAlignmentEnum.CENTER, horizontalAlignment = HorizontalAlignmentEnum.CENTER)
public class User {
// @ExcelProperty作用:设置列标题名称
@ExcelProperty("姓名")
private String name;
@ExcelProperty("年龄")
private int age;
@ExcelProperty("身高")
private int height;
public User() {
}
public User(String name, int age, int height) {
this.name = name;
this.age = age;
this.height = height;
}
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 int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
", height=" + height +
'}';
}
}}
}", height=" + height +
'}';
}
}
2.2Department类
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.annotation.write.style.ContentStyle;
import com.alibaba.excel.enums.poi.HorizontalAlignmentEnum;
import com.alibaba.excel.enums.poi.VerticalAlignmentEnum;
// 设置全局列宽为20
@ColumnWidth(20)
// 设置全局内容居中
@ContentStyle(verticalAlignment = VerticalAlignmentEnum.CENTER, horizontalAlignment = HorizontalAlignmentEnum.CENTER)
public class Department {
// @ExcelProperty作用:设置列标题名称
@ExcelProperty({"部门基本属性", "部门名称"})
private String deptName;
@ExcelProperty({"部门基本属性", "部门Code"})
private String deptCode;
@ExcelProperty({"部门其他属性", "部门地址"})
private String deptLocation;
@ExcelProperty({"部门其他属性", "部门类型"})
private String deptType;
public Department() {
}
public Department(String deptName, String deptCode, String deptLocation, String deptType) {
this.deptName = deptName;
this.deptCode = deptCode;
this.deptLocation = deptLocation;
this.deptType = deptType;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
public String getDeptCode() {
return deptCode;
}
public void setDeptCode(String deptCode) {
this.deptCode = deptCode;
}
public String getDeptLocation() {
return deptLocation;
}
public void setDeptLocation(String deptLocation) {
this.deptLocation = deptLocation;
}
public String getDeptType() {
return deptType;
}
public void setDeptType(String deptType) {
this.deptType = deptType;
}
@Override
public String toString() {
return "Department{" +
"deptName='" + deptName + '\'' +
", deptCode='" + deptCode + '\'' +
", deptLocation='" + deptLocation + '\'' +
", deptType='" + deptType + '\'' +
'}';
}
} ", deptType='" + deptType + '\'' +
'}';
}
}
2.3Goods类
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.annotation.write.style.ContentStyle;
import com.alibaba.excel.enums.poi.HorizontalAlignmentEnum;
import com.alibaba.excel.enums.poi.VerticalAlignmentEnum;
// 设置全局列宽为20
@ColumnWidth(20)
// 设置全局内容居中
@ContentStyle(verticalAlignment = VerticalAlignmentEnum.CENTER, horizontalAlignment = HorizontalAlignmentEnum.CENTER)
public class Goods {
// @ExcelProperty作用:设置列标题名称
@ExcelProperty({"零食基本属性", "名称"})
private String goodsName;
@ExcelProperty({"零食基本属性", "类型"})
private String goodsType;
@ExcelProperty({"相关时间", "生产日期"})
private String goodsProduceDate;
@ExcelProperty({"相关时间", "保质期"})
private String goods2Date;
@ExcelProperty({"工厂", "核心厂"})
private String goodsFactoryAddressMain;
@ExcelProperty({"工厂", "副厂"})
private String goodsFactoryAddressChild;
public Goods() {
}
public Goods(String goodsName, String goodsType, String goodsProduceDate, String goods2Date, String goodsFactoryAddressMain, String goodsFactoryAddressChild) {
this.goodsName = goodsName;
this.goodsType = goodsType;
this.goodsProduceDate = goodsProduceDate;
this.goods2Date = goods2Date;
this.goodsFactoryAddressMain = goodsFactoryAddressMain;
this.goodsFactoryAddressChild = goodsFactoryAddressChild;
}
public String getGoodsName() {
return goodsName;
}
public void setGoodsName(String goodsName) {
this.goodsName = goodsName;
}
public String getGoodsType() {
return goodsType;
}
public void setGoodsType(String goodsType) {
this.goodsType = goodsType;
}
public String getGoodsProduceDate() {
return goodsProduceDate;
}
public void setGoodsProduceDate(String goodsProduceDate) {
this.goodsProduceDate = goodsProduceDate;
}
public String getGoods2Date() {
return goods2Date;
}
public void setGoods2Date(String goods2Date) {
this.goods2Date = goods2Date;
}
public String getGoodsFactoryAddressMain() {
return goodsFactoryAddressMain;
}
public void setGoodsFactoryAddressMain(String goodsFactoryAddressMain) {
this.goodsFactoryAddressMain = goodsFactoryAddressMain;
}
public String getGoodsFactoryAddressChild() {
return goodsFactoryAddressChild;
}
public void setGoodsFactoryAddressChild(String goodsFactoryAddressChild) {
this.goodsFactoryAddressChild = goodsFactoryAddressChild;
}
@Override
public String toString() {
return "Goods{" +
"goodsName='" + goodsName + '\'' +
", goodsType='" + goodsType + '\'' +
", goodsProduceDate='" + goodsProduceDate + '\'' +
", goods2Date='" + goods2Date + '\'' +
", goodsFactoryAddressMain='" + goodsFactoryAddressMain + '\'' +
", goodsFactoryAddressChild='" + goodsFactoryAddressChild + '\'' +
'}';
}
}
3.SheetInfoBean类
SheetInfoBean是用来封装sheet页相关信息的,使用bean将sheet页相关信息封装起来,可以使代码更加简洁优雅,可以根据实际需求,自行设计bean
import java.util.List;
public class SheetInfoBean {
/**
* sheet页名称
*/
private String sheetName;
/**
* sheet标题bean
*/
private Class<?> headClass;
/**
* sheet页数据
*/
private List<?> dataList;
public SheetInfoBean() {
}
public SheetInfoBean(String sheetName, Class<?> headClass, List<?> dataList) {
this.sheetName = sheetName;
this.headClass = headClass;
this.dataList = dataList;
}
public String getSheetName() {
return sheetName;
}
public void setSheetName(String sheetName) {
this.sheetName = sheetName;
}
public Class<?> getHeadClass() {
return headClass;
}
public void setHeadClass(Class<?> headClass) {
this.headClass = headClass;
}
public List<?> getDataList() {
return dataList;
}
public void setDataList(List<?> dataList) {
this.dataList = dataList;
}
@Override
public String toString() {
return "SheetInfoBean{" +
"sheetName='" + sheetName + '\'' +
", headClass=" + headClass +
", dataList=" + dataList +
'}';
}
}
4.测试
以下是测试代码
@Test
void test04() {
// 构造用户数据
List<User> userList = new ArrayList<>();
userList.add(new User("小红", 18, 168));
userList.add(new User("小白", 17, 165));
userList.add(new User("小蓝", 18, 169));
// 构造部门数据
List<Department> deptList = new ArrayList<>();
deptList.add(new Department("java开发部", "DEV001", "南京", "总部"));
deptList.add(new Department("测试部", "TEST001", "上海", "研发中心"));
deptList.add(new Department("财务", "ECONOMY001", "南京", "总部"));
// 构造产品数据
List<Goods> goodsList = new ArrayList<>();
goodsList.add(new Goods("小面包", "速食", "2023-07-21", "3天", "成都", "上海"));
goodsList.add(new Goods("旺旺仙贝", "膨化食品", "2023-07-21", "3天", "仙贝中心", "山寨仙贝厂"));
goodsList.add(new Goods("领克03", "汽车", "2023-07-21", "永久", "领克工厂", "领克副厂"));
// 构造各个sheet页相关信息
List<SheetInfoBean> sheetInfoList = new LinkedList<>();
sheetInfoList.add(new SheetInfoBean("用户信息", User.class, userList));
sheetInfoList.add(new SheetInfoBean("部门信息", Department.class, deptList));
sheetInfoList.add(new SheetInfoBean("产品信息", Goods.class, goodsList));
// 导出文件
File file = new File("C:\\Users\\Administrator\\Desktop\\多sheet导出测试.xlsx");
try(ExcelWriter excelWriter = EasyExcel.write(file).build()) {
WriteSheet writeSheet;
for (SheetInfoBean bean : sheetInfoList) {
// 构建sheet对象
writeSheet = EasyExcel.writerSheet(bean.getSheetName()).head(bean.getHeadClass()).build();
// 写出sheet数据
excelWriter.write(bean.getDataList(), writeSheet);
}
// 关流
excelWriter.finish();
} catch (Exception e) {
// do something you want
}
}
5.运行结果
6.总结
以上是EasyExcel一个Excel文件导出多个sheet页的demo代码,其中重点代码为
try(ExcelWriter excelWriter = EasyExcel.write(file).build()) {
WriteSheet writeSheet;
for (SheetInfoBean bean : sheetInfoList) {
// 构建sheet对象
writeSheet = EasyExcel.writerSheet(bean.getSheetName()).head(bean.getHeadClass()).build();
// 写出sheet数据
excelWriter.write(bean.getDataList(), writeSheet);
}
// 关流
excelWriter.finish();
} catch (Exception e) {
// do something you want
}
大家可以根据自己的需求,将该代码片段进行改造或封装成工具类,以适应自己的业务需求