vue使用asiox 下载后端返回的excel数据流

一、前端代码

javascript 复制代码
<template>
  <div class="hello">
    <h1>{{ msg }}</h1>
    <button style="color: brown" @click="exportExcel">excel导出</button>
  </div>
</template>

<script>
import axios from "axios";

export default {
  name: "HelloWorld",
  props: {
    msg: String,
  },
  methods: {
    exportExcel() {
      //请求
      console.log("test");
      axios
        .post("/api/export", {}, { responseType: "blob" })
        .then((res) => {
          //请求成功,触发then中的函数
          console.log(res);
          const blob = new Blob([res.data], {
            type: "application/vnd.ms-excel",
          });

          // 获取 获取响应头 heads 中的 filename 文件名
          let temp = res.headers["content-disposition"]
            .split(";")[1]
            .split("filename=")[1];
          // 把 %E9%AB%98%E6%84%8F%E5%90%91%E5%AD%A6%E5%91%98303.xlsx 转化成文字
          var fileName = decodeURIComponent(temp);
          //创建一个 a 标签
          const link = document.createElement("a");
          //不显示a标签
          link.style.display = "none";
          // 给a 标签的href属性赋值
          link.href = URL.createObjectURL(blob);
          link.setAttribute("download", fileName);
          //把a标签插入页面中
          document.body.appendChild(link);
          link.click();
          //点击之后移除a标签
          document.body.removeChild(link);
        })
        .catch((error) =>
          //请求失败,触发catch中的函数 可省略
          console.log(error)
        );
    },
  },
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h3 {
  margin: 40px 0 0;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}
</style>

二、后端代码

java 复制代码
    @PostMapping("/export")
    public void download(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码
        String fileName = URLEncoder.encode("交付对象及机构信息关系表", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        //自行编写查询数据库数据
        List<DataOrganizationDrugRelationExport> list = Lists.newArrayList();
        EasyExcel.write(response.getOutputStream(), DataOrganizationDrugRelationExport.class).head(DataOrganizationDrugRelationHeader.class).sheet("结果").doWrite(list);
    }
相关推荐
usdoc文档预览2 小时前
USDOC系列之PPT文件在线编辑-VUE前端实现在线浏览器在线编辑PPT文档
前端·vue.js·编辑器·office365·ppt在线编辑·office文件在线编辑·文件在线编辑
Java小卷2 小时前
面向 schema 编程,轻松搞定 Element 虚拟化表格复杂应用场景
vue.js·低代码·element
用户2705077725242 小时前
Agent 调工具卡住?3 种超时与取消策略实测对比
前端
LuHang3 小时前
我一行代码没写,把领导派的活做成了公司内部系统😭
前端
明月_清风3 小时前
Meta 开源了一个 AI Native 组件库:Astryx,150+ React 组件,专为 AI Agent 而设计
前端·人工智能·ai编程
仨孩子的爹3 小时前
Lodop、hiprint 与 vue-print-designer:打印控件选型对比
vue.js
用户850869276153 小时前
秒杀系统中 Shell 脚本并发控制 Bug:究竟哪里出了
前端
炫酷的大高个6 小时前
6 亿播放的闪身步,我用前端算出 15 倍重心差
前端·javascript
不可能片场6 小时前
内容运营自动化 配图折行别把词切两半
前端·electron
Lank_M6 小时前
HEIC转JPG统一转sRGB,P3高饱和色就回不来了
前端