VUE调用js-file-download

这篇博客介绍了如何在前端项目中使用js-file-download库来导出Excel文件。首先通过npm安装该库,然后在代码中引入并调用。在导出Excel的逻辑中,发送请求获取后台返回的Blob文件流,最后利用fileDownload方法生成下载。文章还提到了接口返回值类型可以是'arraybuffer'或'blob'。
摘要由CSDN通过智能技术生成

一:安装 js-file-download

npm install js-file-download

二:引用 js-file-download

import fileDownload from "js-file-download";

三:调用 js-file-download

// 导出excel
      exportExcel(){
        //封装查询参数data
        let data = {
          param: this.searchData,
        };
        this.$confirm('确定导出?','操作提示',{
          confirmButtonText:'确定',
          cancelButtonText:'取消',
          type:'warning',
        }).then(()=>{
          //调用java接口,取到java返回的文件流,定义类型为blob
          exportExcel(data,{ responseType: "blob" }).then( (res) => {             
         
            let date= this.getDate();
            //调用fileDownload
            fileDownload(res.data, "陕西公司厂内车辆.xlsx");
            this.$message({
              message: '成功!',
              type: 'success'
            });
          })
        })
      },

四:声明接口返回值类型

        'responseType':"arraybuffer"或者'responseType':"blob"

        注:arraybuffer 和blob类型都行

export const exportExcel = (data) => {
  return axios({
    url: prefix + 'base/BasePlanDay/exportExcel',
    headers: {'isMork': false},
    method: 'post',
    data,
    'responseType':"arraybuffer"
  })
}

END...SEE YOU

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值