分享一个json转化成excel的插件

custom-json2excel (github地址:github.com/xxj95719/cu…)

闲来无事,胡乱倒腾,菜鸡一枚,大佬嘴下留情...

插件安装

yarn add custom-json2excel
or
npm install custom-json2excel
复制代码

使用方式

1、直接转化 json:

import Json2excel from 'custom-json2excel'
const data = [
  {
    name: '哈哈',
    age: 1,
    sex: '男',
    companyName: '公司1',
    companyAddress: '公司地址1'
  },
  {
    name: '呵呵',
    age: 2,
    sex: '女',
    companyName: '公司2',
    companyAddress: '公司地址2'
  },
  {
    name: '嘻嘻',
    age: 3,
    sex: '男',
    companyName: '公司3',
    companyAddress: '公司地址3'
  },
  {
    name: '啦啦',
    age: 4,
    sex: '女',
    companyName: '公司4',
    companyAddress: '公司地址4'
  }
]
const json2excel = new Json2excel({ data })
json2excel.generate()
复制代码

2、自定义头部无需过滤字段时的使用方式:

import Json2excel from 'custom-json2excel'
const data = [
  {
    name: '哈哈',
    age: 1,
    sex: '男',
    companyName: '公司1',
    companyAddress: '公司地址1'
  },
  {
    name: '呵呵',
    age: 2,
    sex: '女',
    companyName: '公司2',
    companyAddress: '公司地址2'
  },
  {
    name: '嘻嘻',
    age: 3,
    sex: '男',
    companyName: '公司3',
    companyAddress: '公司地址3'
  },
  {
    name: '啦啦',
    age: 4,
    sex: '女',
    companyName: '公司4',
    companyAddress: '公司地址4'
  }
]
const keyMap = {
  name: '姓名',
  age: '年龄',
  sex: '性别',
  companyName: '公司名称',
  companyAddress: '公司地址'
}
const json2excel = new Json2excel({ data, keyMap })
json2excel.generate()
复制代码

3、需要过滤字段时的使用方式:

import Json2excel from 'custom-json2excel'
const data = [
  {
    name: '哈哈',
    age: 1,
    sex: '男',
    companyName: '公司1',
    companyAddress: '公司地址1'
  },
  {
    name: '呵呵',
    age: 2,
    sex: '女',
    companyName: '公司2',
    companyAddress: '公司地址2'
  },
  {
    name: '嘻嘻',
    age: 3,
    sex: '男',
    companyName: '公司3',
    companyAddress: '公司地址3'
  },
  {
    name: '啦啦',
    age: 4,
    sex: '女',
    companyName: '公司4',
    companyAddress: '公司地址4'
  }
]
const keyMap = {
  name: '姓名',
  age: '年龄',
  sex: '性别',
  companyName: '公司名称',
  companyAddress: '公司地址'
}
const filters = ['sex']
const json2excel = new Json2excel({ data, keyMap, filters })
json2excel.generate()
复制代码

4、需要表格标题时的使用方式:

import Json2excel from 'custom-json2excel'
const data = [
  {
    name: '哈哈',
    age: 1,
    sex: '男',
    companyName: '公司1',
    companyAddress: '公司地址1'
  },
  {
    name: '呵呵',
    age: 2,
    sex: '女',
    companyName: '公司2',
    companyAddress: '公司地址2'
  },
  {
    name: '嘻嘻',
    age: 3,
    sex: '男',
    companyName: '公司3',
    companyAddress: '公司地址3'
  },
  {
    name: '啦啦',
    age: 4,
    sex: '女',
    companyName: '公司4',
    companyAddress: '公司地址4'
  }
]
const keyMap = {
  name: '姓名',
  age: '年龄',
  sex: '性别',
  companyName: '公司名称',
  companyAddress: '公司地址'
}
const filters = ['sex']
const title = [
  { name: '个人信息', colspan: 3 },
  { name: '公司信息', colspan: 2 }
]
const json2excel = new Json2excel({ data, title })
json2excel.generate()
复制代码

Props type

PropTypeDefaultsRequiredDescription
dataArray[]转化成表格初始 json 数据
filtersArray[]×需要过滤的字段数组
footerArray[]×表格最后一列名称,参数同 title
keyMapObject{}×keyMap 映射表,用于自定义表格头部名称
nameStringexcel×excel 表格名称
titleArray[]×表格标题名称 {name:String,colspan:Number} name:名称, colspan:列数
typeStringxls×生成的表格类型,可选值(xls、csv)

转载于:https://juejin.im/post/5b7e2960f265da432a6addd1

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值