excel 加密 与 去除密码

package com.scoket.service.excel;

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

import java.io.IOException;

/**
 * Created by kongweichun on 2019/1/29.
 * chongqing liangyao
 */
public class EncryptDecryptUtil {

    public static void main(String args[]) {
        //加密excel文件
        EncryptDecryptUtil.encrypt("F:\\acb.xls", "123");
        //去除excel文件密码
        EncryptDecryptUtil.decrypt("F:\\acb.xls", "123");

    }

    /**
     * 对excel进行加密
     * @param url excel文件路径
     * @param pwd 设置excel打开密码
     */
    public static void encrypt(String url, String pwd) {
        View m_view = new View();
        try {
            // 读取excel
            m_view.read(url);
            // 设置excel打开密码
            m_view.write(url, pwd);
        } catch (CellException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * excel 去除密码 执行此方法excel将没有密码
     * @param url 需要解密的excel文件路径
     * @param pwd excel密码
     */
    public static void decrypt(String url, String pwd) {
        View m_view = new View();
        try {
            //读取需要解密的excel文件
            m_view.read(url, pwd);
            //去除excel密码
            m_view.write(url);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }


}

 jxcell架包下载:https://files.cnblogs.com/files/codigup/jxcell.zip

posted @ 2019-01-29 10:53  铅笔coding  阅读(1472)  评论(1编辑  收藏  举报