Hi All,
I am working on an ODC application where I receive dynamic JSON data at runtime. Since the JSON structure is not known in advance, I need to download this data as an Excel (.xlsx) file.
Currently, I am using a DataTable to display the dynamic JSON data, as it handles the dynamic structure. However, I am looking for a way to export this data to an Excel file.
Could anyone suggest a suitable approach to achieve this in ODC?
Thanks in advance!
Hi @Vinod Kumar ,
You can use SheetJS to handle dynamic JSON data when exporting to Excel. Refer to the documentation below and modify the function according to your requirements.
https://docs.sheetjs.com/docs/api/write-options
Hope this will help you.
Hi Vinod,
Adding to Deepa's suggestion, here is how to wire SheetJS into an ODC app end to end, since the structure is dynamic and the built-in Excel actions need a fixed record structure:
1. Add the library to the appDownload xlsx.full.min.js from the SheetJS CDN/docs. In ODC Studio go to Interface > Scripts, import the file, then on the screen (or block) that exports, add it under Required Scripts so it loads with the screen.
xlsx.full.min.js
2. Create a client action ExportJsonToExcel with inputs JsonText (Text) and FileName (Text), containing a JavaScript node with the same inputs:
ExportJsonToExcel
JsonText
FileName
var data = JSON.parse($parameters.JsonText); if (!Array.isArray(data)) { data = [data]; } // flatten nested objects so each becomes its own column, e.g. address.city function flat(o, p, r) { r = r || {}; for (var k in o) { var v = o[k], key = p ? p + "." + k : k; if (v !== null && typeof v === "object" && !Array.isArray(v)) { flat(v, key, r); } else { r[key] = Array.isArray(v) ? JSON.stringify(v) : v; } } return r; } var rows = data.map(function (x) { return flat(x); }); var ws = XLSX.utils.json_to_sheet(rows); var wb = XLSX.utils.book_new(); XLSX.utils.book_append_sheet(wb, ws, "Data"); XLSX.writeFile(wb, $parameters.FileName || "export.xlsx");
json_to_sheet builds the header row from the union of keys across all rows, so it copes with records that don't all have the same fields.
json_to_sheet
3. Call it from the Export button's client action, passing the same JSON text you feed to the DataTable. Everything runs in the browser, so no server round trip is needed and large files don't hit request size limits.
If you ever need to generate the file on the server instead (e.g. in a Timer or to email it), the ODC option is External Logic: a small C# library using ClosedXML or similar that receives the JSON and returns the file as Binary Data.
Hope this helps!