Encrypt and Protect your workbook
EPPlus supports both setting protection and reading and writing encrypted packages.
Protection¶
Protection can be set both on the workbook and the worksheet level.
This example locks the window and the structure and sets a password.
Note that these settings don't encrypt the workbook.
To do that you can set the IsEncrypted property:
Worksheets can also be protected by using the worksheets Protection property.
Here is an example of how you can lock cells in a worksheet:
sheet.Protection.AllowSelectLockedCells = false;
sheet.Protection.SetPassword("EPPlus");
sheet.Cells["A:XFD"].Style.Locked = true; //Set Locked to true on all cells (this is the default state, so this is not needed on a newly add worksheet).
sheet.Cells["B3,C7,C9,C11"].Style.Locked = false; //Unlock these cells
sheet.Protection.AllowSelectLockedCells = False
sheet.Protection.SetPassword("EPPlus")
sheet.Cells("A:XFD").Style.Locked = True 'Set Locked to true on all cells (this is the default state, so this is not needed on a newly add worksheet).
sheet.Cells("B3,C7,C9,C11").Style.Locked = False 'Unlock these cells
Encryption¶
To encrypt a workbook simply save the workbook with your password using the Save or SaveAs methods.
Using the SaveAs method:
Using the Save method:
To read an encrypted package you should supply the password in the constructor:
See also¶
Encryption and protection of your workbook is shown in Sample 8.1-C# or Sample 8.1-VB