Encrypt Excel Files in Java

Encrypt Excel Files in Java

File encryption and decryption are very common operations in any business organization. In many scenarios, you do not want unauthorized access to your files. Excel files are mostly used to save important data due to their security and rich data representation features. However, you can make your Excel files password-protected. In this blog post, you will learn how to decrypt or encrypt Excel Files in Java using this Java Excel library. By the end of this blog post, you will be able to set password security on your Excel files programmatically.

The following topics shall be covered in this article:

Java Excel library Installation

The installation process is quite easy and straightforward. In addition, you may visit the installation guide in case of any issues.

However, you can quickly install by either downloading the JAR files or following the Maven configurations mentioned 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

Once the installation is successful, we can write the code snippet to encrypt the Excel file programmatically in a Java application.

You may follow the steps mentioned below:

  1. Instantiate a Workbook object and load an Excel file path.
  2. Password-protect the file by calling the setPassword method.
  3. Specify XOR encryption type by setting values of the setEncryptionOptions method.
  4. Specify strong Encryption type (RC4, Microsoft Strong Cryptographic Provider).
  5. Invoke the save method to save the Excel file.
// Instantiate a Workbook object and load an Excel file path
Workbook workbook = new Workbook("sample.xlsx");
// Password protect the file by calling the setPassword method.
workbook.getSettings().setPassword("1234");
// Specify XOR encrption type by setting values of the setEncryptionOptions method.
workbook.setEncryptionOptions(EncryptionType.XOR, 40);
// Specify Strong Encryption type (RC4,Microsoft Strong Cryptographic Provider).
workbook.setEncryptionOptions(EncryptionType.STRONG_CRYPTOGRAPHIC_PROVIDER, 128);
// Invoke the save to save the Excel file.
workbook.save( "EncryptingFiles_out.xls");

You can see the output in the image below:

Encrypt Excel file

Decrypt Excel XLSX Files Programmatically in Java

You can decrypt the Excel files using this Java Excel Library. Moreover, you can automate the whole process by writing a few lines of source code in Java.

Please follow the following steps and the code snippet:

  1. Create an object of LoadOptions class that represents the options of loading the file.
  2. Invoke the loadOptions.setPassword method to set the password of the workbook.
  3. Instantiate an instance of the Workbook class and load the XLSX file with options.
  4. setPassword method will remove the password by setting the password value null.
  5. Save the file by calling the save method.
// Create an object of LoadOptions class that represents the options of loading the file.
LoadOptions loadOptions = new LoadOptions();
// Invoke the setPassword method to set the password of the workbook.
loadOptions.setPassword("1234");
// Instantiate an instance of the Workbook class and load the xlsx file with options
Workbook workbook = new Workbook("sample.xlsx", loadOptions);
// Remove the password by setting the password value null.
workbook.getSettings().setPassword(null);
// Save the file by calling the save method.
workbook.save("Book1.xlsx");

Get a Free License

You may get a free temporary license to try the API beyond evaluation limitations.

Summing up

In this blog post, you have learned how to decrypt or encrypt Excel Files in Java programmatically. You can automate the file encryption and decryption processes by installing this Java Excel library. Moreover, you can visit documentation to know other features. Finally, stay connected with conholdate.com for regular updates.

Ask a question

You can let us know about your questions or queries on our forum.

FAQs

How do I password-protect an Excel File in Java?

You may install this Java Excel API to set a password on your business Excel files programmatically.

How do I Encrypt an XLSX File?

Invoke this setPassword method to encrypt an Excel file. In addition, also explore this setEncryptionOptions class to learn about encryption options.

How do you decrypt a file in Java?

You can call loadOptions.setPassword method to access the encrypted file and remove the password by setting the password value to null. Further, feel free to visit the list of available methods here.

See Also