MS Excel provides a wide range of features to keep and organize data in the form of worksheets contained by workbooks. In addition to the data organization, it lets you perform sorting, data visualization, mathematical computations and etc. In certain cases, you receive data in JSON format and you need to export it to Excel worksheets programmatically. For such cases, this article covers how to convert JSON data to Excel XLSX/XLS in Node.js.
- Node.js API for JSON to Excel Conversion
- Convert JSON to Excel Worksheet
- Apply Cell Formatting in JSON to Excel Conversion
Node.js API for JSON to Excel Conversion
Aspose.Cells for Node.js via Java is a powerful API that lets you create, modify or convert Excel files in Node.js applications. We will use this API to import data from JSON to Excel worksheets. You can either download the API’s package or install it using the following NPM command.
> npm install aspose.cells
Convert JSON to Excel in Node.js
The following are the steps to convert JSON to Excel in Node.js:
- Create a new Workbook object.
- Get reference of the first worksheet using Workbook.getWorksheets().get(0) method.
- Create an object of JsonLayoutOptions class to set additional options.
- Import data from JSON to Excel using JsonUtility.importData() method.
- Save the Excel file using Workbook.save(string, SaveFormat) method.
The following code sample shows how to import data from JSON to Excel.
Input JSON Data
[{"nodeId":1,"reputation":1134},{"nodeId":2,"reputation":547},{"nodeId":3,"reputation":1703},{"nodeId":4,"reputation":-199},{"nodeId":5,"reputation":-306},{"nodeId":6,"reputation":-49},{"nodeId":7,"reputation":1527},{"nodeId":8,"reputation":1223}]'
Output Excel Worksheet
Convert JSON to Excel with Styles
You can also apply styles in JSON to Excel conversion using Aspose.Cells for Node.js via Java. For example, you can set font, color, alignment, border styles, etc. The following are the steps to apply styles while importing data from JSON to Excel.
- Create a new Workbook object.
- Get reference of the first worksheet using Workbook.getWorksheets().get(0) method.
- Create an object of CellsFactory class and retreive reference of Style using CellsFactory.createStyle() method.
- Set desired styling options.
- Create an object of JsonLayoutOptions class to set additional options.
- Import data from JSON to Excel using JsonUtility.importData() method.
- Save the Excel file using Workbook.save(string, SaveFormat) method.
The following code sample applies styles when importing data from JSON to Excel in Node.js.
Output Excel Worksheet
Get a Free License
You can get a temporary license to use Aspose.Cells for Node.js via Java without evaluation limitations.
Conclusion
JSON files are immensely used to store and share the data among different applications. Often, you need to export data from JSON files to Excel worksheets. Accordingly, in this article, you have learned how to convert JSON to Excel XLSX/XLS using Node.js. Also, you have seen how to apply formatting in JSON to Excel conversion. In order to explore more about Aspose.Cells for Node.js via Java, visit the documentation. Furthermore, you can ask your questions via our forum.