🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Sign inDemoInstall
Socket

js2excel

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

js2excel

A simple module for excel and json converts each other

0.7.8
Source
npm
Version published
Weekly downloads
823
1.6%
Maintainers
1
Weekly downloads
 
Created
Source

js2excel

A simple module for excel and json converts each other.

Installation

It is recommended to run webpack on node 6.x or higher.

Install the pkg with npm:

npm install js2excel --save

or yarn

yarn add js2excel

or bower

bower install js2excel

Usage

Convert json to excel

// es6
import {json2excel, excel2json} from 'js2excel';

//CommonJS
let { json2excel, excel2json } = require('js2excel');

/**
 * excel's data
 **/

// excel's columns's header 
let headers = [
    {
        // the name will be as the excel column name
        name: 'User Id', 
        // the prop will be as the excel row data, which is the rows' item's property.
        prop: 'userId'     
    },
    {
        name: 'Phone Number',
        prop: 'userPhoneNumber'
    },
    {
        name: 'User Address',
        prop: 'userAddress'
    },
    {
        name: 'Date',
        prop: 'date'
    }
];

// excel rows' data
// rows' data will be exports, which you probably get it from server.
let rows = [
    {
        "userId": 1,
        "userPhoneNumber": 1888888888,
        "userAddress": 'xxxx',
        "date": '2013/09/10 09:10'  // string
    },
    {
        "userId": 2,
        "userPhoneNumber": 1888888888,
        "userAddress": 'xxxx',
        "date": new Date()
    },
    {
        "userId": 3,
        "userPhoneNumber": 1888888888,
        "userAddress": 'xxxx',
        "date": new Date()
    }
];

// this will be export a excel and the file's name is user-info-data.xlsx
// the default file's name is excel.xlsx
try {
    json2excel({
        headers, rows, 
        name: 'user-info-data',
        formateDate: 'yyyy/mm/dd'
    });
} catch (e) {
    console.error('export error');
}

// for webpack 3: dynamic import
import(/* webpackChunkName: "js2excel" */ 'js2excel').then(({json2excel}) => {
    json2excel({
        headers, rows, 
        name: 'user-info-data',
        formateDate: 'dd/mm/yyyy'
    });
}).catch((e) => {

});

Exports result as the image shows:

user-data

Convert excel to json

import { excel2json } from 'js2excel';

// html
<input type="file" multiple="false" id="sheets" accept="application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" @change="onchange" />

// methods
onchange(e){
    excel2json(e.target.files, (data) => {
        console.log('json', data)
    }, 'excel2json')
}

// for webpack 3: dynamic import
onchange(e) {
    import(/* webpackChunkName: "js2excel" */ 'js2excel').then(({excel2json}) => {
        excel2json(e.target.files, (data) => {
            console.log('json', data)
        }, 'excel2json')
    }).catch((e) => {

    });
}

Example, if you hava a excel as following:

excel

The data maybe as following:

data

API

json2excel(opts)

Convert json to excel(.xlsx).

opts Type: Object

opts.headers

Type: Array

Default: []

Excel's column's headers.

opts.rows

Type: Array,

Default: []

Excel's rows's data.

opts.name

Type: String,

Default: 'excel'

Excel's name, whose suffix is .xlsx.

opts.formateDate

Type: String,

Default: 'dd/mm/yyyy'

The date formate in rows' data. Examples:

'dd/mm/yyyy' => 08/07/2017
'd/m/yy' => 8/7/17
'd/m/yy hh:ss' => 8/7/17 18:29
'yyyy/mm/dd hh:ss' => 2017/07/17 18:29

excel2json(files, cb(data), [defval])

Convert excel(.xlsx/.xls) to json.

files

Type: Array

FileList from <input type='file' multiple="false" >.

cb(data)

Type: Function

Callback function called on finish. The data maybe as following:

{   
    `${sheetName}`: `[${excelRowsData}]`
    'sheet1': [/** excel rows' data **/],
    'sheet2': [/** excel rows' data **/],
    'sheet3': [/** excel rows' data **/]
    ...
}

defval

Type: String

The default value when the row data corresponding to the column is blank.

Supported browsers

License

MIT

Keywords

json

FAQs

Package last updated on 08 Jul 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts