How to Encrypt Excel File with Password in Java

This brief article describes how to encrypt Excel file with password in Java. It contains the configuration details for the development environment, a sequence of steps to be followed for writing the program, and finally, a runnable sample code that can be used to encrypt Workbook with password in Java. This article will also share the types of encryption that can be used and then save the resultant encrypted file in the desired format like XLSX, XLS, etc.

Steps to Encrypt Excel Spreadsheet in Java

  1. Configure the IDE to use Aspose.Cells for Java for encrypting the Excel file
  2. Load the template Excel file into the Workbook object that is to be encrypted
  3. Select the type of encryption and set using the setEncryptionOptions() method
  4. Set the password using the Settings object in the Workbook class
  5. Save the output encrypted workbook on the disk or to a stream

These steps help us to understand the process to encrypt Excel file in Java. The process is quite simple as we just need to load the source Excel file and then use the setEncryptionOptions() method along with the type of encryption and length of the key. Once the encryption type is set, we have to set the password that will be used to open the Excel file programmatically or using MS Excel.

Code to Password Protect Excel File in Java

The above code is written to demonstrate the process to password protect Excel document in Java. It uses the Workbook class object to load the source Excel file that has a lot of constructors for customization of the loading process. The Workbook class has also methods and properties that are used to set the type of encryption like STRONG_CRYPTOGRAPHIC_PROVIDER, XOR, COMPATIBLE, ENHANCED_CRYPTOGRAPHIC_PROVIDER_V_1, and the password.

This article has enlightened us to encrypt an Excel file in Java. If you want to learn the process of digitally signing an Excel file, refer to the article on how to digitally sign an Excel Document using Java.

 English