MS Excel provides you with a wide range of features to keep and analyze the data seamlessly. You can perform computations, generate charts as well as define your custom logic using VBA modules. Knowing the importance of the data in spreadsheets, MS Excel allows you to protect the workbooks using encryption. Furthermore, you can password protect the Excel files to avoid unauthorized access. In accordance with that, this article covers how to encrypt or decrypt Excel files using Java.
- Java API to Encrypt and Decrypt Excel Files
- Encrypt Excel Files using Java
- Decrypt Excel Files using Java
- Verify Password of Encrypted Excel File
- Encrypt Excel Files Online
- Get Free API License
Java API to Encrypt and Decrypt Excel Files
Aspose.Cells for Java is a powerful spreadsheet manipulation API that lets you create and manipulate Excel files seamlessly. Furthermore, it allows you to encrypt and decrypt the workbooks within a few lines of code. In addition, you can protect an Excel file with a password. Aspose.Cells for Java can either be downloaded as JAR or installed using the following Maven configurations.
<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>
Encrypt Excel Files using Java
Aspose.Cells for Java provides the following encryption types for Excel 2003 files.
- XOR
- COMPATIBLE (Office 97/2000 compatible)
- ENHANCED CRYPTOGRAPHIC PROVIDER V1
- STRONG CRYPTOGRAPHIC PROVIDER
However, 2007/2010 workbooks are encrypted using SHA and AES encryption techniques in the same way MS Excel does. The following are the steps to encrypt an Excel file using Aspose.Cells for Java.
- Load the Excel file using Workbook class.
- Set password using Workbook.getSettings().setPassword(string) method.
- Encrypt Excel file using Workbook.setEncryptionOptions(EncryptionType, int) method.
- Save the encrypted workbook using Workbook.save(string) method.
For demonstration, the following code sample shows how to encrypt an Excel file using Java.
Decrypt Excel Files using Java
In order to decrypt an encrypted Excel file, you only need to provide its password and the rest will be handled by Aspose.Cells for Java. The following are the steps to decrypt an Excel file using Java.
- Create an instance of LoadOptions class.
- Specify the password using LoadOptions.setPassword(string) method.
- Create an instance of the Workbook class and pass the file’s path and LoadOptions object to its constructor.
- Set password to null using Workbook.getSettings().setPassword(string) method.
- Once done, save the decrypted workbook using Workbook.save(string) method.
The following code sample shows how to decrypt a password-protected Excel file using Java.
Verify Password of an Encrypted Excel File
In addition to encryption/decryption, Aspose.Cells for Java also allows you to verify the password of a protected Excel file. The following are the steps to perform password verification.
- Load the encrypted Excel file using FileInputStream class.
- Use FileFormatUtil.verifyPassword(FileInputStream, string) method to verify the password.
- As a result, if the return value is true then the password is valid else it is invalid.
The following code sample shows how to verify the password of an encrypted Excel file using Java.
Encrypt Excel Files Online
Encrypt your Excel files using our free spreadsheet encryption tool. It is a web-based tool that can be accessed from your browser without creating any account.
Get a Free API License
Get a free temporary license and encrypt your Excel files without any evaluation limitations.
Conclusion
In this article, you have learned how to encrypt or decrypt Excel files using Java. Furthermore, you have seen how to verify the password of an encrypted Excel file. In addition, we have provided you with a free online tool to encrypt Excel files.
In case you want to explore more about the Java spreadsheet manipulation API, visit the documentation.