index.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
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}`); |