javascript - How to combine 2 XLSX files into one using NodeJS - Stack Overflow

I am currently working with generating XLSX spreadsheets using NodeJS. I am using the module xlsx-popul

I am currently working with generating XLSX spreadsheets using NodeJS. I am using the module xlsx-populate to create single-sheet XLSX files on an Express server.

I was wondering if anyone knew a way to bine multiple XLSX files into one file with multiple sheets using Node.

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. 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 bine the two workbooks into one with multiple sheets?

EDIT

I ended up switching modules to excel4node which I found to be a more flexible, albeit more plex, module. My problem was that I had two template files, each containing an image, I wanted to merge into one file using 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).

I am currently working with generating XLSX spreadsheets using NodeJS. I am using the module xlsx-populate to create single-sheet XLSX files on an Express server.

I was wondering if anyone knew a way to bine multiple XLSX files into one file with multiple sheets using Node.

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. 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 bine the two workbooks into one with multiple sheets?

EDIT

I ended up switching modules to excel4node which I found to be a more flexible, albeit more plex, module. My problem was that I had two template files, each containing an image, I wanted to merge into one file using 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).

Share Improve this question edited May 19, 2018 at 2:23 DCtheTall asked Jun 19, 2017 at 23:06 DCtheTallDCtheTall 1961 gold badge2 silver badges7 bronze badges 3
  • Please include any relevant code and links to references you have used in your attempts. – user320487 Commented Jun 19, 2017 at 23:14
  • have you tried just creating two node read streams and piping them to a single write stream? – cheesenthusiast Commented Jun 19, 2017 at 23:14
  • @cheesenthusiast see the edit, hopefully more clear – DCtheTall Commented Jun 19, 2017 at 23:53
Add a ment  | 

2 Answers 2

Reset to default 4

This snippet can merge two excel files into a new one.

 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.

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:

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./package/xlsx#streaming-read

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742321342a4421872.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信