2014-12-05 91 views
0

我会看到各种选项来强制执行保护,但没有任何密码。我会怎么做?如何在Java中强制执行MS-Word的密码保护?

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

您可以通过使用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();  
给密码保护