Encrypt and Decrypt Excel files
Using MS Excel
In MS Excel (e.g MS Excel 2003), to implement file encryption settings, you may try:
- From the Tools menu, select Options, and then select the Security tab.
- Input Password to open and click the Advanced button.
- Choose the encryption type and confirm the password.
Figure: Options dialog
Figure: Encryption Type dialog
Encrypting Excel file
The following example shows how you can encrypt / password protect an excel file using the Aspose.Cells API.
Sample Code:
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(EncryptingFiles.class) + "loading_saving/"; | |
// Instantiate a Workbook object by excel file path | |
Workbook workbook = new Workbook(dataDir + "Book1.xls"); | |
// Password protect the file. | |
workbook.getSettings().setPassword("1234"); | |
// Specify XOR encrption type. | |
workbook.setEncryptionOptions(EncryptionType.XOR, 40); | |
// Specify Strong Encryption type (RC4,Microsoft Strong Cryptographic | |
// Provider). | |
workbook.setEncryptionOptions(EncryptionType.STRONG_CRYPTOGRAPHIC_PROVIDER, 128); | |
// Save the excel file. | |
workbook.save(dataDir + "EncryptingFiles_out.xls"); | |
// Print message | |
System.out.println("Encryption applied successfully on output file."); |
Decrypting Excel file with Aspose.Cells
It is very to open password-protect excel file and decrypt using the Aspose.Cells API as following codes:
//Open encrypted file with password. | |
LoadOptions loadOptions = new LoadOptions(); | |
loadOptions.setPassword("password"); | |
Workbook workbook = new Workbook("Book1.xlsx", loadOptions); | |
//Remove password. | |
workbook.getSettings().setPassword(null); | |
//Save the file. | |
workbook.save("Book1.xlsx"); |