I need to export tables and generate reports from my application using pure java. Using COM or anything that needs pre-installed applications is not allowed. I definitely need .doc format. docx format is optional.
To export table means just to create simple table in word document with data. To generate report is to replace placeholders with some values in a template table. It also involves inserting new subtables in a template table or merging cells and rows.
So, the task is:
I’ve tried to search the net but I managed to find Apache POI and Aspose libraries to do the job. Aspose seems to be ok but unfortunately I can’t afford this. POI has very poor documentation and I can’t really figure out whether it suits or not.
Moreover I tried to insert simple table into the document. But it just corrups document. Check out the code below:
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("poi.doc"));
HWPFDocument doc = new HWPFDocument(fs);
Range range = doc.getRange();
Table table = range.insertTableBefore((short)5, 5);
table.getRow(0).getCell(0).replaceText("cell", true);
doc.write(new FileOutputStream("poi_out.doc"));
So i have three questions by that moment:
Thanks for your replies
P.S. I’ve read some posts on this site about that topic. None of them really helped me. Moreover all of them are old. Some new features probably changed since that time.
There are 2 ways you can do this:
Create a HTML file with the stuff you need. Use velocity as your templating engine. Now, after you replace the placeholders with values in your code, save the file with an extension of ‘.doc’ or ‘.docx’. The resultant file will open in word nicely. It will open in the web layout and the user can switch to the print layout if he likes.
http://velocity.apache.org/engine/releases/velocity-1.5/user-guide.html
http://www.docx4java.org/trac/docx4j
But for this you will have understand how the MS Office Word docs XML structure works. And it might be really complicated putting a table inside another table.