Encrypt XLS file with password in Java

Hi There,

Can we set encrypt with password instead of protect workbook structure.

my coding something like this, this coding just set protect workbook structure.

                Workbook copy =new Workbook(fileName);
           
            String fileName2 = AppConstant.TEMP_FILE_LOCATION+fileNameStr+".xls";
            Workbook book = new Workbook();
            book.copy(copy);
          
            book.protect(ProtectionType.ALL, "mypassword");

Can you give me little example for that.

@sueprsand

Thanks for your query.

You can encrypt an XLS file by using following code snippet.

            // 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");

Hope, this helps a bit.

Hi ahsaniqbalsidiqui,

Thanks for your reply. It’s work for me.