Skip to content

About

Generate excel file from json data

Topics

Resources

Stars

65 stars

Watchers

11 watching

Forks

Latest commit

 

History

60 Commits

Folders and files

Repository files navigation

JSON2Excel

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

npm version

How to build


cargo install wasm-pack
wasm-pack build

How to use via npm

  • install the module
yarn add json2excel-wasm
  • import and use the module
// worker.js
import { convert } "json2excel-wasm";
const blob = await convert(json_data_to_export);

you can use code like next to force download of the result blob

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:

You can import and use lib dynamically like

const convert = import("https://cdn.dhtmlx.com/libs/json2excel/1.5/module.js");
const blob = await convert(json_data_to_export);

or use it as web worker

// you need to server worker from the same domain as the main script
var worker = new Worker("./worker.js");
worker.addEventListener("message", ev => {
    if (ev.data.type === "ready"){
        const blob = ev.data.blob;
        // do something with result
    }
});
worker.postMessage({
    type:"convert",
    data: raw_json_data
});

if you want to load worker script from CDN and not from your domain it requires a more complicated approach, as you need to catch the moment when service inside of the worker will be fully initialized

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

var worker = new Promise((res) => {
    const x = Worker(url);
    worker.addEventListener("message", ev => {
        if (ev.data.type === "ready"){
            const json = ev.data.data;
            // do something with result
        } else if (ev.data.type === "init"){
            // service is ready
            res(x);
        }
    });
});

worker.then(x => x.postMessage({
    type:"convert",
    data: raw_json_data
}));

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;         // left | center | right
    verticalAlign?: string; // top | center | bottom

    background?: string;
    color?: string;

    fontWeight?: string;     // bold
    fontStyle?: string;      // italic
    textDecoration?: string; // underline

    format?: string;

    // border valie format: {size} {style} {color}
    // size: 0.5px | 1px | 2px (works only with 'solid' style)
    // style: dashed | dotted | double | thin | solid
    // color: #000 | #000000
    borderTop?: string;
    borderRight?: string;
    borderBottom?: string;
    borderLeft?: string;
}

Versions

  • 1.6.0 — Cell locking (locked style). Preserve text-formatted numeric cells as strings.
  • 1.5.2 — Data validation lists: list sources and lists of values.
  • 1.5.1 — Renamed textWrap to wrapText.
  • 1.5.0 — Text wrapping.
  • 1.4.0 — Hyperlinks, frozen and hidden rows/columns, data validation.
  • 1.3.1 — Export numbers that start with zero as strings.
  • 1.3.0 — convert() is async. Extra styles: borders (style/size), vertical align, fontFamily. Renamed textAlign to align.
  • 1.2.1 — Smaller WASM bundle.
  • 1.2.0 — Formula export, sheet names with spaces. Fixes for shared strings, column widths, and sheet format.
  • 1.0.2 — UTF-8 encoding.
  • 1.0.1 — CDN link fixes.
  • 1.0.0 — Initial release.

License

MIT

About

Generate excel file from json data

Topics

Resources

Stars

65 stars

Watchers

11 watching

Forks

Releases

Packages

Used by

Contributors

Languages