简体   繁体   English

使用Apache POI的密码保护.XLS Excel文件

[英]Password-Protect .XLS Excel files using Apache POI

I am using Apache POI 3.11, which is supposed to support password-protecting Excel files (both XLS and XLSX types). 我正在使用Apache POI 3.11,它应该支持密码保护的Excel文件(XLS和XLSX类型)。 I was easily able to get encrypting of the XLSX type to work properly using POI, but I have been unable to do this with the old XLS type. 使用POI可以轻松地加密XLSX类型以使其正常工作,但是我无法使用旧的XLS类型来做到这一点。 I have attempted this using the following code, but all it does is create a copy of the file without setting a password on it. 我已经尝试使用以下代码进行此操作,但它所做的只是创建文件的副本而未在其上设置密码。 As far as I can tell from the examples and API, this is how it is supposed to be done, but I cannot get it to work. 据我从示例和API可以看出,这是应该完成的方式,但我无法使其正常工作。

    HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(sourcePath));
    Biff8EncryptionKey.setCurrentUserPassword("potato");
    OutputStream os = new FileOutputStream(destinationPath);
    wb.write(os);
    os.flush();
    os.close();

Any suggestions would be appreciated. 任何建议,将不胜感激。 As a last resort, I can perhaps use a different library, but I would prefer to use POI since this is what we use for everything else that is Excel-related. 作为最后的选择,我也许可以使用其他库,但是我更喜欢使用POI,因为这是我们用于与Excel有关的其他所有内容的功能。

As per the Apache POI Encryption Supported Features Matrix , HSSF only supports decrypting .xls files protected with a known password. 根据Apache POI加密支持的功能矩阵 ,HSSF仅支持解密受已知密码保护的.xls文件。 It does not support encrypting them. 它不支持加密它们。 (XSSF can work with both encrypting and decrypting .xlsx files) (XSSF可以与加密和解密.xlsx文件一起使用)

Therefore, if you do want to support encrypting .xls files with Apache POI's HSSF, you'll need to get involved with the project and then work on submitting a patch for this missing piece of functionality! 因此,如果您确实想支持使用Apache POI的HSSF对.xls文件进行加密,则需要参与该项目 ,然后为该功能缺失提交补丁

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM