Let’s learn how to keep our spreadsheet files safe so only the right people can use them. Earlier, we talked about adding text and image watermarks to the Excel spreadsheets to stop anyone from using them in a bad way. Now, we’re going to see how to password protect an Excel file using Java. We’ll also find out how to change the existing password and finally, how to programmatically remove the password of protected spreadsheets to unprotect Excel files.

Programmatically Protect Excel Files with Password - Lock Unlock

The following topics are discussed below:

Java API to Lock and Unlock Excel Files

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

You can download the JAR file from the downloads section or use the latest repository and dependency Maven configurations within your Java applications.

	<name>GroupDocs Java API</name>

Password Protect Excel Files using Java - Lock Spreadsheets

Lock Spreadsheet with Password

Let’s add security to the Excel file by adding a password protection and lock spreadsheets. The following steps show how to password protect Excel spreadsheets using Java.

  1. Set up the password using the AddPasswordOptions class.
  2. Load the Excel file (XLS/XLSX) with the Merger class.
  3. Lock the file by adding a password using the addPassword method.
  4. Preserve the protected spreadsheet using the save method.

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

This is what you’ll see when you run the provided code. If you attempt to open the spreadsheet file, the editor or viewer will request the password from you as a way to confirm your authorization.

Enter Password to Protected File

Update Existing Password of Excel Files using Java

Has your password been accidentally revealed? Be more careful in the future. Let’s modify it, opting for a new and complex one. The following steps allow you to change the current password of your spreadsheet file within your Java application in Java.

  1. Start by configuring the LoadOptions with your current password.
  2. Then, prepare the Update Password Options using the new password.
  3. Load the XLS/XLSX file using the Merger class and the loading options.
  4. Now, change the existing password using the updatePassword method.
  5. Conclude by saving the file with the updated password via the save method.

Here’s the code snippet for changing the existing password of the spreadsheet file.

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

Spreadsheet unlocked - Removed Password

Once your spreadsheets are secure, you can effortlessly eliminate the password for easy access. The subsequent steps outline the process of unlocking an Excel file by eliminating its password using Java.

  1. Set up the LoadOptions using the existing password of the file.
  2. Load the spreadsheet file utilizing the Merger class along with the loading options.
  3. Erase the current password using the removePassword method.
  4. Save the unlocked spreadsheet file using the save method.

The Java code excerpt below demonstrates how to unlock an Excel file by eliminating its current password. This grants unrestricted access without requiring any authorization.


Let’s conclude what we learned today. We started with the simple Excel spreadsheet (XLS/XLSX) file and programmatically added password protection using small Java code. Then we changed the existing password of that protected spreadsheet. Lastly, 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 Java API.

To learn more about GroupDocs.Merger for Java, 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