package com.example.jz.utils; import com.alibaba.excel.EasyExcel; import lombok.SneakyThrows; import javax.servlet.http.HttpServletResponse; import java.net.URLEncoder; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.Collection; public class EasyExcelUtils { /** * @Description 导出 * @Param [response , sheetName sheet名字, className 类名, collection data] * @return void **/ @SneakyThrows public static void export(HttpServletResponse response, String sheetName, Class className, Collection collection) { response.setContentType("application/vnd.ms-excel"); response.setCharacterEncoding("utf-8"); response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + ".xlsx"); EasyExcel.write(response.getOutputStream(), className) .sheet(sheetName) .doWrite(collection); } @SneakyThrows public static void export1(HttpServletResponse response, String sheetName, Class className, Collection collection, String fileName) { fileName = URLEncoder.encode(fileName, "UTF-8"); response.setContentType("application/vnd.ms-excel"); response.setCharacterEncoding("utf-8"); response.setHeader("Content-disposition", "attachment;filename*=" + fileName + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + ".xlsx"); EasyExcel.write(response.getOutputStream(), className) .sheet(sheetName) .doWrite(collection); } }