vue导出表格

简介: vue导出表格

前端vue导出execl表格

Author:kak

  • 记录型文章

1、安装相关依赖

npm install --save xlsx file-saver

2、所需页面导入相关依赖

import FileSaver from 'file-saver'
import XLSX from 'xlsx'

3、添加导出方法

只需要更改对应文件名和即可

 exportExcel () {
   
   
         var wb = XLSX.utils.table_to_book(document.querySelector('#tableName'))
         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
     },

4、设置Button按钮触发方法exportExcel()即可

<div class="buttonStyle2" id="noprint">
    <el-button type="primary" @click="exportExcel()">导出</el-button>
</div>

5、完整代码

<template>
  <div>
    <div class="buttonStyle2" id="noprint">
      <el-button type="primary" @click="exportExcel()">导出</el-button>
    </div>
    <div>
      <el-table
        :data="students"
        :header-cell-style="{
          background: '#E7F4FF',
          color: '#000',
          'font-weight': '700',
        }"
        border
        stripestyle="width: 100%"
      >
        <el-table-column
          prop="ownCode"
          label="学生ID"
          align="center"
          width="200px"
        ></el-table-column>
        <el-table-column
          prop="phone"
          label="手机号"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="name"
          label="学生姓名"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="sex"
          label="学生性别"
          align="center"
          width="100px"
        ></el-table-column>
        <el-table-column
          prop="age"
          label="学生年龄"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="enabled"
          label="学生状态"
          align="center"
          width="100px"
        >
          <template slot-scope="scope">
            <el-tag size="small" v-if="scope.row.enabled == 'Y'" effect="plain"
              >生效</el-tag
            >
            <el-tag
              size="small"
              v-if="scope.row.enabled == 'N'"
              effect="plain"
              type="danger"
              >失效</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
              >修改</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import FileSaver from 'file-saver'
import XLSX from 'xlsx'
export default {
    
    
  data() {
    
    
    return {
    
    
      dialogFormVisible: false,
      dialogFormVisible1: false,
      formLabelWidth: "120px",
      student: {
    
    
        ownCode: "",
        name: "",
        sex: "",
        pwd: "",
        phone: "",
        age: "",
        enabled: "",
      },
      students: [],
      search: "",
    };
  },

  // 页面加载时触发
  created() {
    
    
    this.$axios
      .get("studentList", {
    
    })
      .then((response) => {
    
    
        console.log(response);
        if (response.data.statusCode == 200) {
    
    
          //接受数据
          this.students = response.data.data;
        }
      })
      .catch((error) => {
    
    
        alert(error);
      });
  },
    //导出
     exportExcel () {
    
    
     //document.querySelector('#id'/class) 填要选择的table id  或者对应的class
         var wb = XLSX.utils.table_to_book(document.querySelector('#printTest'))
         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
     },
  },
};
</script>

<style scoped>
.buttonStyle {
    
    
  float: left;
  margin-top: -12px;
}
.buttonStyle2 {
    
    
  float: right;
  margin-top: -12px;
}

</style>

图片.png

目录
相关文章
|
1天前
|
存储 JavaScript 前端开发
搞懂Vue一篇文章就够了
搞懂Vue一篇文章就够了
9 0
|
1天前
|
JavaScript 前端开发 UED
Vue 异步组件
Vue 异步组件
8 0
|
1天前
|
JavaScript
Vue自定义指令的三个方法
Vue自定义指令的三个方法
7 0
|
1天前
|
存储 资源调度 JavaScript
vue引入vuex
vue引入vuex
25 7
|
1天前
|
JavaScript 前端开发
vue怎么自定义底部导航
vue怎么自定义底部导航
20 10
|
1天前
|
JavaScript
vue知识点
vue知识点
8 3
|
2天前
|
JavaScript 索引
vue 在什么情况下在数据发生改变的时候不会触发视图更新
vue 在什么情况下在数据发生改变的时候不会触发视图更新
13 2
|
2天前
|
存储 缓存 JavaScript
vue中性能优化
vue中性能优化
12 1
|
2天前
|
JavaScript
vue常用指令
vue常用指令
11 1
|
2天前
|
JavaScript
vue自定义指令
vue自定义指令