使用Apache poi将数据写入Word文档?

时间:2016-06-29 16:24:43

标签: java apache-poi aes fileoutputstream xwpf

我正在实施AES算法128位密钥。加密后,加密数据的前16个字节将存储在.docx文件中。之后,.docx文件将被阻止。

XWPFDocument document = new XWPFDocument() ;
FileOutputStream out = new FileOutputStream(filename,true);//filename is .docx word document
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText(ress1);//ress1 is a String datatype
document.write(out);

1 个答案:

答案 0 :(得分:1)

根据我对您的评论的理解,您希望加密您的word文件。您可以使用以下代码段实现此目的:

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

Encryptor enc = info.getEncryptor();
enc.confirmPassword(<your_password>);


OPCPackage opc = OPCPackage.open(new File(<file_path>), PackageAccess.READ_WRITE); //opening package for encryption 
OutputStream os = enc.getDataStream(fs); //perform encryption 
opc.save(os); //save package
opc.close();

FileOutputStream fos = new FileOutputStream("file_path"); 
fs.writeFilesystem(fos); //write the file back to file system
fos.close();