Generating MS Word reports and export uing java

11,247

There are 2 ways you can do this:

  1. This I really recommend since this solves your problem of replacing template placeholders with actual values, easily:

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

  1. Look at docx4j.

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.

Share:
11,247
kant
Author by

kant

Updated on June 11, 2022

Comments

  • kant
    kant almost 2 years

    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:

  • create tables in MS Word documents
  • insert subtables into table cells
  • merge table cells and rows
  • replace text in documents leaving the formatting

    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:

  • Am I doing something wrong in this example?
  • Is POI applicable for the tasks I mentioned?
  • What other free libraries you can suggest

    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.

  • kant
    kant over 11 years
    Actually I'm afraid that i need binary doc files. is it possible?
  • kant
    kant over 11 years
    >Use velocity as your templating engine. I'm kind of noob. Can you explain this?
  • sethu
    sethu over 11 years
    There are no libraries that can help with you doc files for java. POI is very rudimentary for doc files. The library remains dormant for close to 5 years now. I dont see it being developed now with docx4j come in.