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();
}
EPPlus does not work with the XLS format. Only XLSX. You'll need to find a new library.
Open the workbook that you want to change or remove the password for. On the Review tab, click Protect Sheet or Protect Workbook. Click Unprotect Sheet or Protect Workbook and enter the password. Clicking Unprotect Sheet automatically removes the password from the sheet.
No, it does not require Excel to be installed on the server, as you can read in the docs: EPPlus is a . NET library that reads and writes Excel files using the Office Open XML format (xlsx).
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With