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
- Protect Excel Files using Java
- Unprotect Excel Files using Java
- Protect Worksheets using Java
- Unprotect Worksheets using Java
- Protect Excel Files Online
- Get Free License
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.
- Load Excel file using Workbook class.
- Use Workbook.protect(ProtectionType, String) method to protect the Excel file.
- Save the protected file using Workbook.save(String) method.
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.
- Load Excel file using Workbook class.
- Use Workbook.unprotect(String) method to unprotect the Excel file.
- Save the unprotected Excel file using Workbook.save(String) method.
The following code sample shows how to unprotect an Excel file in Java.
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.
- Load Excel file using Workbook class.
- Get the specific worksheet in a Worksheet object using Workbook.getWorksheets().get(int) method.
- Get the worksheet protection settings into the Protection object using Worksheet.getProtection() method.
- Set password using Protection.setPassword(String) method.
- Save the Excel file using Workbook.save(String) method.
The following code sample shows how to protect an Excel worksheet using Java.
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.
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.