分享一個json轉化成excel的插件

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

閒來無事,胡亂倒騰,菜雞一枚,大佬嘴下留情...git

插件安裝

yarn add custom-json2excel
or
npm install custom-json2excel
複製代碼

使用方式

一、直接轉化 json:github

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()
複製代碼

二、自定義頭部無需過濾字段時的使用方式:npm

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()
複製代碼

三、須要過濾字段時的使用方式: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 keyMap = {
  name: '姓名',
  age: '年齡',
  sex: '性別',
  companyName: '公司名稱',
  companyAddress: '公司地址'
}
const filters = ['sex']
const json2excel = new Json2excel({ data, keyMap, filters })
json2excel.generate()
複製代碼

四、須要表格標題時的使用方式:數組

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

Prop Type Defaults Required Description
data Array [] 轉化成表格初始 json 數據
filters Array [] × 須要過濾的字段數組
footer Array [] × 表格最後一列名稱,參數同 title
keyMap Object {} × keyMap 映射表,用於自定義表格頭部名稱
name String excel × excel 表格名稱
title Array [] × 表格標題名稱 {name:String,colspan:Number} name:名稱, colspan:列數
type String xls × 生成的表格類型,可選值(xls、csv)
相關文章
相關標籤/搜索