In this article, I’ll show you how to implement Excel automation and create Excel XLSX/XLS file in Node.js applications programmatically.
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.
- Excel API for Node.js
- Create Excel File in Node.js
- Insert Data into an Excel File in Node.js
- Create an Excel File having Image in Node.js
- Insert Charts or Graphs in Excel in Node.js
- Create a Pivot Table in Excel in Node.js
Create Excel File with Node.js API
Aspose.Cells for Node.js via Java is a powerful spreadsheet manipulation API that lets you create, edit, or convert Excel file within Node.js applications. The API’s easy to use methods enable you to perform Excel automation features seamlessly in a few lines of code. You can download the API’s package and integrate it into your Node.js application. For more details, you can visit the Installation and Usage guide.
Create Excel XLSX File in Node.js
The following are steps to create a simple Excel file using Aspose.Cells for Node.js via Java:
- 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.
Insert Data into 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 update the content of an existing Excel file in Node.js.
Create an Excel File having Image in Node.js
The following are the steps to insert an image in an Excel worksheet 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 worksheet in Node.js.
Insert Charts or Graphs in Excel in Node.js
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 draw charts in Excel in Node.js.
Create a Pivot Table in Excel 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.
In this article, you have learned how to create an Excel XLSX or XLS file from scratch in Node.js applications. 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.