美文网首页
Excel导入数据(二进制方式)

Excel导入数据(二进制方式)

作者: world_7735 | 来源:发表于2021-08-18 18:33 被阅读0次
<template>
  <a-modal
    title="导入EXCEL"
    :width="600"
    :visible="visible"
    :confirmLoading="uploading"
    :maskClosable="false"
    @cancel="handleClose"
  >
    <a :href="downloadurl"  style="display:block;padding-bottom:10px;margin-bottom:10px;border-bottom:1px solid #f1f1f1;"><a-button>下载模版</a-button>
                          </a>
    <a-upload
      name="file"
      :multiple="true"
      accept=".xls,.xlsx"
      :fileList="fileList"
      :remove="handleRemove"
      :beforeUpload="beforeUpload"
    >
      <a-button>
        <a-icon type="upload" /> 选择导入文件
      </a-button>
      </a-upload>

      <template slot="footer">
        <a-button @click="handleClose">关闭</a-button>
        <a-button
          type="primary"
          @click="handleImport"
          :disabled="fileList.length === 0"
          :loading="uploading"
        >
          {{ uploading ? '上传中...' : '开始上传' }}
          </a-button>
      </template>

      </a-modal>
</template>

<script>
// import { postAction } from '@/api/manage'
import {

  g_importXlsform
} from '@/api/index';
export default {
  name: 'JImportModal',
  props: {
    url: {
      type: String,
      default: '',
      required: false
    },
    downloadurl:{
      type: String,
      default: '',
      required: false
    },
    biz: {
      type: String,
      default: '',
      required: false
    }
  },
  data () {
    return {
      visible: false,
      uploading: false,
      fileList: [],
      uploadAction: '',
      foreignKeys: ''
    }
  },
  watch: {
    url (val) {
      if (val) {
        const url = val.split("/");
        this.uploadAction = url[url.length - 1]
      }
    }
    
  },
  created () {
    const url = this.url.split("/");

    this.uploadAction = url[url.length - 1]
  },

  methods: {
    handleClose () {
      this.visible = false
    },
    show (arg) {
      this.fileList = []
      this.uploading = false
      this.visible = true
      this.foreignKeys = arg;
    },
    handleRemove (file) {
      const index = this.fileList.indexOf(file);
      const newFileList = this.fileList.slice();
      newFileList.splice(index, 1);
      this.fileList = newFileList
    },
    beforeUpload (file) {
      this.fileList = [...this.fileList, file]
      return false;
    },
    handleImport () {
      const { fileList } = this;
      const formData = new FormData();
      if (this.biz) {
        formData.append('isSingleTableImport', this.biz);
      }
      if (this.foreignKeys && this.foreignKeys.length > 0) {
        formData.append('foreignKeys', this.foreignKeys);
      }
      fileList.forEach((file) => {
        formData.append('files[]', file);
      });
      this.uploading = true
      console.log(this.uploadAction);
      g_importXlsform({ headId: this.uploadAction, data: formData }).then((data) => {
        this.uploading = false
        const res=data.data;
        if (res.success) {
          this.$message.success(res.message)
          this.visible = false
          this.$emit('ok')
        } else {
          this.$message.warning(res.message)
        }
      })
    }

  }
}
</script>

<style scoped>
</style>

使用

  <!-- 导入数据 -->
                  <JImportModal
                    ref="importModal"
                    downloadurl="https://static.98ep.com/Template/cgzt/商品导入模板测试数据.xls"
                    :url="getImportUrl()"
                    @ok="importOk"
                  ></JImportModal>
methods:{
//拿到地址或者token
 getImportUrl () {
      return '/online/cgform/api/importXls/' + '82cde47f4aad4ca2973cb683b6a898c9'
    },
//显示弹框
    handleImportXls () {
      this.$refs.importModal.show()
    },
//导入成功后重新拿到数据
    importOk () {
      // 获取table数据
      this.gettabdata(this.pagination.current, this.pagination.pageSize);
    },
}

相关文章

网友评论

      本文标题:Excel导入数据(二进制方式)

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