Protect and Unprotect Excel Files in Java

Protect Excel Files in Java

Excel files are used to store small as well as large-sized data in the form of rows and columns. Along with data storage, you can perform other operations such as computations and data analysis using graphs and charts. Since data is worth being protected, MS Excel allows you to protect the Excel files. This article covers how to automate the protection of MS Excel files programmatically. Particularly, you will learn how to protect and unprotect Excel files using Java.

Java API to Protect Excel Files

Aspose.Cells for Java is a well-known spreadsheet manipulation API that is designed to create, edit, and convert Excel files from within the Java applications. Along with other basic as well as advanced Excel automation features, Aspose.Cells supports the protection of the Excel files. You can either download API’s JAR or install it using the following Maven configuration.

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>https://repository.aspose.com/repo/</url>
</repository>
<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-cells</artifactId>
    <version>20.12</version>
</dependency>

Protect Excel Files using Java

Aspose.Cells for Java provides the following protection types in order to protect the Excel workbooks.

  • ALL – User cannot modify anything
  • CONTENTS – User cannot enter data
  • OBJECTS – User cannot modify drawing objects
  • SCENARIOS – User cannot modify saved scenarios
  • STRUCTURE – User cannot modify saved structure
  • WINDOWS – User cannot modify saved windows
  • NONE – No protection

The following are the steps to protect an Excel file using Aspose.Cells for Java.

The following code sample shows how to protect an Excel file in Java.

Unprotect Excel Files using Java

The following are the steps to unprotect a password-protected Excel file using Aspose.Cells for Java.

The following code sample shows how to unprotect an Excel file in Java.

Protect Excel Worksheets using Java

You can also apply protection at worksheet level. The following are the steps to protect an Excel worksheet using Aspose.Cells for Java.

The following code sample shows how to protect an Excel worksheet using Java.

Unprotect Worksheets using Java

The procedure of unprotecting a worksheet is same as protecting one. The only difference is, you will use the Worksheet.unprotect(String) method. The following code sample shows how to unprotect an Excel worksheet using Java.

Get a Free License

You can get a free temporary license in order to try the APIs without evaluation limitations.

Conclusion

In this article, you have learned how to protect and unprotect Excel files using Java. Furthermore, you have seen how to protect and unprotect a particular worksheet in an Excel workbook. You can explore more about the Java Excel automation API using documentation.

See Also