Skip to main content

Remove and Reset Excel Passwords in Java

 Excel files are commonly used in backend systems for reporting, data exchange, and batch processing. In many projects, they act as structured data containers moving between services, departments, or external systems.

However, once password protection is applied, automated workflows can easily break.

You may run into situations like:

  • A scheduled job fails because the uploaded file requires an open password

  • The file loads successfully, but certain worksheets cannot be modified

  • A data import module crashes due to unexpected encryption

These issues are not rare in Java backend development. Especially in reporting systems, batch processors, or import/export modules, improper handling of password-protected Excel files can directly affect system stability.

In this article, we’ll walk through how to handle Excel password operations in Java, including:

  • Detecting whether an Excel file is password protected

  • Removing a workbook open password

  • Resetting an existing password

  • Unprotecting a specific worksheet

Remove and Reset Excel Passwords in Java

Understanding Excel Password Protection

Before writing any code, it’s important to understand what type of protection you are dealing with.

Excel provides two main protection mechanisms.

1. Workbook Open Password

This is a file-level password.
If the correct password is not provided, the file cannot be opened or read at all.

From a backend perspective, this means your program cannot even load the workbook object.

2. Worksheet Protection

In this case, the file itself can be opened normally. However, specific worksheets are protected.

Users (or programs) cannot edit cell values, insert rows, delete columns, or modify formatting unless the correct sheet password is supplied.

These two protection mechanisms are independent. A file may have one, both, or neither.

Why Not Modify the .xlsx File Directly?

Technically, .xlsx files follow the Office Open XML (OOXML) specification.
An .xlsx file is essentially a ZIP archive containing multiple XML files that describe:

  • Workbook structure

  • Cell data

  • Styles

  • Security settings

In theory, you could unzip the file and manually edit the XML nodes related to password protection.

In practice, this approach is rarely suitable for production systems:

  • The OOXML structure is complex and version-sensitive

  • A small mistake in XML can corrupt the entire file

  • It is not suitable for automated or batch processing

  • Long-term maintenance becomes difficult

For experimental purposes, manual modification might work. But in backend systems—especially when reliability and maintainability matter—it’s not a practical solution.

In Java projects, it’s far more reasonable to use a dedicated Excel API that already handles encryption and internal structure parsing.

Spire.XLS for Java provides built-in methods to detect, remove, and reset Excel passwords without touching the underlying XML. This allows developers to focus on business logic instead of low-level file manipulation.

Integrating Spire.XLS into a Java Project

Before implementing password operations, you need to add the dependency.

Maven Configuration

Add the following code to your pom.xml file:

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.xls</artifactId>
        <version>16.1.3</version>
    </dependency>
</dependencies>

After updating, refresh your Maven dependencies.

If you’re not using Maven, download the JAR package from the library’s official website and manually add it to your project.

Example 1: Detect and Remove an Excel Open Password in Java

In backend systems, it’s better to check whether a file is password protected before attempting to load it. This avoids unnecessary exceptions and makes your processing logic clearer.

import com.spire.xls.*;

public class RemoveExcelPassword {
    public static void main(String[] args) {

        String filePath = "C:\\Users\\Administrator\\Desktop\\Encrypted.xlsx";

        Workbook wb = new Workbook();

        // Check whether the file requires an open password
        boolean isProtected = Workbook.isPasswordProtected(filePath);

        if (isProtected) {

            // Provide the original password
            wb.setOpenPassword("oldpassword");

            // Load the encrypted file
            wb.loadFromFile(filePath);

            // Remove file-level protection
            wb.unProtect();

            // Save as an unprotected file
            wb.saveToFile("C:\\Users\\Administrator\\Desktop\\Unprotected.xlsx");

            System.out.println("Password removed successfully.");

        } else {
            System.out.println("The file does not have an open password.");
        }

        wb.dispose();
    }
}

Explanation

  • Workbook.isPasswordProtected() checks whether the file requires a password

  • setOpenPassword() must be called before loading an encrypted file

  • unProtect() removes workbook-level protection

  • Saving without setting a password creates an unprotected file

This approach works well in batch-processing scenarios where uploaded files may or may not be encrypted.

Example 2: Reset an Excel File Password in Java

In some cases, you don’t want to remove the password entirely. You just need to replace it with a new one.

This is common in environments that require password rotation.

import com.spire.xls.Workbook;

