vue项目结合element-ui表格添加表格导出功能

vue - vue-json-excel

使用方法

  1. npm 安装依赖
npm install vue-json-excel
  1. 项目主文件入口main.js全局引入
//excel
import JsonExcel from 'vue-json-excel'
Vue.component('downloadExcel', JsonExcel)
  1. 直接在项目中使用
NameTypeDescription
dataArrayData to be exported
fieldsObjectfields inside the Json Object that you want to export. If no given, all the properties in the Json are exported
export-fields (exportFields)Objectthis prop is used to fix the problem with other components that use the variable fields, like vee-validate. exportFields works exactly like fields
typestringmime type [xls, csv], default: xls
namestringfilename to export, deault: data.xls
titlestring/ArrayTitle(s) for the data, could be a string or an array of strings (multiple titles)
footerstring/ArrayFooter(s) for the data, could be a string or an array of strings (multiple footers)
default-value (defaultValue)stringUse as fallback when the row has no field values, default: ''
worksheetstringName of the worksheet tab. default: 'Sheet1'
fetchFunctionCallback to fetch data before download, if it's set it runs immediately after mouse pressed and before download process. IMPORTANT: only works if no data prop is defined (文件下载前通过接口获取数据,不需要data属性)
before-generateFunctionCallback to call a method right before the generate / fetch data, eg:show loading progress(文件下载前的操作)
before-finishFunctionCallback to call a method right before the download box pops out, eg:hide loading progress(文件下载结束操作)

//有数据,无需点击时获取 必需 data ,fields 属性
 <download-excel :data="json_data" :fields="json_fields" name="filename.xls" >
     <el-button type="warning">导出excel</el-button>
 </download-excel>
 
<script>
export default {
     data () {
     return{
        //excel表格的标题头
        json_fields: {
            'Complete name': 'name',  //excel标题:数据中的key
            'City': 'city',
            '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>

//无数据,点击时获取 需要fetch ,fields属性 (有fetch时不需要data属性)

 <download-excel :fetch= "fetchData" :fields="json_fields" :before-generate = "startDownload" :before-finish = "finishDownload"  name="filename.xls"  >
     <el-button type="warning">导出excel</el-button>
 </download-excel>
 
<script>
export default {
     data () {
     return{
        json_fields: {
            'Complete name': 'name',
            'City': 'city',
            'Telephone': 'phone.mobile',
            'Telephone 2' : {
                field: 'phone.landline',
                callback: (value) => {
                    return `Landline Phone - ${value}`;
                }
            },
        },
    }
 },
 methods:{
    async fetchData(){
     const { list } = await this.$store.dispatch("get/infolist", {id : 1});
        return list;
    },
    startDownload(){
        alert('show loading');
    },
    finishDownload(){
        alert('hide loading');
    }
  }
 
}
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值