Password Protected Excel Download using EPPLUS

不羁的心 提交于 2019-11-29 21:06:40

问题


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();
}

回答1:


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);



回答2:


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




回答3:


If you are saving the excel package into a MemoryStream (for sending as an email attachment) you have to do this:

excelPackage.SaveAs(memoryStream, "pa$$w0rd");


来源:https://stackoverflow.com/questions/35285802/password-protected-excel-download-using-epplus

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