I am exporting data in Excel form using EPPLUS Excel Library. I want that when excel downloaded it will ask for password. I have tried following code.
FileInfo newFile = new FileInfo("sample.xlsx");
using (ExcelPackage package = new ExcelPackage(newFile)
{
ExcelWorksheet ws = package.Workbook.Worksheets.Add("demo");
ws.Cells[A1].LoadFromDataTable(dataTable, false);
package.Workbook.Protection.SetPassword("EPPLUS");
package.Save();
}
Just need to use the .Save
overload with a password as the option:
package.Save("password");
Response To Comments
To apply a password if saving via a byte
array it is very similar:
Byte[] bin = pck.GetAsByteArray("password");
System.IO.File.WriteAllBytes(fullFilePath, bin);
It's not documented, but you can do as following:
package.Encryption.Password = "your password here";
Then serve your package with Save()
or GetAsByteArray()
of your choice
如果您将 excel 包保存到 MemoryStream(作为电子邮件附件发送),您必须执行以下操作:
excelPackage.SaveAs(memoryStream, "pa$$w0rd");
package.GetAsByteArray("sometest"); ---> this will protect your excel sheet with password sometest :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.