栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

vue中使用axios下载java后台返回文件流导出excel文档

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

vue中使用axios下载java后台返回文件流导出excel文档

需求:
表格上方有一个导出按钮,点击后,调用后台接口,后台返回数据流,下载成功后并在本地可以打开excel。


代码实现:

金税格式数据导出
import Storage from '@/utils/localStorage'
import ApiUrlconfig from '@/module/api/invoiceManage'
import axios from 'axios'
		formatExportFile () {
		  this.formatExportFileList(true,true)
		},
		setHeaders() {
          let loginInfo = Storage.get('loginInfo') || {}
          console.log('loginInfo', loginInfo);
          let headers = {}
          headers['X-Store'] = JSON.stringify({storeSysNo: loginInfo.StoreSysNo, storeNo: loginInfo.StoreNO})
          headers['Authorization'] = `${loginInfo.token_type} ${loginInfo.access_token}`
          headers['Content-Type'] = 'application/json;charset=UTF-8'
          return headers
        },
        async formatExportFileList(research, isExport=false) {
          this.loading = true
          let pageSize=isExport? 0 :this.pageSize
          // 这里的参数和列表请求参数一致
          let params = {
            // accountSetCode:this.accountSet,
            pageSize: pageSize,
          };
          if (research && !isExport) {
            // 重新搜索
            this.currentPage = 1
          }
          params.pageNum = isExport? 1 :this.currentPage
          if (this.accountSet.length != 0) {
              params.accountSet = this.accountSet;
          }
          if (this.searchForm.fphm.length != 0) {
            params.fphm = this.searchForm.fphm;
          }
          if (this.searchForm.custName.length != 0) {
            params.custName = this.searchForm.custName;
          }
          if (this.searchForm.invType.length != 0) {
            params.invType = this.searchForm.invType;
          }
          if (this.searchForm.orderNo.length != 0) {
            params.orderNo = this.searchForm.orderNo;
          }
          if (this.searchForm.dateType.length != 0) {
            params.dateType = this.searchForm.dateType;
          }
          if (this.searchForm.invoiceFrom.length != 0) {
            params.invoiceFrom = this.searchForm.invoiceFrom;
          }
          if (typeof (this.searchForm.startDate) != "undefined" && this.searchForm.startDate!=null && this.searchForm.startDate!='') {
            params.startDate =this.searchForm.startDate + " 00:00:00";
          }
          if (typeof (this.searchForm.endDate) != "undefined" && this.searchForm.endDate!=null && this.searchForm.endDate!='') {
            params.endDate = this.searchForm.endDate + " 23:59:59";
          }
          if(typeof params.endDate !="undefined" && typeof params.startDate !="undefined" && (new Date(params.endDate.replace(/-/g,"/"))).getTime()<(new Date(params.startDate.replace(/-/g,"/"))).getTime()) {
            this.$tip.notify("结束日期小于开始日期","warning")
            return;
          }
          // 通过axios调用接口
          await axios({
            method: 'POST',
            url: ApiUrlconfig.getReMakeInvoice,
            responseType: 'blob',
            headers: this.setHeaders(),
            data: params
          }).then(res => {
            console.log('res', res)
            if (!res.msg) {
              this.exportLoading = false
              // 这里根据后端返回使用res或res.data
              var blob = new Blob([res], {type: 'application/ms-excel'});
              var elink = document.createElement('a');
              elink.download = '销项发票管理' + '.xlsx';
              elink.href = URL.createObjectURL(blob);
              document.body.appendChild(elink);
              elink.click(); //点击下载
              document.body.removeChild(elink); //下载完成移除元素
              window.URL.revokeObjectURL(elink); //释放掉blob对象
              this.loading = false
              return
            }
            throw new Error(res.msg)
          }).catch(err => {
            this.loading = false
            this.$notify.error({
              title: '提示',
              message: err.message
            })
          })
        },

参考文章:
vue中使用axios下载java后台返回文件流导出excel文档

前端调用接口导出excel文件时浏览器开发者工具中network里response有返回值但乱码,使用自己封装过的axios返回值却为undefined

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/750553.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号