How to create password protected Excel files in java?
Password protected Excel file can be created using
Apache POI is a Java library to work with Excel documents. We can create protected Excel file since Apache POI version 3.16
Encryption is format-dependent and needs to be implemented per format differently.
Creating Protected XLS file
Use setCurrentUserPassword(password) method of Biff8EncryptionKey class to specify the decryption password before saving or writing the workbook of the excel file.
Creating Protected XLSX file
Create EncryptionInfo class object.
Get the Encryptor object using getEncryptor() method.
Set the password using confirmPassword()
Open the xlsx file using OPCPackage.open() in READ_WRITE mode.
Get the output stream using encryptor and then save it.
Write the encrypted data to the file.