Hey there, awesome data explorers! Have you ever found yourself in a situation where you have a bunch of separate Excel files, and you really, really need to put all their information together into just one big Excel sheet? It can feel a bit like trying to herd cats, right? Especially if you’re doing it manually, copying and pasting data from one file to another. Ugh, what a nightmare!
Table of Contents
But what if I told you there’s a super cool and surprisingly simple way to do this automatically, using something called Node.js and a fantastic tool called SheetJS? Yes, you heard that right! In this friendly guide, we’re going to dive into how you can merge Excel files Node.js style, making your life a whole lot easier. By the end of this tutorial, you’ll be a pro at combining your data with just a few lines of code. So, let’s get started on this amazing journey in 2025!
Why Merge Excel Files Node.js?
You might be wondering, “Why should I use Node.js to merge Excel files Node.js when I can just copy and paste?” That’s a great question! Imagine you have dozens, hundreds, or even thousands of Excel files that need to be combined. Manually doing that would take ages, and honestly, you’d probably make a mistake or two (because we’re all human, right?).
This is where Node.js comes to the rescue!
- Speed: Node.js can process these files super fast, saving you tons of time.
- Accuracy: Computers don’t get tired or make typos. Your merged data will be exactly as it should be.
- Automation: Once you write the code, you can use it again and again for different sets of files. It’s like having a little robot helper!
- Efficiency: It frees up your time to do more important and creative tasks instead of boring, repetitive ones.
So, whether you’re a student, a data analyst, or just someone who deals with a lot of spreadsheets, learning to merge Excel files Node.js is a truly valuable skill.
Getting Ready: What You’ll Need to Merge Excel Files Node.js
Before we jump into the fun part of coding, we need to make sure your computer is ready for our little adventure. Don’t worry, it’s pretty straightforward!
- Node.js: Think of Node.js as the engine that runs our JavaScript code outside of a web browser. If you don’t have it installed yet, no worries! You can download it from the official Node.js website: https://nodejs.org/en/download. Just download the recommended version for your operating system (Windows, macOS, or Linux) and follow the installation steps. It’s usually just clicking “Next” a few times.
- How to check if Node.js is installed: Open your computer’s “Terminal” (on macOS/Linux) or “Command Prompt”/”PowerShell” (on Windows). Then, type
node -v
and press Enter. If you see a version number (likev18.17.0
or similar), you’re good to go!
- How to check if Node.js is installed: Open your computer’s “Terminal” (on macOS/Linux) or “Command Prompt”/”PowerShell” (on Windows). Then, type
- npm (Node Package Manager): npm comes automatically with Node.js. It’s like an app store for Node.js projects, where we can get useful tools (called “packages”).
- How to check if npm is installed: In your Terminal/Command Prompt, type
npm -v
and press Enter. You should see a version number.
- How to check if npm is installed: In your Terminal/Command Prompt, type
- SheetJS (xlsx module): This is the superstar tool we’ll be using to read, write, and manipulate Excel files. We’ll install this using npm in the next step.
- Two Sample Excel Files: To test our code, you’ll need at least two simple Excel files. Let’s imagine they look something like this:
1.xlsx
| Name | Age | City |
| :—- | :– | :——- |
| Pawan | 34 | Gorakhpur |
| Ajay | 41 | Deoria |
2.xlsx
| Name | Age | City |
| :—— | :– | :—— |
| Vinayak | 9 | New Delhi |
| Preesha | 7 | New Delhi |
Make sure these files are in the same folder where you’ll be writing your Node.js code. You can name them 1.xlsx
and 2.xlsx
for this tutorial to match our example code.
Step-by-Step Guide to Merge Excel Files Node.js
Alright, brave coders, let’s roll up our sleeves and get our hands dirty (in a good way!) with some code. We’ll go through this step by step, so don’t worry if you’re new to this.
Step 1: Create Your Project Folder
First things first, let’s create a special place on your computer for our project.
Open your Terminal or Command Prompt.
Navigate to a folder where you want to create your project. For example, you can type cd Desktop
to go to your desktop.
Now, create a new folder for our project: Bash
mkdir excel-merger
Go into that new folder:
Bash
cd excel-merger
This excel-merger
folder is where all your code and Excel files will live.
Step 2: Initialize Your Node.js Project
Every Node.js project needs a special file called package.json
. This file keeps track of all the tools (packages) your project needs.
While still inside your excel-merger
folder in the Terminal/Command Prompt, type:
Bash
npm init -y
The -y
flag just tells npm to say “yes” to all the default settings, making it quicker. After this, you’ll see a new file named package.json
inside your excel-merger
folder.
Step 3: Install the SheetJS (xlsx) Module
Now, let’s get the star of our show, the xlsx
module!
- In your Terminal/Command Prompt (still in
excel-merger
folder), type: Bashnpm install xlsx
This command tells npm to download and install thexlsx
package into your project. You’ll see a new folder callednode_modules
and a file calledpackage-lock.json
appear. These are where npm keeps all the installed packages and their details.
Step 4: Create Your JavaScript File
Now, let’s write the code!
- Inside your
excel-merger
folder, create a new file namedmerge-excel.js
. You can use any text editor for this (like Notepad, VS Code, Sublime Text, etc.).
Step 5: Write the Code to Merge Excel Files Node.js
Open merge-excel.js
and paste the following code into it. Don’t worry, we’ll break down what each part does right after!
JavaScript
const XLSX = require('xlsx'); // This line brings in the SheetJS (xlsx) library
const path = require('path'); // This line helps us work with file paths
// Step 1: Tell our program where the Excel files are
// path.join(__dirname, '1.xlsx') means "look for '1.xlsx' in the same folder as this script"
const wb1 = XLSX.readFile(path.join(__dirname, '1.xlsx'));
const wb2 = XLSX.readFile(path.join(__dirname, '2.xlsx'));
// Step 2: Get the very first sheet from each Excel file
// Excel files can have multiple sheets, like different tabs. We're picking the first one.
const ws1 = wb1.Sheets[wb1.SheetNames[0]];
const ws2 = wb2.Sheets[wb2.SheetNames[0]];
// Step 3: Turn the data from each sheet into a format JavaScript understands easily (JSON)
// JSON is like a list of simple objects, perfect for combining.
const data1 = XLSX.utils.sheet_to_json(ws1);
const data2 = XLSX.utils.sheet_to_json(ws2);
// Step 4: Combine the data from both files into one big list
// The '...' (spread operator) helps us quickly put all items from data1 and data2 together.
const mergedData = [...data1, ...data2];
// Step 5: Create a brand new Excel sheet from our combined data
const mergedSheet = XLSX.utils.json_to_sheet(mergedData);
// Step 6: Create a brand new Excel workbook (think of it as a new Excel file)
const mergedWorkbook = XLSX.utils.book_new();
// Step 7: Add our newly created 'mergedSheet' to the new workbook
// We're also giving this new sheet a name: 'MergedSheet'
XLSX.utils.book_append_sheet(mergedWorkbook, mergedSheet, 'MergedSheet');
// Step 8: Decide where to save our new merged Excel file
const outputPath = path.join(__dirname, 'merged.xlsx');
// Step 9: Save the new Excel file to our computer
XLSX.writeFile(mergedWorkbook, outputPath);
// Step 10: Tell us that everything worked successfully!
console.log(`✅ Merged rows saved to ${outputPath}`);
Code Explanation for Merge Excel Files Node.js
Let’s break down that code piece by piece so you truly understand how we merge Excel files Node.js:
const XLSX = require('xlsx');
: This line is super important! It’s like saying, “Hey Node.js, I want to use thexlsx
library I just installed. Please make it available to me and let me call itXLSX
.”const path = require('path');
: This line brings in another helpful built-in Node.js tool calledpath
. It helps us deal with file and folder locations on our computer, making sure our code works correctly no matter where our files are.const wb1 = XLSX.readFile(path.join(__dirname, '1.xlsx'));
: Here, we’re usingXLSX.readFile()
to open our first Excel file,1.xlsx
.path.join(__dirname, '1.xlsx')
is a smart way to tell the program to look for1.xlsx
in the same folder where ourmerge-excel.js
script is located.wb1
now holds the entire first workbook (Excel file). We do the same for2.xlsx
to getwb2
.const ws1 = wb1.Sheets[wb1.SheetNames[0]];
: An Excel file can have many “sheets” (those tabs at the bottom like “Sheet1”, “Sheet2”).wb1.SheetNames
gives us a list of all the sheet names inwb1
.[0]
means we want to pick the first sheet from that list. So,ws1
now represents the data from the first sheet of1.xlsx
. We do the same forws2
.const data1 = XLSX.utils.sheet_to_json(ws1);
: This is where the magic happens!XLSX.utils.sheet_to_json()
is a fantastic function that takes the data from an Excel sheet (ws1
) and converts it into a format called JSON (JavaScript Object Notation). JSON is super easy for JavaScript to work with, as it represents our data as an array of objects, where each object is a row, and its properties are the column headers. We do this for bothws1
andws2
.const mergedData = [...data1, ...data2];
: This is a very cool JavaScript trick called the “spread operator” (...
). It basically takes all the items out ofdata1
and all the items out ofdata2
and puts them all together into one single, new array calledmergedData
. Now,mergedData
contains all the rows from both our original Excel files!const mergedSheet = XLSX.utils.json_to_sheet(mergedData);
: Now that we have all our combined data inmergedData
(which is in JSON format), we useXLSX.utils.json_to_sheet()
to turn it back into an Excel sheet format that SheetJS understands.const mergedWorkbook = XLSX.utils.book_new();
: We create a brand new, empty Excel “workbook” (which is like an empty Excel file).XLSX.utils.book_append_sheet(mergedWorkbook, mergedSheet, 'MergedSheet');
: We take our newly createdmergedSheet
(which has all our combined data) and add it to ourmergedWorkbook
. We also give this new sheet a name:'MergedSheet'
.const outputPath = path.join(__dirname, 'merged.xlsx');
: We define the name and location for our final, merged Excel file. It will be namedmerged.xlsx
and saved in the same folder as our script.XLSX.writeFile(mergedWorkbook, outputPath);
: Finally, this line saves ourmergedWorkbook
(which now contains our combined data) to the hard drive asmerged.xlsx
.console.log(...);
: This just prints a nice message in our Terminal/Command Prompt to let us know that the process was successful and where the new file is saved.
Running Your Code to Merge Excel Files Node.js
You’re almost there! Now it’s time to see your code in action.
1. Make sure your 1.xlsx
and 2.xlsx
files are in the same excel-merger
folder as your merge-excel.js
file.
2. Open your Terminal or Command Prompt.
3. Navigate to your excel-merger
folder (if you’re not already there):
Bash
cd excel-merger
4. Now, run your script using Node.js:
Bash
node merge-excel.js
After a moment, you should see the message: ✅ Merged rows saved to merged.xlsx
.
Go check your excel-merger
folder, and you’ll find a brand new file named merged.xlsx
! Open it up, and you’ll see all the data from 1.xlsx
and 2.xlsx
happily combined into one sheet. How cool is that?!
Full Code Example for Merge Excel Files Node.js
Here’s the complete code you used, all in one place, for your convenience. Remember, this code goes into your merge-excel.js
file.
JavaScript
const XLSX = require('xlsx');
const path = require('path');
// Read both Excel files
const wb1 = XLSX.readFile(path.join(__dirname, '1.xlsx'));
const wb2 = XLSX.readFile(path.join(__dirname, '2.xlsx'));
// Get first sheet from each
const ws1 = wb1.Sheets[wb1.SheetNames[0]];
const ws2 = wb2.Sheets[wb2.SheetNames[0]];
// Convert sheets to JSON
const data1 = XLSX.utils.sheet_to_json(ws1);
const data2 = XLSX.utils.sheet_to_json(ws2);
// Merge rows
const mergedData = [...data1, ...data2];
// Create new sheet from merged data
const mergedSheet = XLSX.utils.json_to_sheet(mergedData);
// Create new workbook and append merged sheet
const mergedWorkbook = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(mergedWorkbook, mergedSheet, 'MergedSheet');
// Save the new Excel file
const outputPath = path.join(__dirname, 'merged.xlsx');
XLSX.writeFile(mergedWorkbook, outputPath);
console.log(`✅ Merged rows saved to ${outputPath}`);
Tips and Tricks for Merge Excel Files Node.js
- Handling More Than Two Files: What if you have 3, 5, or even 100 Excel files? You can modify the code to loop through a list of file names and combine them all. You could put all your Excel files in a subfolder (e.g.,
input_files
) and usefs.readdir
(another Node.js module for file system operations) to get all the.xlsx
files in that folder. - Error Handling: In real-world applications, it’s a good idea to add “error handling.” This means adding code that checks if a file exists before trying to read it, or if something goes wrong during the process, it tells you nicely instead of crashing.
- Column Mismatches: Our current code assumes your Excel files have the same column headers. If they don’t, SheetJS will still combine them, but you might end up with extra columns in your merged file. For more advanced scenarios, you might need to process the data more carefully before merging.
- Specific Sheets: If your Excel files have multiple sheets and you want to merge a specific one (not just the first one), you can change
wb1.SheetNames[0]
towb1.Sheets['Your Sheet Name']
. - Performance for Large Files: For extremely large Excel files (tens of thousands or millions of rows), you might need to look into SheetJS’s streaming API, which processes data in smaller chunks to save memory. But for most everyday tasks, the method we learned today is perfect!
Where to Go Next?
Now that you’ve mastered how to merge Excel files Node.js, what’s next? The SheetJS library is incredibly powerful, and this is just the tip of the iceberg!
- Reading Specific Cells: Learn how to read data from a particular cell (like A1, B5, etc.).
- Writing Data to Excel: You can use SheetJS to create brand new Excel files from scratch or add new data to existing ones.
- Formatting Cells: Explore how to add styling, colors, and other formatting to your Excel sheets using code.
- Advanced Data Manipulation: Combine SheetJS with other Node.js libraries to filter data, perform calculations, or even generate charts.
Check out the official SheetJS documentation for more advanced features: https://docs.sheetjs.com/
Conclusion
Phew! You’ve done an amazing job today. You’ve learned a super useful skill: how to effortlessly merge Excel files Node.js using the awesome SheetJS (xlsx) module. No more tedious copy-pasting! You’re now equipped to automate your data merging tasks, saving you time and ensuring accuracy. This is a fantastic step in your journey to becoming a coding wizard!
Keep experimenting, keep learning, and don’t be afraid to try new things. The world of programming is full of exciting possibilities!
Did you find this guide helpful in learning how to merge Excel files Node.js? Please let us know if you liked it or if there’s anything we can improve!
👍 Like this post if it helped you!
👎 Dislike this post if it didn’t meet your expectations.
Share this post with your friends or on your social media handles (like Twitter, LinkedIn, or Facebook) if you think it could help them too! Your support means the world to us. Happy coding!