mma*_*mar 7 javascript node.js exceljs
我尝试创建 Excel,但出现提交错误。我的代码有什么问题。如果我删除sheet.commit()并且workbook.commit()程序工作正常。
const Excel = require(\'exceljs\');\nconst fs = require(\'fs\');\n\nconst workbook = new Excel.Workbook();\nconst sheet = workbook.addWorksheet("MySheet");\nconst writeToExcel = fs.createWriteStream("./test/testfile.xlsx");\n\nsheet.columns = [\n { header: \'Id\', key: \'id\', width: 10 },\n { header: \'Name\', key: \'name\', width: 40 },\n { header: \'DOB\', key: \'dob\', width: 10, outlineLevel: 1}\n];\n\n\nlet names = [\'Windows\', \'Mac Os\', \'Ubuntu\', \'B OS\'];\nlet i = 2;\n\nnames.forEach( (singleName) => {\n let row = sheet.getRow(i);\n row.values = {\n id: i-1,\n name: singleName,\n dob: new Date()\n };\n row.commit();\n i++;\n});\n\nsheet.commit();\nworkbook.commit();\n\nworkbook.xlsx.write(writeToExcel)\n .then( (response) => {\n console.log("Excel file is created with data.");\n })\n .catch( (error) => {\n console.log("Some problem in creating an excel file. Please check for errors...");\n });\nRun Code Online (Sandbox Code Playgroud)\n\n下面是错误。
\n\n\xce\xbb node excel.js\nc:\\Personal\\node_projects\\node-js-playlist-master\\excel.js:29\nsheet.commit();\n ^\n\nTypeError: sheet.commit is not a function\n at Object.<anonymous> (c:\\Personal\\node_projects\\node-js-playlist-master\\excel.js:29:7)\n at Module._compile (module.js:624:30)\nRun Code Online (Sandbox Code Playgroud)\n\n如果我删除工作表提交,则会在工作簿提交行上引发错误。任何帮助在这里都是有用的..
\n小智 4
不知道你是否找到了答案。
唯一具有提交功能的对象是行和 XLSX 流。
您已经提交了每一行
row.commit();
Run Code Online (Sandbox Code Playgroud)
表本身没有提交功能,而行有。您在示例中可能发现的是这样的:
sheet.addRow([...]).commit();
Run Code Online (Sandbox Code Playgroud)
但是,这不是在工作表上提交,而是在行上提交。
至于工作簿提交。您可能已经看过这个提交示例,但同样这是对 XLSX 流的提交,而不是工作簿本身。