Is there any way to generate password protected XLSX in NodeJS?

扶醉桌前 提交于 2020-05-09 06:11:07

问题


I am looking for an npm package or any other workaround to be able to generate xlsx file with password. I do not want to protect the sheet(s) or cell(s)... I want the whole file password protected. I found few quite good packages (excel4node, exceljs..) but none of them able to do what I need.

Any suggestion appreciated


回答1:


You can use the xlsx-populate module to do this, for example:

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

XlsxPopulate.fromBlankAsync().then(workbook => {
    workbook.sheet("Sheet1").cell("A1").value("Some sample text");
    return workbook.toFileAsync("./test.xlsx", { password: "$secret_password" });
});

The resulting workbook will require the user to enter the correct password if they wish to access it.




回答2:


you can use exceljs package, it provides a way to create write protected excel, but does not provide password protected excel sheet.

await worksheet.protect('the-password', options);

reference: https://github.com/exceljs/exceljs#sheet-protection

But if you want to create a password protected excel sheet then you can use xlsx-populate package.

XLSX Agile encryption and decryption are supported so you can read and write password-protected workbooks. To read a protected workbook, pass the password in as an option:

XlsxPopulate.fromFileAsync("./Book1.xlsx", { password: "S3cret!" })
.then(workbook => {
    // ...
});

Similarly, to write a password encrypted workbook:

workbook.toFileAsync("./out.xlsx", { password: "S3cret!" });

reference: https://github.com/dtjohnson/xlsx-populate#encryption



来源:https://stackoverflow.com/questions/54250905/is-there-any-way-to-generate-password-protected-xlsx-in-nodejs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!