Export2Excel.js
可控性比较好,可以按自己的需求修改源码。
使用三个插件,分别是:file-saver、xlsx、script-loader。
安装:
npm install -S file-saver xlsxnpm install -D script-loader
- file-saver :保存文件用
- xlsx:xlsx核心
- script-loader:上面的库不支持 import 引入,需要 script-loader 来挂载到全局环境下
下载 Blob.js 和 Export2Excel.js ,在 src 下放入这两个 JS 文件并引用。我这里放在了 src/plugins/vendor 目录下。
Export2Excel.js 主要提供了 export_table_to_excel 和 export_json_to_excel 方法给我们导出成 excel 。我用的是传递 json 格式的方法,比较好控制数据。
另外,因为不支持导出多个 sheets ,所以我修改了一下 export_json_to_excel 方法:
export function export_json_to_excel(th, jsonData, defaultTitle, sheets) {/* original data */var wb = new Workbook();var ws_name = "";var ws;for (let i = 0; i < sheets.length; i++) {var data = jsonData[i];data.unshift(th[i]);ws_name = sheets[i];ws = sheet_from_array_of_arrays(data);/* add worksheet to workbook */wb.SheetNames.push(ws_name);wb.Sheets[ws_name] = ws;}var wbout = XLSX.write(wb, {bookType: 'xlsx', bookSST: false, type: 'binary'});var title = defaultTitle || '列表'saveAs(new Blob([s2ab(wbout)], {type: "application/octet-stream"}), title + ".xlsx")}
使用:
import { export_json_to_excel } from "@/plugins/vendor/Export2Excel";const tableData = [{type: "小枪",child: [{"chinese" : 97, "math": "99" , "english": 95}, {"chinese" : 97, "math": "99" , "english": 95}];},{type: "小刀",child: [{"chinese" : 97, "math": "99" , "english": 95}, {"chinese" : 97, "math": "99" , "english": 95}];}]exportExcel() {const header = [];const sheets = [];const data = [];tableData.forEach(item => {sheets.push(item.type);const th = ["语文","数学","英语"];header.push(th);const list = item.child;const filterVal = ["chinese", "math", "english"];const d = this.formatJson(filterVal, list);data.push(d);});export_json_to_excel(header, data, "分析结果", sheets);},formatJson(filterVal, jsonData) {return jsonData.map(v => filterVal.map(j => v[j]));}
vue-json-excel
使用简单,但是功能比较单一。看了一下源码似乎也不能导出多个 sheet 。
使用:
npm install vue-json-excel
示例代码:
<template><div><el-row><el-col :span="6"></el-col><el-col :span="12"><h1>{{ msg }}</h1><download-excel v-if="json_data.length >= 0"class="el-button":data="json_data":fields="json_fields"worksheet = "My Worksheet"name ="用户信息列表">导出Excel</download-excel></el-col><el-col :span="6"></el-col></el-row><el-table:data="json_data"borderstyle="width: 100%"><el-table-columnprop="type"label="序号"align="center"width="180"></el-table-column><el-table-columnprop="userName"label="姓名"align="center"width="180"></el-table-column><el-table-columnprop="age"align="center"label="年龄"></el-table-column><el-table-columnprop="phone"align="center"label="手机号"></el-table-column><el-table-columnprop="createTime"align="center"label="注册时间"></el-table-column></el-table></div></template><script>import JsonExcel from 'vue-json-excel'export default {name: 'HelloWorld',components:{'downloadExcel': JsonExcel},data () {return {msg: '使用vue-json-excel插件导出Excel',json_fields: { //导出Excel表格的表头设置'序号': 'type','姓名': 'userName','年龄': 'age','手机号': 'phone','注册时间': 'createTime',},json_data:[{"userName":"张三","age":18,"phone":15612345612,"createTime":"2019-10-22"},{"userName":"李四","age":17,"phone":15612345613,"createTime":"2019-10-23"},{"userName":"王五","age":19,"phone":15612345615,"createTime":"2019-10-25"},{"userName":"赵六","age":18,"phone":15612345618,"createTime":"2019-10-15"},]}},created() {this.initList();},methods: {initList(){for(let i in this.json_data){this.json_data[i].type=parseInt(i)+1}},}}</script><!-- Add "scoped" attribute to limit CSS to this component only --><style scoped>.el-button{background-color: lightskyblue;}</style>
打开 excel 的时候会有提示,体验没那么好。
js-xlsx
一款只需要纯 js 即可读取和导出 excel 的工具库,支持 xls 、 xlsx 、ods 等十几种格式。这个暂时还没有尝试过。
参考链接:
