对EasyExcel进一步封装,方便自己使用。
依赖
<dependency>
<groupId>com.alibabagroupId>
<artifactId>easyexcelartifactId>
<version>3.1.1version>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.5.1version>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatisplus-spring-boot-starterartifactId>
<version>1.0.5version>
dependency>
<dependency>
<groupId>cn.hutoolgroupId>
<artifactId>hutool-coreartifactId>
<version>5.5.1version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
public class EasyExcelUtil {
@Data
@Builder
public static class ExcelParam{
/** 查询 Mapper */
private BaseMapper baseMapper;
/** Lambda查詢方式 */
private LambdaQueryWrapper lambdaQueryWrapper;
/** 页码,默认从1开始 */
private Integer pageNo = 1;
/** 分页条数,,默认每个sheet 1000 条数据 */
private Integer pageSize = 1000;
/** 用于存放查询到的結果,让Excel生成 */
private Class<?> respClazz;
/** 生成的Excel 名称,不加后缀 */
private String fileName;
/** Excel sheet名称 */
private String sheetName;
}
public static void exportExcel(ExcelParam excelParam, HttpServletResponse response){
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setCharacterEncoding("utf-8");
response.setHeader(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, HttpHeaders.CONTENT_DISPOSITION);
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + excelParam.getFileName() + ".xls");
try (
ServletOutputStream outputStream = response.getOutputStream();
ExcelWriter excelWriter = EasyExcel.write(outputStream,excelParam.getRespClazz()).build();
) {
Page page = new Page(excelParam.getPageNo(), excelParam.getPageSize());
page = (Page) excelParam.getBaseMapper().selectPage(page, excelParam.getLambdaQueryWrapper());
/** 构建 */
WriteSheet writeSheet1 = EasyExcel.writerSheet(1, excelParam.getSheetName() + "第" + excelParam.getPageNo() + "页").build();
/** 获取总数 */
Long totalPage = page.getPages();
List records = page.getRecords();
/** 写入内容 */
excelWriter.write(records, writeSheet1);
writeSheet1 = null; // GC
// 若为空表
if (CollUtil.isEmpty(page.getRecords())) {
/** 生成完毕 */
excelWriter.finish();
/** 立即刷回 */
outputStream.flush();
return;
}
for (int i = excelParam.pageNo + 1,index = 2; i <= totalPage; i++,index++) {
/** 清空*/
records.clear();
WriteSheet writeSheet = EasyExcel.writerSheet(index, excelParam.getSheetName() + "第" + i + "页").build();
page.setCurrent(i);
/** 新的查询 */
page = (Page) excelParam.getBaseMapper().selectPage(page,excelParam.getLambdaQueryWrapper());
records = page.getRecords();
/** 输入内容內容 */
excelWriter.write(records, writeSheet);
}
/** 生成完毕 */
excelWriter.finish();
/** 立即刷回 */
outputStream.flush();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
@RestController
@Slf4j
@RequestMapping("/test")
@Api(tags = "测试模块")
public class TestController {
@Autowired
ApplicationMapper applicationMapper;
@GetMapping
public void test(HttpServletResponse httpServletResponse){
EasyExcelUtil.ExcelParam p = EasyExcelUtil.ExcelParam.builder()
.baseMapper(applicationMapper)
.lambdaQueryWrapper(new LambdaQueryWrapper<Application>()
.eq(true,Application::getApplicationStatus,0)
)
.pageNo(2)
.respClazz(RespTest.class)
.pageSize(2)
.fileName("test")
.sheetName("测试sheet")
.build();
EasyExcelUtil.exportExcel(p,httpServletResponse);
}
}
public class RespTest {
@ExcelProperty(value = {"测试","id"},index = 0)
private Long id;
@ExcelProperty(value={"测试","名称"},index = 1)
private String applicationName;
@ExcelProperty(value = {"测试","状态"},index = 2)
private Short applicationStatus;
@ExcelProperty(value = {"测试","时间"},index = 3)
private LocalDateTime createTime;
}
@Data
@TableName("tb_application")
@ApiModel(value = "Application对象", description = "")
public class Application implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty("ID")
@TableId(value = "id", type = IdType.ASSIGN_ID)
private Long id;
@ApiModelProperty("应用名称")
private String applicationName;
@ApiModelProperty("状态")
private Short applicationStatus;
@ApiModelProperty("创建时间")
private LocalDateTime createTime;
}