Element-ui組件庫Table表格導出Excel表格--存在重復數據問題-----建議前端不做導出邏輯,應該是后端做處理


借鑒:https://www.jianshu.com/p/1971fc5b97ca

https://blog.csdn.net/qq_40614207/article/details/94003793

貼出代碼

  // 定義導出Excel表格事件
      exportExcel() {
        // 解決生成重復數據-因為使用l fixed屬性
        var fix = document.querySelector('.el-table__fixed')
        var wb
        // 判斷要導出的節點中是否有fixed的表格,如果有,轉換excel時先將該dom移除,然后append回去
        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'))
        }
        /* 獲取二進制字符串作為輸出 */
        var wbout = XLSX.write(wb, {
          bookType: 'xlsx',
          bookSST: true,
          type: 'array'
        })
        try {
          FileSaver.saveAs(
            // Blob 對象表示一個不可變、原始數據的類文件對象。
            // Blob 表示的不一定是JavaScript原生格式的數據。
            // File 接口基於Blob,繼承了 blob 的功能並將其擴展使其支持用戶系統上的文件。
            // 返回一個新創建的 Blob 對象,其內容由參數中給定的數組串聯組成。
            new Blob([wbout], { type: 'application/octet-stream' }),
            // 設置導出文件名稱
            'sheetjs.xlsx'
          )
        } catch (e) {
          if (typeof console !== 'undefined') console.log(e, wbout)
        }
        return wbout
      },

  


免責聲明!

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



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