2017-06-19 426 views
0

我目前正在使用NodeJS生成XLSX電子表格。我正在使用模塊xlsx-populate在Express服務器上創建單頁XLSX文件。如何將2個XLSX文件合併爲一個使用NodeJS

我想知道是否有人知道使用Node將多個XLSX文件合併到一個文件中的方法。

謝謝!

實施例:

const xlsx = require('xlsx-populate'); 

xlsx.fromFileAsync('template1.xlsx') 
    .then((workbook) => { 
    // populate the workbook with stuff 

    return workbook.toFileAsync('spreadsheet1.xlsx'); 
    }) 
    .then(() => xlsx.fromFileAsync('template2.xlsx')) 
    .then((workbook) => { 
    // populate the other workbook with stuff 

    return workbook.toFileAsync('spreadsheet2.xlsx'); 
    }); 

這無極鏈節省兩個單獨XLSX文件(spreadsheet1.xlsx,spreadsheet2.xlsx),每個從對應的模板文件構建的。 xlsx-populate不允許您從同一工作簿上的不同XLSX文件創建多個工作表,因此我想知道是否可以將兩個工作簿合併到一個工作表中?

+0

請包括任何相關的代碼和鏈接到你在嘗試中使用的引用。 – btl

+0

您是否嘗試過創建兩個節點讀取流並將它們管道化爲單個寫入流? – cheesenthusiast

+0

@cheesenthusiast看到編輯,希望更清楚 – DCtheTall

回答

0

添加一個新的工作表到工作簿

此示例使用XLSX.utils.aoa_to_sheet做一個工作表,並追加新的工作表到工作簿:

var new_ws_name = "SheetJS"; 

/* make worksheet */ 
var ws_data = [ 
    [ "S", "h", "e", "e", "t", "J", "S" ], 
    [ 1 , 2 , 3 , 4 , 5 ] 
]; 
var ws = XLSX.utils.aoa_to_sheet(ws_data); 

/* Add the sheet name to the list */ 
wb.SheetNames.push(ws_name); 

/* Load the worksheet object */ 
wb.Sheets[ws_name] = ws; 

來自:https://www.npmjs.com/package/xlsx#streaming-read