js 通过FileSaver、XLSX将table表格转换为excel文件进行下载

581 阅读1分钟
import FileSaver from 'file-saver'
import XLSX from 'xlsx'
// id参数为table表格 ID
exportExcel (id) {
      /* generate workbook object from table */
      var wb = XLSX.utils.table_to_book(document.querySelector('#' + id), {raw: true})
      /* get binary string as output */
      var wbout = XLSX.write(wb, {bookType: 'xlsx', bookSST: true, type: 'array'})
      try {
        FileSaver.saveAs(new Blob([wbout], {type: 'application/octet-stream'}), '文件名称.xlsx')
      } catch (e) {
        if (typeof console !== 'undefined') console.log(e, wbout)
      }
      return wbout
    }