How to Merge Excel Files in Node.js
Looking for an easy way to combine Excel files in your application workflow? The below API solution will seamlessly merge two Excel XLSX documents into one, requiring only a few lines of ready-to-run code in the process. This solution is a great way to add a quick, consistent & reliable file processing mechanism into your applications; the resulting Excel document will contain multiple worksheets (in the same order the documents were put included in the operation).
To install the Node.js SDK, first run the below command:
npm install cloudmersive-convert-api-client --save
Or add this snippet (below) to your package.json:
"dependencies": {
"cloudmersive-convert-api-client": "^2.6.3"
}
Lastly, copy & paste the below code. Before calling this API, prepare to satisfy the following request parameters:
- var inputFile1 — the first file to perform the operation on (these worksheets will show up first in the resulting document)
- var inputFile2 — the second file to perform the operation on (these worksheets will show up directly following the final worksheet from the first input)
- var Apikey — supply a free-tier Cloudmersive API key in this field (you can get one by registering a free account on our website)
var CloudmersiveConvertApiClient = require('cloudmersive-convert-api-client');
var defaultClient = CloudmersiveConvertApiClient.ApiClient.instance;
// Configure API key authorization: Apikey
var Apikey = defaultClient.authentications['Apikey'];
Apikey.apiKey = 'YOUR API KEY';
var apiInstance = new CloudmersiveConvertApiClient.MergeDocumentApi();
var inputFile1 = Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer); // File | First input file to perform the operation on.
var inputFile2 = Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer); // File | Second input file to perform the operation on (more than 2 can be supplied).
var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.mergeDocumentXlsx(inputFile1, inputFile2, callback);
After that, you’re all done!