vue中純前端實現(xiàn)導(dǎo)出簡單Excel表格的功能

前言
使用方法
npm install vue-json-excel
cnpm install vue-json-excel
import Vue from 'vue'import JsonExcel from 'vue-json-excel'Vue.component('downloadExcel', JsonExcel)
:data = "json_data":fields = "json_fields"name = "用戶統(tǒng)計列表">導(dǎo)出Excel
:fields = “json_fields” ----------------Excel中表頭的名稱
:data = “json_data” -------------------導(dǎo)出的數(shù)據(jù)
export default{data(){return{json_fields: { //導(dǎo)出Excel表格的表頭設(shè)置'序號': 'type','姓名': 'userName','年齡': 'age','手機號': 'phone','注冊時間': 'createTime',},}}}
export default{data(){return{json_data:[{"userName":"張三","age":18,"gender":"phone":15612345612,"createTime":"2019-10-22"},{"userName":"李四","age":17,"gender":"phone":15612345613,"createTime":"2019-10-23"},{"userName":"王五","age":19,"gender":"phone":15612345615,"createTime":"2019-10-25"},{"userName":"趙六","age":18,"gender":"phone":15612345618,"createTime":"2019-10-15"},]}}}

導(dǎo)出Excel 導(dǎo)出Excel
如果表格中有數(shù)據(jù)的時候,點擊導(dǎo)出功能

在給json_data數(shù)據(jù)賦值的時候,添加加一個type屬性,來顯示序號。
for(let i in this.json_data){this.json_data[i].type=parseInt(i)+1}

如果里面的的時間字段精確到秒的話,可能導(dǎo)出來的數(shù)據(jù)不會那么精確,這時你可以通過改插件的源碼來解決; 參考連接。

還有就是想要表格中數(shù)據(jù)居中顯示,也可以改源碼;

總結(jié)

評論
圖片
表情