public class ResetExcelPassword {
    public static void main(String[] args) {

        Workbook wb = new Workbook();

        // Set the old password
        wb.setPassword("oldpassword");

        // Load the encrypted Excel file
        wb.loadFromFile("C:\\Users\\Administrator\\Desktop\\Encrypted.xlsx");

        // Set a new password
        wb.setPassword("newpassword");

        // Save the file with the new password
        wb.saveToFile("C:\\Users\\Administrator\\Desktop\\Encrypted_with_new_password.xlsx");
    }
}

Implementation Logic

  1. Provide the existing password to load the file

  2. Set a new password

  3. Save the file

After saving, the file will require the new password to open.

Example 3: Remove Excel Worksheet Protection in Java

Sometimes the workbook itself is not encrypted, but certain sheets are locked.

In that case, you only need to remove worksheet-level protection.

import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;

public class RemoveSheetPassword {
    public static void main(String[] args) {

        Workbook wb = new Workbook();
        wb.loadFromFile("C:\\Users\\Administrator\\Desktop\\SheetProtected.xlsx");

        // Get the first worksheet
        Worksheet sheet = wb.getWorksheets().get(0);

        // Remove worksheet protection
        sheet.unprotect("sheetpassword");

        wb.saveToFile("C:\\Users\\Administrator\\Desktop\\SheetUnprotected.xlsx");

        System.out.println("Worksheet protection removed.");

        wb.dispose();
    }
}

Important Notes

  • If the workbook itself is encrypted, unlock it first

  • You must provide the correct worksheet password

  • Always save the file after modifying protection settings

Practical Considerations

When handling Excel password protection in Java backend systems, keep the following in mind:

  • You must know the original password to remove or reset it

  • Workbook-level and worksheet-level protections are independent

  • Always detect encryption before loading files in batch processes

  • Dispose of workbook resources properly to avoid memory leaks

Conclusion

Handling Excel password protection in Java is straightforward once you clearly distinguish between workbook-level and worksheet-level protection.

In this article, we covered:

  • The two main Excel password mechanisms

  • How to detect whether a file is password protected

  • How to remove an open password

  • How to reset an existing password

  • How to unprotect a specific worksheet

If your backend service processes user-uploaded Excel files or generates secured reports, proper password handling is essential for maintaining both system stability and data security.

Comments

Popular posts from this blog

3 Ways to Generate Word Documents from Templates in Java

A template is a document with pre-applied formatting like styles, tabs, line spacing and so on. You can quickly generate a batch of documents with the same structure based on the template. In this article, I am going to show you the different ways to generate Word documents from templates programmatically in Java using Free Spire.Doc for Java library. Prerequisite First of all, you need to add needed dependencies for including Free Spire.Doc for Java into your Java project. There are two ways to do that. If you use maven, you need to add the following code to your project’s pom.xml file. <repositories>               <repository>                   <id>com.e-iceblue</id>                   <name>e-iceblue</name>...

Insert and Extract OLE objects in Word in Java

You can use OLE (Object Linking and Embedding) to include content from other programs, such as another Word document, an Excel or PowerPoint document to an existing Word document. This article demonstrates how to insert and extract embedded OLE objects in a Word document in Java by using Free Spire.Doc for Java API.   Add dependencies First of all, you need to add needed dependencies for including Free Spire.Doc for Java into your Java project. There are two ways to do that. If you use maven, you need to add the following code to your project’s pom.xml file.     <repositories>               <repository>                   <id>com.e-iceblue</id>                   <name>e-iceblue</name>    ...

Simple Java Code to Convert Excel to PDF in Java

This article demonstrates a simple solution to convert an Excel file to PDF in Java by using free Excel API – Free Spire.XLS for Java . The following examples illustrate two possibilities to convert Excel to PDF:      Convert the whole Excel file to PDF     Convert a particular Excel Worksheet to PDF Before start with coding, you need to Download Free Spire.XLS for Java package , unzip it and import Spire.Xls.jar file from the lib folder in your project as a denpendency. 1. Convert the whole Excel file to PDF Spire.XLS for Java provides saveToFile method in Workbook class that enables us to easily save a whole Excel file to PDF. import com.spire.xls.FileFormat; import com.spire.xls.Workbook; public class ExcelToPDF {     public static void main(String[] args){         //Create a Workbook         Workbook workbook = new Workbook();   ...