问题
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