如何在Java中强制实施MS-Word的密码保护?

时间:2014-12-05 16:52:52

标签: java apache-poi docx4j

我确实看到了各种强制保护的选项,但没有密码。我该怎么做?

File file               = new File(fileName);
FileInputStream fis     = new FileInputStream(file.getAbsolutePath());
XWPFDocument document   = new XWPFDocument(fis);

document.enforceCommentsProtection();
document.enforceFillingFormsProtection();
document.enforceReadonlyProtection();
document.enforceTrackedChangesProtection();
document.enforceUpdateFields();

document.removeProtectionEnforcement();

1 个答案:

答案 0 :(得分:1)

您可以使用apache poi来提供密码保护 http://www.quicklyjava.com/create-password-protected-excel-using-apache-poi/

        POIFSFileSystem fs = new POIFSFileSystem();
        EncryptionInfo info = new EncryptionInfo(fs, EncryptionMode.agile);

        Encryptor enc = info.getEncryptor();
        enc.confirmPassword("xxxxx");

        OPCPackage opc = OPCPackage.open(new File("c:/test/sample.xlsx"),PackageAccess.READ_WRITE);
        OutputStream os = enc.getDataStream(fs);
        opc.save(os);
        opc.close();

        FileOutputStream fos = new FileOutputStream("c:/test/sample.xlsx");
        fs.writeFilesystem(fos);
        fos.close();