Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Password Protected Excel Download using EPPLUS

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();
}
like image 896
preeti jain Avatar asked Feb 09 '16 07:02

preeti jain


People also ask

Does EPPlus work with XLS?

EPPlus does not work with the XLS format. Only XLSX. You'll need to find a new library.

How do I remove password protection from Xlsx?

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.

Does EPPlus require Excel?

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


1 Answers

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);
like image 167
Ernie S Avatar answered Sep 19 '22 22:09

Ernie S