Excel導入數據(二進制方式)

<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);
    },
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末庶艾,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子蓉媳,更是在濱河造成了極大的恐慌跛溉,老刑警劉巖栖榨,帶你破解...
    沈念sama閱讀 218,755評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件柄慰,死亡現場離奇詭異踢械,居然都是意外死亡奸晴,警方通過查閱死者的電腦和手機,發(fā)現死者居然都...
    沈念sama閱讀 93,305評論 3 395
  • 文/潘曉璐 我一進店門诈胜,熙熙樓的掌柜王于貴愁眉苦臉地迎上來豹障,“玉大人,你說我怎么就攤上這事焦匈⊙” “怎么了?”我有些...
    開封第一講書人閱讀 165,138評論 0 355
  • 文/不壞的土叔 我叫張陵缓熟,是天一觀的道長累魔。 經常有香客問我,道長够滑,這世上最難降的妖魔是什么垦写? 我笑而不...
    開封第一講書人閱讀 58,791評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮彰触,結果婚禮上梯投,老公的妹妹穿的比我還像新娘。我一直安慰自己况毅,他們只是感情好分蓖,可當我...
    茶點故事閱讀 67,794評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著尔许,像睡著了一般么鹤。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上味廊,一...
    開封第一講書人閱讀 51,631評論 1 305
  • 那天蒸甜,我揣著相機與錄音,去河邊找鬼余佛。 笑死柠新,一個胖子當著我的面吹牛,可吹牛的內容都是我干的辉巡。 我是一名探鬼主播登颓,決...
    沈念sama閱讀 40,362評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼红氯!你這毒婦竟也來了?” 一聲冷哼從身側響起咕痛,我...
    開封第一講書人閱讀 39,264評論 0 276
  • 序言:老撾萬榮一對情侶失蹤痢甘,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后茉贡,有當地人在樹林里發(fā)現了一具尸體塞栅,經...
    沈念sama閱讀 45,724評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年腔丧,在試婚紗的時候發(fā)現自己被綠了放椰。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片作烟。...
    茶點故事閱讀 40,040評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖砾医,靈堂內的尸體忽然破棺而出拿撩,到底是詐尸還是另有隱情,我是刑警寧澤如蚜,帶...
    沈念sama閱讀 35,742評論 5 346
  • 正文 年R本政府宣布压恒,位于F島的核電站,受9級特大地震影響错邦,放射性物質發(fā)生泄漏探赫。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,364評論 3 330
  • 文/蒙蒙 一撬呢、第九天 我趴在偏房一處隱蔽的房頂上張望伦吠。 院中可真熱鬧,春花似錦魂拦、人聲如沸毛仪。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽潭千。三九已至,卻和暖如春借尿,著一層夾襖步出監(jiān)牢的瞬間刨晴,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評論 1 270
  • 我被黑心中介騙來泰國打工路翻, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留狈癞,地道東北人。 一個月前我還...
    沈念sama閱讀 48,247評論 3 371
  • 正文 我出身青樓茂契,卻偏偏與公主長得像蝶桶,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子掉冶,可洞房花燭夜當晚...
    茶點故事閱讀 44,979評論 2 355

推薦閱讀更多精彩內容