Convert JSON to Excel in Node.js

Convert JSON to Excel in Node.js

Being a developer, you deal with JSON data format very frequently, and WHAT IF you need to parse that JSON to an Excel sheet? This Node.js Excel API offers a huge stack of methods that help you automate this conversion robustly and efficiently. In addition, you can manipulate XLSX/XLS files as per the requirements such as adding custom styles, data import/export, file format conversion, and more. In this blog post, we will write the code snippet to convert JSON to Excel in Node.js.

We will cover the following sections:

Node.js Excel API installation

The installation of this API is very quick as you can install it using a couple of commands. However, you can integrate this library in the following two ways.

Open the terminal, and run the following commands to install it.

Note: You can skip the first command in case you have set up Java on your machine.

npm install java
npm install aspose.cells

Secondly, you can download the API package and enable this library in your Node.js project.

Convert JSON to Excel in Node.js

In this section, we can jump to the code snippet that will convert JSON data to an Excel file programmatically. You can see a long list of methods of this Node.js Excel API available here.

The following steps will be performed:

  1. Get the aspose.cells modules into your Node.js project.
  2. Initialize an object of the Workbook class.
  3. Call the get method to access the default empty worksheet.
  4. Instantiate an instance of JsonLayoutOptions class for formatting.
  5. setArrayAsTable method processes Array as a table.
  6. Import JSON data to the default worksheet by calling importData method.
  7. Invoke save method to save the resultant file.

Copy & paste the following code into your main file:

You can see the output in the image below:

JSON to Excel in Node.js

Import JSON to Excel - advance options

In addition, there is a wide range of methods offered by this Node.js Excel API. However, we will use some features to know the scope and usage.

Following are the steps:

  1. Initialize an object of the Workbook class.
  2. Invoke the get method to access the default empty worksheet.
  3. Create an instance of JsonLayoutOptions class for formatting.
  4. Call the setArrayAsTable method processes Array as a table.
  5. setIgnoreArrayTitle method will ignore the title if the array is a property of the object.
  6. Call the setIgnoreNull method to ignore NULL values.
  7. Create an instance of CellsFactory class.
  8. Call createStyle method to create a style.
  9. Invoke the setColor method to set the color.
  10. Set the style of the title by calling the setTitleStyle method.
  11. Import JSON data to the default worksheet by calling importData method.
  12. Call the save method to save the resultant file.

Get a Free License

You can get a free temporary license to try the API without evaluation limitations.

Summing up

I hope this blog helped you. We have gone through how to convert JSON to Excel in Node.js programmatically. We also have gone through some prominent features of this Node.js Excel API. However, you can explore the documentation to know about other methods. Moreover, you can jump to the “See Also” section to visit relevant articles. conholdate.com is continuously writing on new interesting topics. Therefore, please stay connected for the latest updates.

Ask a question

You can let us know about your questions or queries on our forum.

FAQs

How do I convert JSON data to Excel?

You can convert/import JSON data format to Excel sheet using this Node.js Excel library. It is rich featured, easy to install and comes with comprehensive documentation.

Can JSON be used in Excel?

Yes, this API offers a wide range of methods to parse JSON into Excel sheets and also provides provisions to manipulate the conversion process.

How do I pull data from API to Excel?

This library has exposed importData method that imports JSON data format into an Excel sheet. Moreover, you can set the layout, style the data and more.

See Also