2014-12-05 42 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();  
給密碼保護