使用EasyExcel实现多sheet模板导出
依赖导入
我这边使用的是2.1.6,大家可以自行选择版本
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.1.6</version>
<dependency>
实体类
package com.ruoyi.system.domain.vo;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.annotation.write.style.ContentRowHeight;
import com.alibaba.excel.annotation.write.style.HeadRowHeight;
import com.ruoyi.common.annotation.Excel;
import com.ruoyi.common.core.domain.BaseEntity;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
/**
* 测试对象
*
* @author wxl
* @date 2024-04-18
*/
@ColumnWidth(25)
@ContentRowHeight(30)
@HeadRowHeight(50)
@Data
public class TestVo
{
/** 签字日期 */
@ExcelProperty(value = "签字日期")
private String signTime;
/** 填报日期 */
@ExcelProperty(value = "填报日期")
private String fillTime;
/** 事由 */
@ExcelProperty(value = "事由")
private String reason;
/** 类型 */
@ExcelProperty(value = "类型")
private String type;
/** 申请人 */
@ExcelProperty(value = "申请人")
private String applicant;
/** 经办人 */
@ExcelProperty(value = "经办人")
private String attn;
/** 份数 */
@ExcelProperty(value = "份数")
private String num;
/** 备注 */
@ExcelProperty(value = "备注")
private String remark;
}
多实体类文件导出
//out为OutputStream
ExcelWriter excelWriter = EasyExcelFactory.write(out).build();
//设置内容样式
WriteCellStyle contentStyle = new WriteCellStyle();
contentStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);//居中
contentStyle.setWrapped(true);//自动换行
//设置头部样式
WriteCellStyle headerStyle = new WriteCellStyle();
headerStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
//设置策略
HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headerStyle, contentStyle);
WriteSheet sheet1 = EasyExcelFactory.writerSheet(0, "sheetName1").head(TestVo.class).registerWriteHandler(horizontalCellStyleStrategy).build();
WriteSheet sheet2 = EasyExcelFactory.writerSheet(1, "sheetName2").head(TestVo.class).registerWriteHandler(horizontalCellStyleStrategy).build();
WriteSheet sheet3 = EasyExcelFactory.writerSheet(2, "sheetName3").head(TestVo.class).registerWriteHandler(horizontalCellStyleStrategy).build();
// getWithSeal方法为获取当前sheet页所要填充的数据集合List
excelWriter.write(getWithSeal(), sheet1 );
excelWriter.write(getWithSeal(), sheet2 );
excelWriter.write(getWithSeal(), sheet3 );
excelWriter.finish();
以上便是使用EasyExcel实现多sheet页数据导出,感谢观看