VUE中使用vue-json-excel超级方便导出excel表格数据

在项目开发时免不了有时会用到表格数据导出excel的功能,之前会用file-saver xlsx script-loader来导出,并且配置很是麻烦,如今用vue-json-excel配置及使用都很是简单vue

1、安装vue-json-excel

npm install vue-json-excel -S
  • 1

2、main.js里面引入并注册使用

import JsonExcel from 'vue-json-excel'

Vue.component('downloadExcel', JsonExcel)

 

3、页面中使用

<download-excel
    class = "export-excel-wrapper"
    :data = "json_data"
    :fields = "json_fields"
    name = "filename.xls">
    <!-- 上面能够自定义本身的样式,还能够引用其余组件button -->
    <!-- <el-button type="primary" size="small">导出EXCEL</el-button> -->
</download-excel>

 

在这里说明一下组件的各个属性npm

  • json_data:须要导出的数据
  • json_fields:自主选择要导出的字段,若不指定,默认导出所有数据中心所有字段
属性名 类型 描述
data Array 须要导出的数据,支持中文
fields Object 定义须要导出数据的字段
name String 导出excel的文件名
type String 导出excel的文件类型(xls,csv),默认是xls

下面给个实例json

注意如下几点markdown

  • json_fields里面的属性是excel表每一列的title,注意多个词组组成的属性名要加双引号
  • 若是须要自定义导出的数据,能够定义回调函数。
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 "
          }
        ]
      ]
    };
  }
相关文章
相关标签/搜索