java代码实现对excel加密、解密(设置或去除打开密码)

本文介绍如何使用jxcell组件实现Excel文件的加密与解密功能。通过提供的Java代码示例,可以为指定的Excel文件设置打开密码,也可以移除已设置的密码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用jxcell组件来完成对excel加密、解密的功能。

  jxcell.jar【点击下载】(此jar没有使用限制,你懂得)

具体代码如下:

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)

 

转载于:https://www.cnblogs.com/haha12/p/4335076.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值