zoukankan      html  css  js  c++  java
  • POI 加密Excel文件

    今天遇到一个业务,需要为Excel 文件加密后发送邮件给客户,Excel文件需要使用密码才能打开。

    在网上查了一轮后发现官网有相应的例子可以参考,可以看官网资料(参考http://poi.apache.org/encryption.html)。

    下面的例子只能支持Excel 2007文件

            //Add password protection and encrypt the file
            final POIFSFileSystem fs = new POIFSFileSystem();
            final EncryptionInfo info = new EncryptionInfo(EncryptionMode.standard);
            //final EncryptionInfo info = new EncryptionInfo(EncryptionMode.agile, CipherAlgorithm.aes256, HashAlgorithm.sha256, -1, -1, null);
            final Encryptor enc = info.getEncryptor();
    
            //set the password
            enc.confirmPassword("abcdef");
    
            //encrypt the file
            final OPCPackage opc = OPCPackage.open(new File(path), PackageAccess.READ_WRITE);
            final OutputStream os = enc.getDataStream(fs);
            opc.save(os);
            opc.close();
    
            //save the file back to the filesystem
            final FileOutputStream fos = new FileOutputStream(path);
            fs.writeFilesystem(fos);
            fos.close();
    

      这里可选的EncryptionMode有多种,本人简单测试,只有EncryptionMode.standard、EncryptionMode.binaryRC4可以不需要其它参数就加密成功。

    用EncryptionMode.agile 加密则打开Excel 文件还是有问题(可以不输入密码打开文件,文件无内容)。而官网写着binaryRC4是不安全。需要继续看文档或者研究才能断续。

  • 相关阅读:
    Windows如何使用jstack跟踪异常代码
    内存溢出和内存泄漏的区别
    Tomcat 服务器性能优化
    Spotlight on oracle
    JVM性能调优监控工具jps、jstack、jmap、jhat、jstat使用详解(转VIII)
    linux下使用yum安装mysql
    MySQL监控系统MySQL MTOP的搭建(转VIII)
    SQL中Group By的使用
    视图
    触发器
  • 原文地址:https://www.cnblogs.com/latteyan/p/6165983.html
Copyright © 2011-2022 走看看