前端導出excel(一)


1、安裝依賴:

  npm install --save xlsx file-saver

2、在組件中引入:

  import FileSaver from 'file-saver'
  import XLSX from 'xlsx'

3、添加導出按鈕:

 

 4、導出函數:

    outTab() {
      let fix = document.querySelector('.el-table__fixed')
      let wb
      if (fix) {
        wb = XLSX.utils.table_to_book(document.querySelector('#out-table').removeChild(fix))
        document.querySelector('#out-table').appendChild(fix)
      } else {
        wb = XLSX.utils.table_to_book(document.querySelector('#out-table'))
      }
      let wbout = XLSX.write(wb, {
        bookType: 'xlsx',
        bookSST: true,
        type: 'array'
      })
      try {
        FileSaver.saveAs(
          new Blob([wbout], {
            type: 'application/octet-stream'
          }),
          'file.xlsx'
        )
      } catch (e) {
        if (typeof console !== 'undefined') console.log(e, wbout)
      }
      return wbout
    },

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM