使用Apache POI打开受密码保护的Word / Excel文件

时间:2012-11-10 02:42:36

标签: java ms-office apache-poi

  

可能重复:
  How can I access password protected Excel workbook in Java using POI api

如何使用Java中的Apache POI打开受密码保护的Word / Excel文件? 请写下代码。

1 个答案:

答案 0 :(得分:1)

Apache POI支持读取加密的XLSX和DOCX文件。请参阅Apache POI documentation

您的代码应该是这样的:

 EncryptionInfo info = new EncryptionInfo(filesystem);
    Decryptor d = Decryptor.getInstance(info);

    try {
        if (!d.verifyPassword(password)) {
            throw new RuntimeException("Unable to process: document is encrypted");
        }

        InputStream dataStream = d.getDataStream(filesystem);
        HSSFWorkbook wb = new HSSFWorkbook(dataStream);
        // parse dataStream

    } catch (GeneralSecurityException ex) {
        throw new RuntimeException("Unable to process encrypted document", ex);
    }