package tech.riemann.ims.utils; import club.zhcs.lina.starter.exception.BizException; import cn.idev.excel.FastExcel; import jakarta.servlet.http.HttpServletResponse; import lombok.experimental.UtilityClass; import java.io.IOException; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.util.Collection; /** * @author mayong * @since 2025/3/15 16:17 */ @UtilityClass public class ExcelUtil { public void exportExcel(HttpServletResponse response, String excelName, String sheetName, Class clazz, Collection result) { response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setCharacterEncoding("utf-8"); String fileName = URLEncoder.encode(excelName, StandardCharsets.UTF_8).replaceAll("\\+", "%20"); response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx"); try { FastExcel.write(response.getOutputStream(), clazz) .sheet(sheetName) .doWrite(result); } catch (IOException e) { throw BizException.create("导出Excel失败"); } } }