Detect if an Excel Document is Password Protected Using Java

In the previous article, I’ve introduced how to encrypt an Excel file using Free Spire.XLS for Java. Now this article will share how to detect whether an Excel document is password protected or not using the same free library.

Installation (2 Method)
1# Download the free library and unzip it, then add the Spire.Xls.jar file to your project as dependency.

2# You can also add the jar dependency to maven project by adding the following configurations to the pom.xml.

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

The Java Code Used

import com.spire.xls.Workbook;

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

//Get the file path
String filePath= "C:\\Users\\Administrator\\Desktop\\my file.xlsx";

//Detect whether the workbook is password protected or not
Boolean isProtected = Workbook.bookIsPasswordProtected(filePath);

//Print results
if (isProtected) {
System.out.print("The document is password protected.");
}
else {
System.out.print("The document is not protected.");
}
}
}

Sharing Java Code