Let’s learn to secure spreadsheet files from unauthorized access. Previously we discussed adding text and image watermarks to the Excel spreadsheets to avoid any illegal use. In this article, we will see how to password-protect an Excel file using C#. Additionally, we will change the existing password and also learn to remove the password to unlock Excel files.

Programmatically Protect Excel Files with Password - Lock Unlock

The following topics are discussed below:

.NET API to Lock and Unlock Excel Files

To lock and unlock documents, we will use GroupDocs.Merger for .NET. This API enables adding, changing, and removing password security features for the documents within the .NET applications. Along with protecting and unprotecting Excel spreadsheets, the API provides many more features like merging and splitting that are mentioned in the documentation.

You can download the DLLs or MSI installer from the downloads section or install the API in your .NET application via NuGet.

PM> Install-Package GroupDocs.Merger

Password Protect Excel Files using C# - Lock Spreadsheets

Lock Spreadsheet with Password

Let’s start by adding protection to the file by locking it with the password. The following steps show how to add password security to the Excel spreadsheets using C#.

  • Define the password using the AddPasswordOptions class.
  • Load the Excel file (XLS/XLSX) using the Merger class.
  • Lock the file by adding a password using the AddPassword method.
  • Save the protected spreadsheet using the Save method.

The following C# code adds the password to the Excel file for security.

Here is the output of the above code. When you try to open the spreadsheet file, the editor or viewer will ask for the password to prove your authority.

Enter Password to Protected File

Update Existing Password of Excel Files using C#

Is your password really exposed? Be more careful next time. Let’s quickly change it programmatically with the new and a bit difficult one. The following steps allow you to change the current password of your spreadsheet file within your .NET application in C#.

  • First, prepare the Loading Options using the current password.
  • Then, prepare the Update Password Options using the new password.
  • Then, load the XLS/XLSX file using the Merger class and the loading options.
  • Now, change the existing password using the UpdatePassword method.
  • Finally, save the locked file with the changed password using the Save method.

Here is the code snippet to change the current password of the spreadsheet file.

Remove Password to Unprotect Excel Spreadsheet Files using C# - Unlock XLS/XLSX

Spreadsheet unlocked - Removed Password

If your spreadsheets are now in safe hands, you can remove the password to access these easily. The following steps show how to unlock the Excel file by removing its password using C#.

  • Prepare the Loading Options using the file’s password.
  • Load the spreadsheet file using the Merger class and the load options.
  • Remove the existing password using the RemovePassword method.
  • Save the unlocked file using the Save method.

The following C# code snippet unlocks the Excel file by removing its existing password, hence anyone can access it without authorization.

Conclusion

Let’s conclude what we learned today. We started with the simple Excel spreadsheet (XLS/XLSX) file and added password protection. Then we changed the existing password of that protected spreadsheet. In the end, we learned how to remove the password of the protected file to unprotect the Excel spreadsheet. Now you can jump to build your own password protector and password remover application using the .NET API.

To learn more about GroupDocs.Merger for .NET, visit the documentation. For queries, contact us via the forum.

Get a Free API License

You can get a free temporary license to use the API without the evaluation limitations.

See Also