Skip to content

Instantly share code, notes, and snippets.

@conholdate-gists
Last active August 10, 2022 07:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save conholdate-gists/cb66543c79c30623969adfb102cd8cdf to your computer and use it in GitHub Desktop.
Save conholdate-gists/cb66543c79c30623969adfb102cd8cdf to your computer and use it in GitHub Desktop.
Encrypt Excel Files in Java
// 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");
// 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");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment