zoukankan      html  css  js  c++  java
  • vue+axios 与spring boot EasyExcel实现后台导出excel并下载

    一.后端:

    @Log("导出excel")
    @ApiOperation(value = "查询LawCaseCollectMain")
    @GetMapping(value = "/lawCaseCollectMain/download")
    public void download(HttpServletResponse response) throws IOException {
        // 这里注意 有同学反应使用swagger 会导致各种问题,请直接用浏览器或者用postman
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("测试", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            // 这里需要设置不关闭流
            EasyExcel.write(response.getOutputStream(), LawCaseCollectMainDownloadData.class)
                    .autoCloseStream(Boolean.FALSE)
                    .sheet("模板")
                    .doWrite(data());
        } catch (Exception e) {
            // 重置response
            response.reset();
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            JsonResponse.fail().setMsg("导出失败");
        }
    }

    二.前端:

    1.调用

     handleDown() {
          download().then((res) => {
            debugger;
            let blob = new Blob([res], { type: "application/xlsx" });
            let url = window.URL.createObjectURL(blob);
            const link = document.createElement("a"); // 创建a标签
            link.href = url;
            link.download = "download.xlsx"; // 重命名文件
            link.click();
            URL.revokeObjectURL(url);
          });
        },

    2.axios封装调用:

    export function download() {
      return request({
        url: '/BalDetail/download',
        method: 'get',
        responseType: 'blob'
      })
    }
    为了明天能幸福,今天付出再多也不后悔。
  • 相关阅读:
    应用提交到安卓应用市场需要注意哪些地方?
    chromedriver 下载
    缺陷与测试报告
    需求分析与测试计划、方案
    【转】使用信号监控 Django 模型对象字段值的变化
    缓存技术
    Tomcat 代码方式启动
    枚举类型 (币种例子)
    SpringMVC 常用注解
    HttpClient 教程
  • 原文地址:https://www.cnblogs.com/zlp520/p/15321097.html
Copyright © 2011-2022 走看看