Excel file manipulation and conversion to other file formats programmatically offer a competitive edge to business applications. In fact, you can add charts, font styles, and other graphical elements to the XLS/XLSX files using a few lines of source code in any programming language. However, we will use this XLSX JS library to automate this process. In this blog post, we will learn how to make charts in Excel using Node.js programmatically.
We will cover the following points:
XLSX JS library installation
This XLSX JS library can be installed easily without any third-party dependency. However, it lets you either download the API package or install it by running the following commands:
npm install java
npm install aspose.cells
How to Make Charts in Excel using Node.js
Once API is installed, we can move to the next step. Let’s write steps and the code snippet to make charts in Excel using Node.js.
You may follow the following steps:
- Create an instance of the Workbook class.
- Obtaining the reference of the first worksheet by calling the get(index) method.
- Adding sample values to cells by calling the putValue method.
- Invoke the add method to add a chart to the worksheet.
- Access the instance of the newly added chart by calling the get(index) method.
- Call the setChartDataRange method to set the chart data source as the range “A1:C4”.
- The save method will save the file in XLSX format.
Copy & paste the following code into your main file:
var aspose = aspose || {}; | |
aspose.cells = require("aspose.cells"); | |
// Create an instance of the Workbook class | |
var workbook = new aspose.cells.Workbook(); | |
// Obtaining the reference of the first worksheet by calling the get(index) method | |
var worksheet = workbook.getWorksheets().get(0); | |
// Adding sample values to cells by calling the putValue method | |
worksheet.getCells().get("A2").putValue("Category1"); | |
worksheet.getCells().get("A3").putValue("Category2"); | |
worksheet.getCells().get("A4").putValue("Category3"); | |
worksheet.getCells().get("B1").putValue("Column1"); | |
worksheet.getCells().get("B2").putValue(4); | |
worksheet.getCells().get("B3").putValue(20); | |
worksheet.getCells().get("B4").putValue(50); | |
worksheet.getCells().get("C1").putValue("Column2"); | |
worksheet.getCells().get("C2").putValue(50); | |
worksheet.getCells().get("C3").putValue(100); | |
worksheet.getCells().get("C4").putValue(150); | |
// Invoke the add method to add a chart to the worksheet | |
var chartIndex = worksheet.getCharts().add(aspose.cells.ChartType.COLUMN, 5, 0, 15, 5); | |
// Access the instance of the newly added chart by calling the get(index) method | |
var chart = worksheet.getCharts().get(chartIndex); | |
// Call the method to set chart data source as the range "A1:C4" | |
chart.setChartDataRange("A1:C4", true); | |
// The save method will save the file in xlsx format | |
workbook.save( "ColumnChart.xlsx", aspose.cells.SaveFormat.XLSX); |
You can see the output in the image below:
Get a Free License
You can avail a free temporary license to try the API without evaluation limitations.
Summing up
We can end this blog post here. We have demonstrated how to make Charts in Excel using Node.js programmatically. In addition, you may navigate to the documentation of this XLSX JS library to know the other provisions & methods. In the coming days, we will come up with other blog posts to create pivot chart in Excel files programmatically. Therefore, please visit conholdate.com for the latest updates.
Ask a question
You can let us know about your questions or queries on our forum.
FAQs
How do I create a Node.js Excel spreadsheet?
You may install this XLSX JS library to create, and process spreadsheets programmatically.
How do I make Excel charts automatically?
Please follow this link to learn how to write the code snippet to create charts in Excel files in Node.js.