vue导出excel

1. 全部导出

    <div class="toexcel">
      <el-button
        @click="exportExcel"
        type="primary"
        class="button"
        style="width:70px;position:absolute;top:0;right:30px"
        >导出</el-button
      >
    </div>
      <el-table
        :data="tableData"
        :span-method="objectSpanMethod"
        :header-cell-style="{ background: '#cef4f1', color: '#606266' }"
        border
        :cell-style="tableCellStyle"
        style=" margin-top: 20px"
        width="100%"
        class="table"
      >

 

    function exportExcel () {
      // 设置当前日期
      // const time = new Date()
      // const year = time.getFullYear()
      // const month = time.getMonth() + 1
      // const day = time.getDate()
      // const name = year + '' + month + '' + day
      const name = '表格测试'
      // console.log(name)
      /* generate workbook object from table */
      //  .table要导出的是哪一个表格
      var wb = XLSX.utils.table_to_book(document.querySelector('.table'))
      /* get binary string as output */
      var wbout = XLSX.write(wb, {
        bookType: 'xlsx',
        bookSST: true,
        type: 'array'
      })
      try {
        //  name+'.xlsx'表示导出的excel表格名字
        FileSaver.saveAs(
          new Blob([wbout], { type: 'application/octet-stream' }),
          name + '.xlsx'
        )
      } catch (e) {
        if (typeof console !== 'undefined') console.log(e, wbout)
      }
      return wbout
    }

 

posted on 2021-05-08 15:57  稳住别慌  阅读(1403)  评论(0编辑  收藏  举报