JSON2Excel is a Rust and WebAssembly-based library that allows converting JSON files into Excel ones at ease.
cargo install wasm-pack
wasm-pack build
- install the module
yarnaddjson2excel-wasm- import and use the module
// worker.jsimport{convert}"json2excel-wasm";constblob=awaitconvert(json_data_to_export);you can use code like next to force download of the result blob
consta=document.createElement("a");a.href=URL.createObjectURL(blob);a.download="data.xlsx";document.body.append(a);a.click();document.body.remove(a);CDN links are the following:
- https://cdn.dhtmlx.com/libs/json2excel/1.5/worker.js
- https://cdn.dhtmlx.com/libs/json2excel/1.5/module.js
You can import and use lib dynamically like
constconvert=import("https://cdn.dhtmlx.com/libs/json2excel/1.5/module.js");constblob=awaitconvert(json_data_to_export);or use it as web worker
// you need to server worker from the same domain as the main scriptvarworker=newWorker("./worker.js");worker.addEventListener("message",ev=>{if(ev.data.type==="ready"){constblob=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
varurl=window.URL.createObjectURL(newBlob(["importScripts('https://cdn.dhtmlx.com/libs/json2excel/1.5/worker.js');"],{type: "text/javascript"}));varworker=newPromise((res)=>{constx=Worker(url);worker.addEventListener("message",ev=>{if(ev.data.type==="ready"){constjson=ev.data.data;// do something with result}elseif(ev.data.type==="init"){// service is readyres(x);}});});worker.then(x=>x.postMessage({type:"convert",data: raw_json_data}));interfaceIConvertMessageData{uid?: string;data: ISheetData;styles?: IStyles[];wasmPath?: string;// use cdn by default}interfaceIReadyMessageData{uid: string;// same as incoming uidblob: Blob;}interfaceISheetData{name?: string;cols?: IColumnData[];rows?: IRowData[];cells?: IDataCell[][];// if cells mising, use plainplain?: string[][];merged?: IMergedCells;}interfaceIMergedCells{from: IDataPoint;to: IDataPoint;}interfaceIDataPoint{column: number;row: number;}interfaceIColumnData{width: number;}interfaceIRowData{height: number;}interfaceIDataCell{v: string;s: number;}interfaceIStyle{fontSize?: string;align?: string;// left | center | rightverticalAlign?: string;// top | center | bottombackground?: string;color?: string;fontWeight?: string;// boldfontStyle?: string;// italictextDecoration?: string;// underlineformat?: 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 | #000000borderTop?: string;borderRight?: string;borderBottom?: string;borderLeft?: string;}MIT