1. 程式人生 > >HTML的表格以Excel匯出

HTML的表格以Excel匯出

說明:在工作中遇到這個需求,正好在網上看到方法,自己記錄下來供以後參考。
原博點這裡

//引入兩個依賴
import XLSX from 'xlsx'
import fileSave from 'file-saver'

//掛載到vue原型上,引數id為表格的id,引數tablename為打印出來的表格名字

Vue.prototype.exportExcel = function (id, tablename) {
  let et = XLSX.utils.table_to_book(document.getElementById(id))
  let etout = XLSX.write(et, {
    bookType: 'xlsx',
    bookSST: true,
    type: 'array'
  })
  try {
    fileSave.saveAs(new Blob([etout], {
      type: 'application/octet-stream'
    }), tablename + '.xlsx')
  } catch (e) {
    console.log(e, etout)
  }
  return etout
}