VUE 에서 엑셀 파일 을 업로드 하고 내용 을 분석 합 니 다.

1741 단어 jsvue

        
    

   import XLSX from 'xlsx'         //        XLSX 

  importExcel (content) {
    const file = content.file
    // let file = file.files[0] //      input         
    const types = file.name.split('.')[1]
    const fileType = ['xlsx', 'xlc', 'xlm', 'xls', 'xlt', 'xlw', 'csv'].some(item => item === types)
    if (!fileType) {
      this.$message('    !     ')
      return
    }
    this.file2Xce(file).then(tabJson => {
      if (tabJson && tabJson.length > 0) {
        this.xlsxJson = tabJson
        this.fileList = this.xlsxJson[0].sheet
        let n = '     '
        this.fileList.forEach((item, index, arr) => {
          if (item[n] === this.name) {
            this.dataForm.projectno = item['XXXX'] //        
          }
        })
      }
    })
  },
  file2Xce (file) {
    return new Promise(function (resolve, reject) {
      const reader = new FileReader()
      reader.onload = function (e) {
        const data = e.target.result
        this.wb = XLSX.read(data, {
          type: 'binary'
        })
        const result = []
        this.wb.SheetNames.forEach((sheetName) => {
          result.push({
            sheetName: sheetName,
            sheet: XLSX.utils.sheet_to_json(this.wb.Sheets[sheetName])
          })
        })
        resolve(result)
      }
      // reader.readAsBinaryString(file.raw)
      reader.readAsBinaryString(file) //   input  
    })
  },

남 의 것 을 거울 로 삼 았 다

좋은 웹페이지 즐겨찾기