vue 利用FileSaver和XLSX实现element table导出

190 阅读1分钟

1.安装

npm install -S file-saver
npm install -S xlsx

2. 重点!!! raw:true属性控制把内容当做字符串进行输出,处理身份证号输出含E或者特殊编号如00001

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