title: Generating MS Word reports and export uing java
body: <p>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 <code>.doc</code> format. <code>docx</code> format is optional.</p>
  
  <p>To <em>export</em> table means just to create simple table in word document with data. To generate <em>report</em> 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.</p>
  
  <p>So, the task is:
  <li> <em>create tables in MS Word documents</em>
  <li> <em>insert subtables into table cells</em>
  <li> <em>merge table cells and rows</em>
  <li> <em>replace text in documents leaving the formatting</em></p>
  
  <p>I've tried to search the net but I managed to find <strong>Apache POI</strong> and <strong>Aspose</strong> 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.</p>
  
  <p>Moreover I tried to insert simple table into the document. But it just corrups document. Check out the code below:</p>
  
  <pre><code>    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"));
  </code></pre>
  
  <p>So i have three questions by that moment:</p>
  
  <p><li> <em>Am I doing something wrong in this example?</em>
  <li> <em>Is POI applicable for the tasks I mentioned?</em>
  <li> <em>What other <strong>free</strong> libraries you can suggest</em></p>
  
  <p>Thanks for your replies</p>
  
  <p><strong>P.S.</strong> 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.</p>