表格导入导出(字节流)

HTML 代码
        <a-button @click="saveData('submit')" v-if="formStatus === '已保存'">提交</a-button>
        <input type="file" @change="importExcel($event)" v-show="false" ref="file" />
        <a-button v-if="formStatus === '已保存'" @click="handlickImport">导入</a-button>
        <a-button v-if="id" @click="handleClickExport">导出</a-button>
        <a-button v-if="formStatus === '已保存'" @click="handlickDownloadTemplate"
          >下载模板</a-button
        >

JS 代码
  handlickImport() {
    (this.$refs.file as any).click();
  }
  importExcel(event: any) {
    const file = event.target.files[0];
    let params = new FormData();
    params.append("file", file);
    const schemaCode: any = this.schemaCode;
    params.append("schemaCode", schemaCode);
    this.import(params).then((res: any) => {
      if (res.errcode === 0) {
        let externalClaimAccountChild = res.data.externalClaimAccountChild;
        externalClaimAccountChild.map((item: any) => {
          for (let key in item) {
            for (let key in item) {
              if (key.includes("time") || key.includes("Time")) {
                item[key] = item[key] ? moment(item[key]) : null;
              } else if (key.includes("Cost") || key.includes("cost")) {
                item[key] = item[key] ? Number(Number(item[key]).toFixed(2)) : null;
              }
            }
          }
        });
        this.externalChangeAccountChild = externalClaimAccountChild;
        this.$message.success("导入成功", 1, () => {
          event.target.value = "";
        });
      } else {
        this.$message.success("导入失败", 1, () => {
          event.target.value = "";
        });
      }
    });
  }




  handlickDownloadTemplate() {
    let params = new FormData();
    const schemaCode: any = this.schemaCode;
    params.append("schemaCode", schemaCode);
    this.downLoad(params).then((res: any) => {
      if (res && res.errcode && res.errcode !== 0) {
        this.$message.error("下载模板失败");
      } else if (res) {
        const blob = new Blob([res], {
          type: "application/vnd.ms-excel;charset=UTF-8"
        });
        const date = new Date();
        const mounth = date.getMonth() > 8 ? date.getMonth() + 1 : `0${date.getMonth() + 1}`;
        const days = date.getDate() > 9 ? date.getDate() : `0${date.getDate()}`;
        const hours = date.getHours() > 9 ? date.getHours() : `0${date.getHours()}`;
        const minutes = date.getMinutes() > 9 ? date.getMinutes() : `0${date.getMinutes()}`;
        const seconds = date.getSeconds() > 9 ? date.getSeconds() : `0${date.getSeconds()}`;
        const stamp = `${date.getFullYear()}${mounth}${days}${hours}${minutes}${seconds}`;
        const fileName = `对外索赔台账模板下载${stamp}.xlsx`;
        const a = document.createElement("a");
        const url = URL.createObjectURL(blob);
        a.download = fileName;
        a.href = url;
        a.click();
        URL.revokeObjectURL(url);
        this.$message.destroy();
      }
    });
  }
  handleClickExport() {
    this.export({
      objectId: this.id,
      schemaCode: this.schemaCode
    }).then((res: any) => {
      if (res && res.errcode && res.errcode !== 0) {
        this.$message.error("导出失败");
      } else if (res) {
        const blob = new Blob([res], {
          type: "application/vnd.ms-excel;charset=UTF-8"
        });
        const date = new Date();
        const mounth = date.getMonth() > 8 ? date.getMonth() + 1 : `0${date.getMonth() + 1}`;
        const days = date.getDate() > 9 ? date.getDate() : `0${date.getDate()}`;
        const hours = date.getHours() > 9 ? date.getHours() : `0${date.getHours()}`;
        const minutes = date.getMinutes() > 9 ? date.getMinutes() : `0${date.getMinutes()}`;
        const seconds = date.getSeconds() > 9 ? date.getSeconds() : `0${date.getSeconds()}`;
        const stamp = `${date.getFullYear()}${mounth}${days}${hours}${minutes}${seconds}`;
        const fileName = `对外索赔台账导出${stamp}.xlsx`;
        const a = document.createElement("a");
        const url = URL.createObjectURL(blob);
        a.download = fileName;
        a.href = url;
        a.click();
        URL.revokeObjectURL(url);
        this.$message.destroy();
      }
    });
  }


接口

  downLoad(params): Promise<Common.Data> {
    const url = "/api/swat/StandingBook/downLoad";
    return axios.post(url, params, {
      responseType: "arraybuffer",
      headers: { "Content-Type": "application/octet-stream" }
    });
  }

  export(params): Promise<Common.Data> {
    const url = "/api/swat/StandingBook/mstandingBookExport";
    return axios.post(url, params, { responseType: "arraybuffer" });
  }
  
  import(params: any): Promise<Common.Data> {
    let url: any = "/api/sawt/StandingBook/mstandingBookImport";
    return axios.post(url, params, {
      headers: { "Content-Type": "multipart/form-data" }
    });
  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值