美文网首页Vue
vue 导入xlsx

vue 导入xlsx

作者: 安徒生1997 | 来源:发表于2021-09-23 13:24 被阅读0次

npm install xlsx --s

handlePreview(file) {

        // console.log(file);

        this.importfile(file.raw)

        this.fileList = []

      },

importfile (obj) {

        const _this = this

        const reader = new FileReader()

        reader.readAsArrayBuffer(obj)//需要传blob类型

        reader.onload = function () {

          const buffer = reader.result

          const bytes = new Uint8Array(buffer)

          const length = bytes.byteLength

          let binary = ''

          for (let i = 0; i < length; i++) {

            binary += String.fromCharCode(bytes[i])

          }

          const XLSX = require('xlsx')//引用

          const wb = XLSX.read(binary, {

            type: 'binary'

          })

          let tableList = ['业务类型','用户订单号','快递单号']

          for( let i = 0; i<tableList.length; i++ ) {

            if ( wb.Strings[i].t != tableList[i] ) {

              _this.$message.error('格式错误,请重新上传');

              return false

            }

          }

          const outdata = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]])

          // console.log(outdata) // 此为取到的excel中内容,然后就可以自己改数据,画页面啦~

          _this.allVisible = true

          let dataStr = []

          for ( let i = 0; i<outdata.length; i++ ) {

            dataStr.push({

              expressName: outdata[i].业务类型 == undefined ? '':outdata[i].业务类型,

              expressNumber: outdata[i].用户订单号 == undefined ? '':outdata[i].用户订单号,

              orderId: outdata[i].快递单号 == undefined ? '':outdata[i].快递单号.toString(),

            });

          }

          _this.allTableData = dataStr

        }

      },

相关文章

网友评论

    本文标题:vue 导入xlsx

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