
Encrypt Excel Files in Java
File encryption and decryption are common operations in any business. Often you need to prevent unauthorized access to important Excel files. Excel files are widely used for data storage because of their security features and rich data representation. You can add password protection to Excel files. In this post, you will learn how to decrypt or encrypt Excel files in Java using this Java Excel library. By the end, you will be able to set password security on Excel files programmatically.
The article covers these topics:
- Java Excel library Installation
- Encrypt Excel XLSX Files in Java
- Decrypt Excel XLSX Files Programmatically in Java
Java Excel library Installation
The installation process is easy and straightforward. You can also visit the installation guide for troubleshooting.
You can quickly install by either downloading the JAR files or adding the Maven configuration below:
<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>22.6</version>
</dependency>
Encrypt Excel XLSX Files in Java
After installing the library, use the following steps to encrypt an Excel file programmatically.
- Create a Workbook object and load the Excel file.
- Call the setPassword method to add a password.
- Set XOR encryption by using the setEncryptionOptions method.
- Choose a strong encryption type (RC4, Microsoft Strong Cryptographic Provider).
- Call the save method to write the encrypted file.
The result is shown below:

Decrypt Excel XLSX Files Programmatically in Java
You can decrypt Excel files with this Java Excel library and automate the process with a few lines of code.
Follow these steps:
- Create a LoadOptions object to specify loading options.
- Use loadOptions.setPassword to provide the workbook password.
- Instantiate a Workbook with the XLSX file and the load options.
- Call setPassword with a null value to remove the password.
- Save the file using the save method.
Get a Free License
You may obtain a free temporary license to try the API beyond evaluation limits.
Summing up
In this post, you learned how to decrypt or encrypt Excel files in Java programmatically. You can automate encryption and decryption by installing this Java Excel library. Visit the documentation for more features. Stay connected with conholdate.com for regular updates.
Ask a question
You can share your questions or queries on our forum.
FAQs
How do I password-protect an Excel file in Java?
Install this Java Excel API and use the provided methods to set a password on your Excel files programmatically.
How do I encrypt an XLSX file?
Call the setPassword method to encrypt the file. Explore the setEncryptionOptions class for additional encryption settings.
How do you decrypt a file in Java?
Use the loadOptions.setPassword method to open the encrypted file, then set the password to null to remove protection. See the full method list here.