Use the Jxcell component to perform the functions of encrypting and decrypting Excel.
Jxcell.jar "Click to download" (This jar has no use limit, you know)
The specific code is as follows:
Importjava.io.IOException;Importcom.jxcell.CellException;ImportCom.jxcell.View;/*** Excel encryption, decryption code * *@authorLifq * @date 2015-3-13 pm 02:13:24*/ Public classEncryptdecryptutil {/*** Read Excel and Encrypt * *@paramURL * Excel file path Example: D:\\word.xls *@parampwd * Encryption password*/ Public Static voidEncrypt (string URL, string pwd) {View M_view=NewView (); Try { //Read Excelm_view.read (URL); //Set the workbook open passwordm_view.write (URL, pwd); } Catch(cellexception e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } } /*** Excel Decryption * *@returnvoid *@authorLifq * @date 2015-3-13 pm 02:15:49*/ Public Static voiddecrypt (string URL, string pwd) {View M_view=NewView (); Try { //read the encrypted Excel filem_view.read (URL, pwd); //write without password protectedm_view.write (URL); } Catch(Exception e) {System.out.println (E.getmessage ()); } } Public Static voidMain (String args[]) {//The following 1 and 22 methods should be performed separately, you can see the effect// //1. Add G:\\test.xls to open password 123Encryptdecryptutil.encrypt ("G:\\test.xls", "123"); //2. Remove the G:\\test.xls password 123Encryptdecryptutil.decrypt ("G:\\test.xls", "123"); }}
After encryption: (Prompt for password when open 123)
Java code implementation for Excel encryption, decryption (set or remove open password)