vue2 ant design vue项目中上传和导入excel文件

vue2 ant design vue项目中上传和导入excel文件

下载excel文件

代码

// template中
<template>
	// 导出文件封装的按钮
	<x-down v-if="hasPerm('sysUser:export')" ref="batchExport" @batchExport="batchExport" />
</template>

// script中
<script>
import { sysUserExport } from '@/api/modular/system/userManage'

export default {
	methods: {
		batchExport() {
	      sysUserExport().then((res) => {
	        this.$refs.batchExport.downloadfile(res)
	      })
	    },
	}
}
</script>

@/api/modular/system/userManage.js文件

export function sysUserExport (parameter) {
  return axios({
    url: '/main/sysUser/export',
    method: 'get',
    params: parameter,
    responseType: 'blob'
  })
}

x-down组件

<template>
  <a-tooltip placement="top">
    <template slot="title">
      <span>导出所有数据</span>
    </template>
    <!-- 正常来说,这里加个插槽最好了,但是这个就是为导出准备的,一般这两个字不会变 -->
    <a-button type="dashed" @click="batchExport" :loading="batchExportLoading"><a-icon type="export"/>导出</a-button>
  </a-tooltip>
</template>

<script>
export default {
  name: 'XDown',
  data () {
    return {
      batchExportLoading: false
    }
  },
  methods: {
    /**
     * 本控件中点击按钮事件
     */
    batchExport () {
      this.batchExportLoading = true
      // 将其传达到上个界面
      this.$emit('batchExport', '')
    },

    /**
     * 通过返回的元素通过浏览器下载
     * 也就是接受使用这个组件的地方吧下载的内容传过来,
     * 但是这个组件本公子编写的时候只想的适用于导出excel来使用,下载文件呀图片方面的重新整个组件即可
     */
    downloadfile (res) {
      this.batchExportLoading = false
      var blob = new Blob([res.data], { type: 'application/octet-stream;charset=UTF-8' })
      var contentDisposition = res.headers['content-disposition']
      var patt = new RegExp('filename=([^;]+\\.[^\\.;]+);*')
      var result = patt.exec(contentDisposition)
      var filename = result[1]
      var downloadElement = document.createElement('a')
      var href = window.URL.createObjectURL(blob) // 创建下载的链接
      var reg = /^["](.*)["]$/g
      downloadElement.style.display = 'none'
      downloadElement.href = href
      downloadElement.download = decodeURI(filename.replace(reg, '$1')) // 下载后文件名
      document.body.appendChild(downloadElement)
      downloadElement.click() // 点击下载
      document.body.removeChild(downloadElement) // 下载完成移除元素
      window.URL.revokeObjectURL(href)
    }
  }
}
</script>

sysUserExport函数

import { axios } from '@/utils/request'
export function sysUserExport(parameter) {
	return axios({
		url: '根据情况写',
		methods: 'get',
		params: parameter,
		responseType: 'blob'
	}) 
}

导入excel文件

<template>
	<a-button v-if="hasPerm('sysUser:import')" icon="import" type="dashed" @click="importUserExcel">
      导入
    </a-button>
     <input
	     type="file"
	     id="uploadUserExcel"
	     ref="uploadUserExcel"
	     v-show="false"
	     accept=".xls, .xlsx, .excel"
	     @input="readUserExcel"
    />
</template>


<script>
export default {
	methods: {
		importUserExcel() {
	      let userInfo = JSON.parse(localStorage.getItem('userInfo'))
	      this.$refs.uploadUserExcel.click(); // 浏览文件
	      return
	    },
	    readUserExcel(event) {
	      if (event.target.files.length != 0) {
	        let fileList = event.target.files;
	        this.excelPersonName = fileList[0].name;
	        this.excelUserFile = this.$refs.uploadUserExcel.files[0];
	        this.gmDrUserSure();
	      }
	    },
	    //发送请求
	    gmDrUserSure() {
	      // this.uploadDialog = false;
	      let formData = new FormData();
	      formData.append("file", this.excelUserFile);
	      if (this.excelName === "") {
	        this.$message({
	          type: "warning",
	          message: "请选择excel文件!",
	          duration: 2 * 1000,
	        });
	        return;
	      }
	      //上传excel
	
	      exportUser(formData).then(res => {
	        if(res.code == 200) {
	          this.$message.success('导入成功!')
	          this.$refs.table.refresh()
	        } else {
	          this.$message.warning('导入失败:', res.messgae)
	        }
	      })
	    },
	}
}
</script>
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值