美文网首页
vue 导出excel/下载zip

vue 导出excel/下载zip

作者: 前端陈陈陈 | 来源:发表于2020-08-04 22:38 被阅读0次

vue 导出excel

1、如果后台数据是json格式的话就用如下方法:

  exports() {
      //导出
      var url = " ";
      this.$axios
        ._axios({
          url: url,
          method: "get"
          //  responseType: "blob",
        })
        .then(res => {
          console.log(res);
          this.$exportExcel(res.data, "商品信息");
        })
        .catch(err => {
          console.log(err);
        });
    },
       $exportExcel(data, name) {
      console.log(data);
      console.log(name);
      let blob = new Blob([data], {
        type: "application/vnd.ms-excel"
      });
      let url = window.URL.createObjectURL(blob);
      let a = document.createElement("a");
      a.href = url;
      a.download = name + ".xlsx";
      a.click();
      window.URL.revokeObjectURL(url);
    },

2、如果后台数据是二进制数据流就用如下方法:

    exportsn() {
      //导出
      var url = " ";
      let searchlist = {
        search: {
          is_del: this.formSearch.search.is_del,
          category_id: this.formSearch.search.category_id,
          content: this.formSearch.search.content,
          type: this.formSearch.search.type,
          brand_id: this.formSearch.search.brand_id
        },
        id: this.formShelves.id
      };
      this.$axios
        .axiose({
          url: url,
          data: qs.stringify(searchlist),
          method: "post",
          responseType: "blob"
        })
        .then(result => {
    if (window.navigator.msSaveOrOpenBlob) {  //兼容ie 浏览器
            let fileName = "商品清单" + ".xlsx";
            navigator.msSaveBlob(result, fileName);
          } else {
            const blob = result;
            const reader = new FileReader();
            reader.readAsDataURL(blob);
            reader.onload = e => {
              const a = document.createElement("a");
              a.download = `商品清单.xlsx`;
              a.href = e.target.result;
              document.body.appendChild(a);
              a.click();
              document.body.removeChild(a);
            };
          }
        })
        .catch(err => {
          console.log(err);
        });
    },

相关文章

网友评论

      本文标题:vue 导出excel/下载zip

      本文链接:https://www.haomeiwen.com/subject/agugrktx.html