How to edit existing word file using POI API

2019-08-13 23:05发布

问题:

Using:

XWPFDocument document = new XWPFDocument();

How do I assign it so it doesn't overwrite the file if it already exists?

回答1:

The following will create a brand new, empty XWPF Word Document to work with:

XWPFDocument document = new XWPFDocument();

Whereas this will open up an existing one for editing:

XWPFDocument document = new XWPFDocument(OPCPackage.open("MyFile.docx"));

Or if you have an InputStream rather than a file:

XWPFDocument document = new XWPFDocument(inputStream);