一、POM文件中引入EasyExcel的依赖
<!--easyExcel工具--> <dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>2.2.6</version> </dependency>
二、使用EasyExcel复杂头写入和动态头,实时生成头写入的方法
service实现类中业务处理方法
@Override
public void reportExcelOut(String startDate, String endDate, HttpServletRequest request, HttpServletResponse response) {
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-disposition", "attachment;filename=HZB.xls");
// 这里注意 有同学反应使用swagger 会导致各种问题,请直接用浏览器或者用postman
try {
// 这里需要设置不关闭流
WriteCellStyle headWriteCellStyle = new WriteCellStyle();
//设置背景颜色
headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());