
How to Create a Pivot Table in Excel using Node.js
Excel files allow you to modify data at any stage of visualization or analysis. Microsoft Excel remains the top tool for organizing data and performing complex calculations. In this post we show how to create a Pivot table in Excel using Node.js, and how to generate a Pivot chart from that table. We will use the Excel JavaScript API in a Node.js project.
The guide covers:
- Excel JavaScript API installation
- How to Create a Pivot Table in Excel using Node.js
- How to make a Pivot chart in Excel programmatically
Excel JavaScript API installation
Install the library by downloading the package or running the commands below:
npm install java
npm install aspose.cells
How to Create a Pivot Table in Excel using Node.js
Follow these steps and the code snippet to create a Pivot table programmatically.
- Instantiate a Workbook object.
- Retrieve the first worksheet with the get(index) method.
- Rename the worksheet using the setName method.
- Access the Cells collection via getCells.
- Get a specific cell with Get.
- Set the cell value using setValue.
- Retrieve a range cell with get.
- Add a new sheet using add.
- Get the new worksheet with get.
- Rename the sheet via setName.
- Add a Pivot Table to the worksheet with add.
- Show row grand totals using setRowGrand.
- Show column grand totals using setColumnGrand.
- Enable automatic formatting with setAutoFormat.
- Choose an auto‑format type via setAutoFormatType.
- Drag fields to the row area using addFieldToArea.
- Set the number format of the first data field with getDataFields.
- Save the workbook using save.
Copy and paste the code below into your main file:
The result looks like this:

How to make a Pivot chart in Excel programmatically
Now we will create a Pivot chart based on the Pivot table.
- Create a Workbook instance.
- Add a worksheet with the add method.
- Retrieve the worksheet using get.
- Rename the sheet via setName.
- Add a column chart with add.
- Get the chart object using get.
- Set the chart’s data source with setPivotSource.
- Hide pivot field buttons when appropriate via setHidePivotFieldButtons.
- Save the workbook with save.
Insert the following code into your main file:
The output is shown below:

Get a Free License
You can obtain a free temporary license to try the API without evaluation limits.
Summing up
You have explored the enterprise‑level Excel JavaScript API, learned how to create a Pivot table in Excel using Node.js, and added code to generate a Pivot chart. For more features, visit the documentation.
Also, follow our Getting Started guide.
Stay tuned to conholdate.com for the latest blog posts.
Ask a question
Post your questions on our forum.
FAQs
How do you create a PivotTable in Excel and edit it?
Install the Excel JavaScript API and follow the steps and code snippet linked above.