0

When attempting to create an Excel spreadsheet in node.js, why does the following code not work?

var Excel = require("xlsx");
workbook = new Excel.Workbook();
Excel.writeFile(workbook, 'out.xlsx');

I get the error:

workbook = new Excel.Workbook();
           ^
TypeError: undefined is not a function

How should I be doing this differently? I'm stumped.

Dirk
  • 3,073
  • 4
  • 31
  • 36

1 Answers1

1

It doesn't appear Workbook is an exported method of "xlsx". In fact, the whole library doesn't appear to support creating a file from scratch. I did find this posting that could be quite helpful to you: How to create an Excel File with Nodejs?

Taking from that, I'd recommend first trying the second answer, installing the node package "msexcel-builder" which looks like it's does what the accepted answer does but with less file write streaming work on your part.

Alternatively, using that posting's accepted answer, you could modify it a bit to:

 var fs = require('fs');
 var writeStream = fs.createWriteStream("file.xls");
 writeStream.close();

 var Excel = require("xlsx");
 var workbook = Excel.readFile('file.xls');

 Excel.writeFile(workbook, 'out.xlsx');
Community
  • 1
  • 1
Steve Hynding
  • 1,799
  • 1
  • 12
  • 22