vue导出Excel格式 vue-json-excel file-saver xlsx

最近公司在写一个项目,需要把vue里面json数据导出成Excel 格式 ,在网上查了资料两种有实现方法

个人推荐用第二种方法,虽然第一种比较方便,但是第二种可控性好

第一种 :vue-json-excel

 安装依赖包   npm install vue-json-excel

在main.js种引入:

import JsonExcel from 'vue-json-excel'
Vue.component('downloadExcel', JsonExcel)

在这里插入图片描述
在项目中

<template>
  <div id="app">
    <download-excel class="export-excel-wrapper" :data="json_data" :fields="json_fields" name="点击下载">
    </download-excel>
  </div>
</template>
<script>
  export default {
    data() {
      return {
        json_fields: {
          "Complete name": "name", //常规字段
          Telephone: "phone.mobile", //支持嵌套属性
          "Telephone 2": {
            field: "phone.landline",
            //自定义回调函数
            callback: value => {
              return `Landline Phone - ${value}`;
            }
          }
        },
        json_data: [{
            name: "Tony Peña",
            city: "New York",
            country: "United States",
            birthdate: "1978-03-15",
            phone: {
              mobile: "1-541-754-3010",
              landline: "(541) 754-3010"
            }
          },
          {
            name: "Thessaloniki",
            city: "Athens",
            country: "Greece",
            birthdate: "1987-11-23",
            phone: {
              mobile: "+1 855 275 5071",
              landline: "(2741) 2621-244"
            }
          }
        ],
        json_meta: [
          [{
            " key ": " charset ",
            " value ": " utf- 8 "
          }]
        ]
      };
    }
  }
</script>

在这里插入图片描述

第二种实现方法:

第一步:安装依赖包:

npm install -S file-saver xlsx
npm install -D script-loader

yarn 安装包

yarn add file-saver
yarn add xlsx
yarn add script-loader --dev

#第二部导入Blob.js 和Export2Excel.js两个文件,在src目录下创建excel文件,里面放入Blob.js 和Export2Excel.js两个文件,
Blob.js 和Export2Excel.js两个文件地址: 文件提取码:kk8a
Blob.js 和Export2Excel.js下载地址

在main.js 引入:

import Blob from './excel/Blob'
import Export2Excel from './excel/Export2Excel.js'

在项目中:

<template>
  <div class="hello">
           <button class="tap"  @click="export2Excel">导出表格</button>
  </div>
</template>

<script>
export default {
 data () {
    return {
      tableData: [
        {'index':'999',"nickName": "的旧时光", "name": "98491231841251"},
        {'index':'1',"nickName": "高贵", "name": "张"},
        {'index':'2',"nickName": "海aaa灰尘", "name": "小兰"}
      ]
    }
  },
  methods: {
    export2Excel() {
      require.ensure([], () => {
        const { export_json_to_excel } = require('../excel/Export2Excel');
        const tHeader = ['序号', '昵称', '姓名'];  // 设置Excel的表格第一行的标题
        const filterVal = ['index', 'nickName', 'name'];  // index、nickName、name是tableData里对象的属性
        const list = this.tableData;  //把data里的tableData存到list
        const data = this.formatJson(filterVal, list);
        export_json_to_excel(tHeader, data, '导出文件名');  //导出Excel 文件名
      })
    },

    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    }
  }
}
```![在这里插入图片描述](https://img-blog.csdnimg.cn/20190520194933110.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0Nzk1ODEw,size_16,color_FFFFFF,t_70)

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值