简体   繁体   English

如何使用NodeJS将2个XLSX文件合并为一个

[英]How to combine 2 XLSX files into one using NodeJS

I am currently working with generating XLSX spreadsheets using NodeJS. 我目前正在使用NodeJS生成XLSX电子表格。 I am using the module xlsx-populate to create single-sheet XLSX files on an Express server. 我正在使用模块xlsx-populate在Express服务器上创建单页XLSX文件。

I was wondering if anyone knew a way to combine multiple XLSX files into one file with multiple sheets using Node. 我想知道是否有人知道一种使用Node将多个XLSX文件合并为一个具有多个工作表的文件的方法。

Thanks! 谢谢!

Example: 例:

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');
  });

This Promise chain saves two separate XLSX files (spreadsheet1.xlsx, spreadsheet2.xlsx), each being built from a corresponding template file. 此Promise链保存两个单独的XLSX文件(spreadsheet1.xlsx,sheetsheet2.xlsx),每个文件都是从相应的模板文件构建的。 xlsx-populate does not let you create multiple sheets from different XLSX files on the same workbook, so I am wondering if it is possible to combine the two workbooks into one with multiple sheets? xlsx-populate不允许您从同一工作簿上的不同XLSX文件创建多个工作表,所以我想知道是否可以将两个工作簿合并为一个包含多个工作表的工作表?

EDIT 编辑

I ended up switching modules to excel4node which I found to be a more flexible, albeit more complex, module. 我最终将模块切换到excel4node,我发现它是一个更灵活但更复杂的模块。 My problem was that I had two template files, each containing an image, I wanted to merge into one file using xlsx-populate. 我的问题是我有两个模板文件,每个文件都包含一个图像,我想使用xlsx-populate合并为一个文件。

Since I failed to failed to find a successful way to merge the two templates to one file using xlsx-populate, I used excel4node to rebuild the template files from scratch, inserting the images (which xlsx-populate does not support). 由于无法找到使用xlsx-populate将两个模板合并到一个文件的成功方法,因此我使用excel4node从头开始重建模板文件,并插入图像(xlsx-populate不支持)。

Adding a new worksheet to a workbook 将新工作表添加到工作簿

This example uses XLSX.utils.aoa_to_sheet to make a worksheet and appends the new worksheet to the workbook: 本示例使用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;

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

This snippet can merge two excel files into a new one. 此代码段可以将两个excel文件合并为一个新文件。

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

 Promise.all([
    XlsxPopulate.fromFileAsync('./src/data/template.xlsx'),
    XlsxPopulate.fromFileAsync('./src/data/template2.xlsx')
 ])
  .then(workbooks => {
    const workbook = workbooks[0];
    const workbook2 = workbooks[1];
    const sheets2 = workbook2.sheets();

    sheets2.forEach(sheet => {
      const newSheet = workbook.addSheet(sheet.name());
      const usedRange = sheet.usedRange();
      const oldValues = usedRange.value();

      newSheet.range(usedRange.address()).value(oldValues);
    });

    return workbook.toFileAsync('./src/data/xlsx-populate/spreadsheet2.xlsx');
 });

Known Issue: 已知问题:

It will lose the second one's style, that's because the copy style feature was under development, please refer here . 它将失去第二种样式,因为复制样式功能正在开发中,请参阅此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM