Vue - 下载页面中Table表格至Excel

一 . 安装依赖

npm install xlsx file-saver --save

二. 引入

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

三. 下载

3.1 单表格下载

 <template>
 	<div>
 		<el-button type="primary" @click="downExcel">下载至Excel</el-button>
 		<el-table id="example" :data="listData">
	      <el-table-column label="姓名" prop="name"></el-table-column>
	      <el-table-column label="性别" prop="sex"></el-table-column>
	      <el-table-column label="年龄" prop="age"></el-table-column>
	    </el-table>
 	</div>
</template>
<script>
import FileSaver from 'file-saver'
import XLSX from 'xlsx'
export default {
  data() {
    return {
      listData: [{
        name:'aa',
        sex:'男',
        age:10
      },{
        name:'bb',
        sex:'女',
        age:12
      },{
        name:'cc',
        sex:'男',
        age:15
      }]
    };
  },

  methods: {
    //下载数据至Excel
    downExcel() {
      let et = XLSX.utils.table_to_book(document.getElementById("example")); //此处传入table的DOM节点
      let etout = XLSX.write(et, {
        bookType: "xlsx",
        bookSST: true,
        type: "array"
      });
      try {
          FileSaver.saveAs(
            new Blob([etout], {
              type: "application/octet-stream"
            }),
            `人员信息.xlsx`
          ); //导出的文件名
      } catch (e) {
        console.log(e, etout);
      }
      return etout;
    }
  }
};
</script>

<style scoped>

</style>

3.2 多表格下载

 <template>
 	<div>
 		<el-button type="primary" @click="downExcel">下载至Excel</el-button>
 		<el-table id="Table1" :data="listData">
	      <el-table-column label="姓名" prop="name"></el-table-column>
	      <el-table-column label="性别" prop="sex"></el-table-column>
	      <el-table-column label="年龄" prop="age"></el-table-column>
	    </el-table>
	    <el-table id="Table2" :data="listData1">
	      <el-table-column label="姓名" prop="name"></el-table-column>
	      <el-table-column label="科目" prop="subject"></el-table-column>
	      <el-table-column label="成绩" prop="achievement"></el-table-column>
	    </el-table>
 	</div>
</template>
<script>
import FileSaver from 'file-saver'
import XLSX from 'xlsx'
export default {
  data() {
    return {
      listData: [{
        name:'aa',
        sex:'男',
        age:10
      },{
        name:'bb',
        sex:'女',
        age:12
      },{
        name:'cc',
        sex:'男',
        age:15
      }],
      listData1: [{
        name:'aa',
        subject:'语文',
        achievement:98
      },{
        name:'aa',
        subject:'数学',
        achievement:95
      },{
        name:'aa',
        subject:'英语',
        achievement:96
      }],
    };
  },

  methods: {
    //下载数据至Excel
    downExcel() {
      	var workbook = XLSX.utils.book_new();

		/* convert table 'table1' to worksheet named "Sheet1" */
		var ws1 = XLSX.utils.table_to_sheet(document.getElementById('Table1'));
		XLSX.utils.book_append_sheet(workbook, ws1, "sheet1");
		
		/* convert table 'table2' to worksheet named "Sheet2" */
		var ws2 = XLSX.utils.table_to_sheet(document.getElementById('Table2'));
		XLSX.utils.book_append_sheet(workbook, ws2, "sheet2");
		
		/* get binary string as output */
		var wbOut = XLSX.write(workbook, {
		  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;
    }
  }
};
</script>

<style scoped>

</style>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值