Skip to content
Generate excel file from json data
Branch: master
Clone or download
Aleksei Kolosov
Latest commit 0883d33 Mar 22, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
example [fix] sheet format pr sequence Mar 22, 2019
js [dev] fix CDN links Feb 26, 2019
src [fix] sheet format pr sequence Mar 22, 2019
wasm [fix] sheet format pr sequence Mar 22, 2019
.gitignore init Feb 14, 2019
Cargo.lock
Cargo.toml [dev] update dependencies versions Mar 22, 2019
README.md Update README.md Mar 13, 2019
package.json
rollup.config.js init Feb 14, 2019
yarn.lock init Feb 14, 2019

README.md

JSON2Excel

JSON2Excel is a Rust and WebAssembly-based library that allows converting JSON files into Excel ones at ease.

npm version

How to build

yarn install
// build js code
yarn build
// rebuild wasm code
// rust toolchain is required
yarn build-wasm

How to use via npm

  • install the module
yarn add json2excel-wasm
  • import the module
// worker.js
import "json2excel-wasm";
  • use the module in the app
// app.js
const worker = new Worker("worker.js");

function doConvert(){
    worker.postMessage({ 
        type: "convert",
        data: json_data_to_export
    });
}

worker.addEventListener("message", e => {
    if (e.data.type === "ready"){
        const blob = e.data.blob;
        //excel file is ready to be downloaded!

        const a = document.createElement("a");
        a.href = URL.createObjectURL(blob);
        a.download = "data.xlsx";
        document.body.append(a);
        a.click();
        document.body.remove(a);
    }
});

How to use from CDN

CDN links are the following:

In case you use build system like webpack, it is advised to wrap the link to CDN source into a blob object to avoid possible breakdowns:

var url = window.URL.createObjectURL(new Blob([
    "importScripts('https://cdn.dhtmlx.com/libs/json2excel/1.0/worker.js');"
], { type: "text/javascript" }));

var worker = new Worker(url);

Input format

interface IConvertMessageData {
    uid?: string;
    data: ISheetData;
    styles?: IStyles[];
    wasmPath?: string; // use cdn by default
}

interface IReadyMessageData {
    uid: string; // same as incoming uid
    blob: Blob;
}

interface ISheetData {
    name?: string;
    cols?: IColumnData[];
    rows?: IRowData[];
    cells?: IDataCell[][]; // if cells mising, use plain
    plain?: string[][];

    merged?: IMergedCells;
}

interface IMergedCells {
    from: IDataPoint;
    to: IDataPoint;
}

interface IDataPoint {
    column: number; 
    row: number;
}

interface IColumnData {
    width: number;
}

interface IRowData {
    height: number;
}

interface IDataCell{
    v: string;
    s: number;
}

interface IStyle {
    fontSize?: string;
    align?: string;

    background?: string;
    color?: string;

    fontWeight?: string;
    fontStyle?: string;
    textDecoration?: string;

    format?: string;
}

License

MIT

You can’t perform that action at this time.