Convert JSON to Excel with Node.js via C++
Convert JSON to Excel Workbook
No need to wonder how to convert JSON to Excel file, because Aspose.Cells for Node.js via C++ provides the best solution. The Aspose.Cells API provides support for converting JSON format to spreadsheets. You can use JsonLoadOptions class to specify additional settings for importing JSON to Workbook.
The following code example demonstrates importing JSON to Excel Workbook. Please see the code to convert source file to the xlsx file generated by the code for reference.
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const filePath = path.join(dataDir, "sample.json");
// create a Workbook object
const wb = new AsposeCells.Workbook(filePath);
// save file to xlsx format
wb.save("sample_out.xlsx");
The following code example, which uses the JsonLoadOptions class to specify additional settings, demonstrates importing JSON to Excel Workbook. Please see the code to convert source file to the xlsx file generated by the code for reference.
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const filePath = path.join(dataDir, "sample.json");
// Create an options of loading the file.
const options = new AsposeCells.JsonLoadOptions();
options.setMultipleWorksheets(true);
// Loads the workbook from JSON file
const book = new AsposeCells.Workbook(filePath, options);
// Save file to xlsx format
book.save("sample_out.xlsx");
The following code example demonstrates importing a JSON string to Excel Workbook. You can also specify the location of the layout when importing JSON. Please see the code to convert a JSON string to the xlsx file generated by the code for reference.
const AsposeCells = require("aspose.cells.node");
const path = require("path");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const inputJson = JSON.stringify([
{ BEFORE: 'before cell', TEST: 'asd1', AFTER: 'after cell' },
{ BEFORE: 'before cell', TEST: 'asd2', AFTER: 'after cell' },
{ BEFORE: 'before cell', TEST: 'asd3', AFTER: 'after cell' },
{ BEFORE: 'before cell', TEST: 'asd4', AFTER: 'after cell' }
]);
const sheetName = "Sheet1";
const row = 3;
const column = 2;
// create a Workbook object
const book = new AsposeCells.Workbook();
const worksheet = book.getWorksheets().get(sheetName);
// set JsonLayoutOptions to treat Arrays as Table
const jsonLayoutOptions = new AsposeCells.JsonLayoutOptions();
jsonLayoutOptions.setArrayAsTable(true);
AsposeCells.JsonUtility.importData(inputJson, worksheet.getCells(), row, column, jsonLayoutOptions);
// save file to xlsx format
book.save("out.xlsx");