In this article, I’ll show you how to implement Excel automation and create Excel XLSX or XLS files in Node.js applications.
Spreadsheets have made it possible to store a huge amount of data in the form of rows, columns, and sheets. In addition, you can perform a variety of operations on the data such as sorting, applying mathematical formulas, generating charts and graphs, and much more. In the current era of the digital world, automation has taken the place of manual work. As a result, Excel automation has also emerged as a tool to generate spreadsheet documents without any third-party software. Keeping that in mind, I’ll let you know how to create Excel file and insert various objects in worksheets in Node.js.
- Node.js Library to Create Excel Files
- Create Excel File in Node.js
- Modify an Excel File in Node.js
- Create an Excel File having Image
- Insert Charts or Graphs in Excel
- Create a Pivot Table in Excel
Node.js Library to Create Excel Files - Free Download
Aspose.Cells for Node.js is a powerful spreadsheet manipulation library that lets you create, edit, or convert Excel file within Node.js applications. The library’s easy to use methods enable you to perform Excel automation features seamlessly in a few lines of code. You can download the library’s package and integrate it into your Node.js application. For more details, you can visit the Installation and Usage guide.
How to Create Excel XLSX in Node.js
The following are steps to create a simple Excel file in Node.js:
- Create an instance of Workbook class.
- Access the desired worksheet from the workbook using Workbook.getWorksheets.get() method.
- Put the value in the desired cell in the worksheet using the cell’s identifier, such as A1, B3, etc.
- Save the workbook as an Excel file using Workbook.save() method.
The following code sample shows how to create an Excel file in Node.js.
Modify an Excel XLSX in Node.js
You can also load an existing Excel file to update its content. The only thing you’ll have to do is provide the Excel file’s path in the Workbook constructor.
The following code sample shows how to modify the content of an existing Excel file in Node.js.
Generate an Excel File having Image in Node.js
The following are the steps to insert an image in an Excel XLSX/XLS in Node.js.
- Create an instance of Workbook class.
- Access the desired worksheet as well as cell in the worksheet.
- Set the height and width of the cell using setRowHeight() and setColumnWidth() methods of Cell class.
- Insert image using Worksheet.getPictures().add(upperLeftRow, upperLeftColumn, fileName) method.
- Save the workbook as an Excel file.
The following code sample shows how to insert an image in the Excel XLS/XLSX in Node.js.
Node.js: Create Charts in an Excel File
Chart and graphs are important components of Excel files that are used to visually represent the data. Aspose.Cells for Node.js via Java lets you draw and manipulate a variety of charts including sunburst, treemap, histogram, pyramid, and etc. The following are the simple steps to draw a chart in the Excel worksheet.
- Create a new Excel file or load an existing one using Workbook class.
- Add data to the worksheet as a data source for charts.
- Access the chart collection of the worksheet using Worksheet.getCharts() method.
- Add a new chart using Worksheet.getCharts().add() method.
- Access the newly created chart from the collection in a new variable.
- Specify the cells’ range to set NSeries for the chart.
- Save the workbook as an Excel .xlsx file.
The following code sample shows how to create charts in Excel files in Node.js.
Add a Pivot Table in Excel Files in Node.js
Excel files also allow you to create pivot tables based on a range of cells to summarize a large amount of data. The following are the steps to create a pivot table within an Excel worksheet.
- Create a new Workbook or load an existing excel file.
- Insert data into the worksheet (optional).
- Access the pivot table collection using Worksheet.getPivotTables() method.
- Add a new pivot table in the worksheet using Worksheet.getPivotTables().add() method.
- Provide data to the pivot table.
- Save the workbook as Excel file.
The following code sample shows how to create a pivot table in Excel in Node.js.
Node.js Library to Create Excel Files - Get a Free License
You can generate Excel files in Node.js for free without evaluation limitations using a free temporary license.
In this article, you have learned how to create an Excel XLSX or XLS file from scratch in Node.js. We have also seen how to insert data, images, charts, and pivot tables in Excel worksheets. You can learn more about the advanced features of Aspose.Cells for Node.js via Java using the documentation.