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 Java applications. Along with other basic as well as advanced Excel automation features, Aspose.Cells supports the protection of 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 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.

// Open the Excel file
Workbook workbook = new Workbook("workbook.xlsx");
// Protect workbook by specifying protection type
workbook.protect(ProtectionType.ALL, "12345");
// Save the file
workbook.save("workbook_protected.xlsx");

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.

// Open the Excel file
Workbook workbook = new Workbook("workbook_protected.xlsx");
// Unprotect workbook
workbook.unprotect("12345");
// Set password to null
workbook.getSettings().setPassword(null);
// Save the file
workbook.save("workbook_unprotected.xlsx");

Protect Specific Excel Worksheets in Java

You can also apply protection at the 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.

// Open the Excel file
Workbook workbook = new Workbook("workbook.xlsx");
// Accessing the first worksheet in the Excel file
WorksheetCollection worksheets = workbook.getWorksheets();
Worksheet worksheet = worksheets.get(0);
Protection protection = worksheet.getProtection();
// The following 3 methods are only for Excel 2000 and earlier formats
protection.setAllowEditingContent(false);
protection.setAllowEditingObject(false);
protection.setAllowEditingScenario(false);
// Protect the first worksheet with a password "1234"
protection.setPassword("1234");
// Save the file
workbook.save("Excel.xlsx");

Unprotect Worksheets in Java

The procedure of unprotecting a worksheet is the 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.

// Open the Excel file
Workbook workbook = new Workbook("workbook.xlsx");
// Accessing the first worksheet in the Excel file
WorksheetCollection worksheets = workbook.getWorksheets();
Worksheet worksheet = worksheets.get(0);
// Unprotect worksheet
worksheet.unprotect("12345");
// Save the file
workbook.save("workbook_updated.xlsx");

Protect Excel Files Online

Use our free tool to protect Excel files, which is a web-based tool that can be accessed from your browser without creating any account.

Free Java Excel Library

You can get a free temporary license to use our Java Excel API without evaluation limitations.

Conclusion

In this article, you have learned how to protect and unprotect Excel files in Java. Furthermore, you have seen how to protect and unprotect a particular worksheet in an Excel workbook. In addition, we have provided you with a free online tool to protect Excel files, which is based on Aspose.Cells for Java.

You can explore more about the Java Excel automation API using documentation.

See Also