Solving the Mystery of Export to Excel in NodeJS

How to create an export to excel service using NodeJS and Express

Faizal Vasaya
Globant
9 min readJan 7, 2021

--

Prerequisite

It would be expected that the reader has basic knowledge of NodeJS and ExpressJS since this read is more towards targeting a specific use case rather than an overview of Node and Express :)

Use case

It was a couple of months back when I was assigned with a mind-boggling task of creating an export to excel service for one of the applications in banking domain. It took us lot of research, struggle and perseverance to come up with the best library for achieving this, hence we thought of sharing this with the open source community.

Let’s lay down the basic requirements for the export to excel service.

  • Exported excel file should contain the list of the customers whose current bank balance is greater than or equal to the specified amount in query parameter.
  • Sheet should be password protected for modifications with the password as the username of the user who raised the request for exporting the file. The username will be available in the request header.
  • Sheet that has a list of customers should have a filter applied by default.
  • First row should freeze when the user scrolls the list of customer.
  • A total row should be displayed at the end to show the total balance of all the customers.

Now that we are aware of the problem statement, let’s solve this mystery step by step. The strategy is to create a NodeJS with Express application which uses a library to export the JSON documents from MongoDB to an excel file. As always, in the npm world, choosing the best library for the defined use case takes a lot of trial and error considering there are tons of libraries available out there. For our use case and feature request, we had to choose from these following available options:

NPM export to excel libraries mapped to its supported feature set
NPM export to excel libraries mapped to its supported feature set

Based on the above mentioned technical and feature set analysis we landed on a decision to use exceljs for our use case. ExcelJS supports password protection out of the box which would enable us to protect the generated files using the username as the password. In addition to that, it supports features such as total row at the end and row freeze as well to freeze the first row which contains the header of the grid.

With that in mind, we are all set to get our hands dirty and dive directly into the code. What are we waiting for? Let’s get the ball rolling.

Application Setup

Step 1: Let’s create an npm package using the following command.

npm init

It should ask you couple of questions, answer them as follows:

Answering the questions asked while npm init
Answering the questions asked while npm init

Step 2: Install express, http-status-code, mongoose, nodemon and dotenv using the following commands.

npm install express http-status-codes mongoose exceljs --save

npm install nodemon dotenv --save-dev

Step 3: Create the following files and folders in your working directory.

Folder structure to begin the development of export to excel service
Folder structure to begin the development of export to excel service

Step 4: Next up we will setup the database. We will be using MongoDB Atlas cloud database but you’re free to setup MongoDB locally as well.

MongoDB Atlas with customer database and customers collection
MongoDB Atlas with customer database and customers collection

Step 5: Let us now insert sample data in customers collection so that it could be used to generate the report. We have used json-generator tool using the schema mentioned below to generate sample records. Copy the schema and you should be able to generate around 33 records for testing purposes.

Step 6: Download the json file from the json generator tool and import it into MongoDB using MongoDB compass tool. This is the last step to up the database.

Customer data imported into MongoDB Compass
Customer data imported into MongoDB Compass

Step 7: Add the following content to .env file which we created in step 3. This file will contain all the configurations related to the application.

Step 8: In the config/config.js file add the following content. The code in this file is responsible to read the environment variables loaded by NodeJS and provide a single access point.

Step 9: In common/db.js add the code related to establish database connection as follows.

Step 10: In the server.js file available at the root level, add the following content to test the connection to the database and run the express application on port 3000

Step 11: Add a start command in package.json file available at the root folder. Here we are setting up nodemon to ensure that the application gets reloaded whenever we save changes to the js files. Your package.json should look like this.

Routes and Validations

Step 1: Let’s create a route that will be invoked by the user to generate an excel file. Add the following content in the routes/customer.js file. Here, we have added couple of validators to check the balance amount received in query parameter and the availability of username in the header (Remember that the report that needs to be generated requires the query parameter and the username in the headers for setting the password)

Step 2: The next step is to create the validators in the validators/customer.js file. Add the following line of code to create a balance validator and a username validator.

Step 3: Now that we have validated our requests, let’s create the controller which would handover the minBalance and username to the service for further processing. Add the following content in controllers/customer.js file.

Ready to munch the meat ?

Step 1: Here comes the export to excel service, whose responsibility is to get the minimum balance and username from controller, query the database to fetch the customers who have balance greater than the minimum balance, create an excel of such customers and the send it back to the controller. First, add the following code to models/customer.js.

Step 2: Next up, we will create a simple service that will fetch customer data whose balance is above minimum balance and return it to the controller. Add the following code in services/customer.js

Step 3: In common/export-to-excel.js and add the following content.

Once we are done with all these steps, run npm start which should show the output on the command prompt below

How to test it

  • A prompt should ask you to save the file at a given location.
  • Once, saved open it using MS Excel.

Known issues:

  • In case you need to support Libre office, this library is not for you. The file generated by this library does not have same behavior in MS Excel and Libre office.
  • In some versions of MS Excel, you might get warnings when you open the file. You may ignore them and proceed ahead.

Hope this was an interesting experiment for you. Feel free to reach out via comments or LinkedIn in case you get issues running the above demonstration :)

--

--

Faizal Vasaya
Globant

Technical Lead | Full-stack JS developer | Ethereum, Web 3.0, Solidity and Blockchain developer | GCP 1x