Skip to content

Instantly share code, notes, and snippets.

@himanshuteotia
Created May 30, 2019 09:42
Show Gist options
  • Save himanshuteotia/ce100b2852de7195cc05e2584753707e to your computer and use it in GitHub Desktop.
Save himanshuteotia/ce100b2852de7195cc05e2584753707e to your computer and use it in GitHub Desktop.
combine multiple xlsx files in a directory and save them into one file
var xlsx = require("xlsx");
var fs = require("fs");
function readFileToJson(filename) {
var wb = xlsx.readFile(filename);
var firstTabName = wb.SheetNames[0];
var ws = wb.Sheets[firstTabName];
var data = xlsx.utils.sheet_to_json(ws);
return data;
}
var target_dir = __dirname + "/files";
var files = fs.readdirSync(target_dir);
console.log(files);
var combinedData = [];
files.forEach(function(file) {
var data = readFileToJson(`./files/${file}`);
combinedData = combinedData.concat(data);
});
var newWB = xlsx.utils.book_new();
var newWS = xlsx.utils.json_to_sheet(combinedData);
xlsx.utils.book_append_sheet(newWB, newWS, "urls data");
xlsx.writeFile(newWB, "newCombinedData.xlsx");
console.log("Done");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment