SpringBoot 导出Excel 文件名乱码 解决方案

废话不多说 直接上代码 直接copy 不好用你来打我

/**
     * @param request      请求
     * @param response     响应
     * @param data         数据
     * @param tempPlath    临时普拉斯
     * @param downFileName 下文件名称
     * @author xiaoyc
     * @date 2023/04/10
     * @description 做出口excel
     */
    private void doExportExcel(HttpServletRequest request, HttpServletResponse response, Object data, String tempPlath, String downFileName) throws IOException {
        InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream(tempPlath);
        ServletOutputStream out = response.getOutputStream();
        if (isIE(request)) {
            downFileName = URLEncoder.encode(downFileName, "UTF8");
        } else {
            downFileName = new String(downFileName.getBytes("UTF-8"), "ISO-8859-1");
        }
        // 可自行定义编码格式
        response.setHeader("content-disposition", "attachment;filename=" + downFileName);
        response.addHeader("DownloadFileName", URLEncoder.encode(downFileName, "utf-8"));
        //清除jsp编译html文件的空白,防止excel出现空行
        response.flushBuffer();
        EasyExcel.write(out).withTemplate(resourceAsStream).sheet().doFill(data);
    }

    private static boolean isIE(HttpServletRequest request) {
        return request.getHeader("USER-AGENT").toLowerCase().indexOf("msie") > 0 || request.getHeader("USER-AGENT").toLowerCase().indexOf("rv:11.0") > 0;
    }

你可能感兴趣的:(spring,boot,excel,java)