java代碼實現對excel加密、解密(設置或去除打開密碼)


本demo使用jxcell組件來完成對excel加密、解密的功能。

  jxcell.jar已破解,下載方式為關注下面二維碼,回復jxcell810即可。

Java碎碎念公眾號

具體代碼如下:

import java.io.IOException;

import com.jxcell.CellException;
import com.jxcell.View;

/**
 * excel加密、解密 代碼
 * 
 * @author lifq
 * @date 2015-3-13 下午02:13:24
 */
public class EncryptDecryptUtil {

    /**
     * 讀取excel,並進行加密
     * 
     * @param url
     *            excel文件路徑 例:D:\\word.xls
     * @param pwd
     *            加密密碼
     */
    public static void encrypt(String url, String pwd) {
        View m_view = new View();
        try {
            // read excel
            m_view.read(url);
            // set the workbook open password
            m_view.write(url, pwd);
        } catch (CellException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * excel 解密
     * 
     * @return void
     * @author lifq
     * @date 2015-3-13 下午02:15:49
     */
    public static void decrypt(String url, String pwd) {
        View m_view = new View();
        try {
            // read the encrypted excel file
            m_view.read(url, pwd);

            // write without password protected
            m_view.write(url);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public static void main(String args[]) {
        // 下面1與2 兩個方法請分開執行,可以看到效果
        //
        //1. 把g:\\test.xls 添加打開密碼123
        EncryptDecryptUtil.encrypt("g:\\test.xls", "123");
        //2. 把g:\\test.xls 密碼123 去除
        EncryptDecryptUtil.decrypt("g:\\test.xls", "123");

    }
}

加密后效果圖:(打開時提示輸入密碼123)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM