Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache POI XWPFDocument object serialization

I'm using Apache POI methods to create and fill XWPFDocument object in my project, smth like this

public XWPFDocument test() {
 XWPFDocument doc = new XWPFDocument();
 ...

 return doc;
}

but thre's a problem, for my case XWPFDocument should be serialized. Is there any way to serialize it?

like image 884
Amewarashi Avatar asked Oct 29 '25 05:10

Amewarashi


1 Answers

Promoting a comment to an answer...

The way to serialise a XWPFDocument (or in fact any POI UserModel document) is via the write(OutputStream) method

If you need to serialise to a byte array, you'd do something like:

XWPFDocument doc = new XWPFDocument();
...

ByteArrayOutputStream baos = new ByteArrayOutputStream();
doc.write(baos);
return baos.toByteArray();

Assuming you want to serialise into something like a database or persistence framework, just get the OutputStream from that and write into it directly!

like image 128
Gagravarr Avatar answered Oct 31 '25 11:10

Gagravarr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!