Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.1k views
in Technique[技术] by (71.8m points)

vue中使用xlsx后不能返回数据

// vue

 <input
  type="file"
  @change="importExcel($event)"
  ref="importExcel"
  id="importExcel"
  style="display:none;"
  accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel" >
<a id="exportExcel" ref="exportExcel"></a>
<a-button type="primary" class="button" @click="btnImportClick">导入Excel</a-button>
<a-button type="primary" class="button" @click="btnExportClick(excelData)" style="margin-left:20px;">导出Excel</a-button>`
              
 export const importExcelMethods = function(event) {
    if (!event.target.files) {
        return
      }
      var suffix = event.target.files[0].name.split('.')[1]
      if (suffix != 'xls' && suffix != 'xlsx') {
        alert('导入的文件格式不正确!')
        return
      }
      var f = event.target.files[0]
      var reader = new FileReader()
      reader.onload = function (e) {
        var data = e.target.result
        if (rABS) {
          wb = XLSX.read(btoa(fixdata(data)), {
            //手动转化
            type: 'base64',
          })
        } else {
          wb = XLSX.read(data, {
            type: 'binary',
          })
        }
        const wsname = wb.SheetNames[0]
        const ws = wb.Sheets[wsname]
        const zdata = utils.sheet_to_json(ws, { header: 1 })
        const newData = JSON.stringify(
          XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]])
        )
        console.log('===',newData)
      }
      if (rABS) {
        reader.readAsArrayBuffer(f)
      } else {
        reader.readAsBinaryString(f)
      }
    }
    //调用
  
  importExcel(event){
  const myevent = event
  importExcelMethods(myevent)
},

现在的问题是:我封装渲染后想返回zdata,但是就是显示undefined

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

我看你 importExcelMethods这个方法也没返回什么东西额 没明白你说的显示undefined的是指哪里


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...