首页 > 其他分享 >vue通过Export2Excel.js进行导入excel,获取数据

vue通过Export2Excel.js进行导入excel,获取数据

时间:2023-04-14 15:44:06浏览次数:35  
标签:vue const name excel 获取数据 fileTemp file message type

<!--    封装的模板下载和导入按钮和功能组件-->

<template> <span style="margin-left: 10px"> <el-button size="mini" class="el-icon-download" @click="downFiles"> 下载模板</el-button> <el-upload action="" style="width: 115px;display: inline-block;margin-left: 10px" :http-request="uploadSectionFile" :on-success="handleAvatarSuccess" :before-upload="beforeUpload" :show-file-list="false" :accept="accept" > <el-button class="el-icon-upload" size="mini">批量导入</el-button> </el-upload> </span> </template> <script> import XLSX from 'xlsx'

  import Blob from './Excel/Blob'
  import Export2Excel from './Excel/Export2Excel.js'

export default {
  name: 'importTemplate',
  data() {
    return {
      accept: '.xlsx',
      fileTemp: {}, // 导入的文件流
      tableData: []
    }
  },
  methods: {
    downFiles() {
      // <a href="./static/培训实施参加人员统计模板.xlsx"><i className="el-icon-download"/>下载模板</a>
      window.open('/static/培训实施参加人员统计模板.xlsx')
    },
    uploadSectionFile(uploader) {
      this.fileTemp = uploader.file
      if (this.fileTemp) {
        if ((this.fileTemp.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') ||
          (this.fileTemp.type === 'application/vnd.ms-excel')) {
       this.importfEx(this.fileTemp) } else { this.$message({ type: 'warning', message: '附件格式错误,请删除后重新上传!' }) } } else { this.$message({ type: 'warning', message: '请上传附件!' }) } }, importfEx(event) { // FileReader 对象允许Web应用程序异步读取存储在用户计算机上的文件(或原始数据缓冲区)的内容 const fileReader = new FileReader() const file = event // 回调函数 fileReader.onload = ev => { try { const data = ev.target.result const workbook = XLSX.read(data, { type: 'binary' }) // excel读取出的数据 const excelData = XLSX.utils.sheet_to_json(workbook.Sheets[workbook.SheetNames[0]]) // 将上面数据转换成 table需要的数据---将获取到的数据$emit抛出数据 this.$emit('changeImportData', excelData) } catch (e) { this.$message({ type: 'error', message: '文件类型不正确!' }) return false } } // 读取文件 成功后执行上面的回调函数 fileReader.readAsBinaryString(file) }, handleAvatarSuccess(res, file) { console.log(res) // this.vm.edit.data.contractProjects.push({}) }, beforeUpload(file) { if (this.accept !== '') { const index = file.name.indexOf('.') const type = file.name.substring(index + 1, file.name.length) if (this.accept.indexOf(type) === -1) { this.$message.error(`文件 ${file.name}格式不正确,请上传指定格式的文件 ${this.accept}`) return false } } } } } </script> <style scoped> </style>

<!--   2. 父组件调用封装的组件-->
<template>
<!--              //导入excel  下载模板组件-->
<import-template @changeImportData="changeImportDataFun" />
</template>
methods: {
changeImportDataFun(dataArr) {
// 返回的数组数据
},
}

 

标签:vue,const,name,excel,获取数据,fileTemp,file,message,type
From: https://www.cnblogs.com/Z-HarOld/p/17318507.html

相关文章

  • Vue2总结
    笔记脚手架文件结构├──node_modules├──public│├──favicon.ico:页签图标│└──index.html:主页面├──src│├──assets:存放静态资源││└──logo.png││──component:存放组件││└──HelloWorld.vue│......
  • 2023-04-14 vue之组件全局注册
    新建一个vue文件,随便写点什么,然后在main.js中引入,如下:xxx.vue:<template><viewclass="container"><viewclass="content">登录窗口</view></view></template><script>exportde......
  • Vue3 Vite H5 手写一个横向展开的多级树列表
    最近写h5要做那种稍微复杂一点的树,没找到现成的UI组件库可用,vant的树只有两级不满足,只能自己写ps.选框的选择/反选/半选对父子选项的影响还有bug,留到脑子好的时候再优化效果代码框架是Vue3+Vite+Vant4。复选框用的vant的checkbox,应该也可以换别的或者原生。模板<templ......
  • vue 使用 vue-seamless-scroll 实现LED屏滚动效果
    yarnaddvue-seamless-scroll<divclass="index-roll"><vue-seamless-scroll:data="partnerList":class-option="optionLeft"><ul><liv-for=&quo......
  • vue3微信公众号商城项目实战系列(3)项目初始文件及文件夹简介
    首先我们来看下项目的文件结构图,如下: 各个文件及文件夹作用如下:文件或文件夹名称作用.vscodeVisualStudioCode开发工具的配置信息存放目录,从这个目录可以看出vue3确实是推荐使用vscode作为开发工具的。node_modules项目中用到的包存放目录,当我们用"npminstall......
  • vue3 + ts + electron项目搭建过程
    1、输入指令npmcreateelectron-vite2、工程创建好后进入工程目录 执行npmi加载依赖项,加载过程中可能会出现加载失败的问题,是因为github资源的问题,非项目本身问题,多执行几次即可3、打包输入npmrunbuild,打包过程也会出现打包失败的问题,原因和上面一样,也是github资......
  • Vue
    相关资料地址官网: https://cn.vuejs.org/官网教程: https://cn.vuejs.org/v2/guide/官网API: https://cn.vuejs.org/v2/api/ 王树东FE10资料:https://github.com/itguide/fe10    (对应视频资源:链接:https://pan.baidu.com/s/1nP-eD4XEH2nG1uABhsPvuw密码:w0ve)......
  • Vue2入门之超详细教程六-数据代理
    1、简介这章有点长,希望大家可以耐心看完。在了解数据代理之前需要先了解Object.defineProperty,Object.defineProperty就是给一个对象添加属性用的。数据代理,通过一个对象代理对另一个对象中数据的操作(读/写)。Vue中的数据代理:通过vm对象来代理data对象中的数下......
  • Vue.js element-ui
    基本使用视频按需引入视频7.1移动端常用UI组件库Vanthttps://youzan.github.io/vantCubeUIhttps://didi.github.io/cube-uiMintUIhttp://mint-ui.github.io7.2PC端常用UI组件库.ElementUIhttps://element.eleme.cnIViewUIhttps://www.iviewui.comsrcApp.vue......
  • vuex存储和本地存储(localstorage、sessionstorage)的区别
    1.最重要的区别:vuex存储在内存,localstorage则以文件的方式存储在本地2.应用场景:vuex用于组件之间的传值,localstorage则主要用于不同页面之间的传值。3.永久性:当刷新页面时vuex存储的值会丢失,localstorage不会。注:很多朋友觉得用localstorage可以代替vuex,对于不变的数据确实可......