1

I have a scenario where i need to create a word document with Header , Footer with Table . And the Table data will come dynamically from the Front End. sometimes it may have to create more tables based on the input. can somebody help me. Thanks in Advance

JgSudhakar
  • 55
  • 3
  • 9

1 Answers1

3

There are multiple libraries doing this,Apache Poi is one of them.

Sample code

   public static void main(String[] args) throws IOException {
        XWPFDocument document = new XWPFDocument();

        XWPFTable tableOne = document.createTable();
        XWPFTableRow tableOneRowOne = tableOne.getRow(0);
        tableOneRowOne.getCell(0).setText("Header1");
        tableOneRowOne.addNewTableCell().setText("header2");
        XWPFTableRow tableOneRowTwo = tableOne.createRow();
        tableOneRowTwo.getCell(0).setText("Data1");
        tableOneRowTwo.getCell(1).setText("Data2");
        FileOutputStream outStream = new FileOutputStream("test.doc");
        document.write(outStream);
        outStream.close();
    }

Sample tutorial http://tkgospodinov.com/writing-microsoft-word-documents-in-java-with-apache-poi-part-2-creating-tables/

Pom.xml

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.9</version>
        </dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.9</version>
</dependency>
<dependency>

Imports

import java.io.FileOutputStream; import java.io.IOException;

import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFTable; import org.apache.poi.xwpf.usermodel.XWPFTableRow;

Hirak
  • 3,601
  • 1
  • 22
  • 33
  • Thanks for your quick response. I have tried this . But it is always giving the Exception as below Exception in thread "main" java.lang.NoSuchMethodError: org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl.getTrList()Ljava/util/List; at org.apache.poi.xwpf.usermodel.XWPFTable.(XWPFTable.java:119) at org.apache.poi.xwpf.usermodel.XWPFDocument.createTable(XWPFDocument.java:862) at CreateTablesWithPOI.main(CreateTablesWithPOI.java:11)" – JgSudhakar May 22 '14 at 09:30
  • I have added the below jar files poi-3.9.jar "xbean-2.3.0.jar xmlbeans-2.5.0.jar poi-ooxml-3.8.jar poi-ooxml-schemas-3.6-20100309.jar poi-ooxml-schemas-3.7-beta1.jar poi-scratchpad-3.2-FINAL.jar xmlbeans-xmlpublic-2.4.0.jar " – JgSudhakar May 22 '14 at 09:30
  • I am using maven, so I dont face jar file issues. Is it possible for you to use maven? And I have edited my post to put a complete example. Can you try this small code and see if this works? – Hirak May 22 '14 at 10:17
  • thanks for replay, still giving the NoSuchMethodError.Method is not there in the Specifies Jar. I think this is the issue with Jar file. could you please send the pom.xml and the List of import which you used in the class . Thanks ... – JgSudhakar May 22 '14 at 10:26
  • Thanks very much Hirak. It's working fine and able to create word document with Table. If you please how can add the Header and footer to the same file(test.docx)(header and footer having images) really helped me alot . could u plz suggest on header and footer too – JgSudhakar May 22 '14 at 10:52
  • You can refer this link for header/footer. Please accept the answer if it helped. http://stackoverflow.com/questions/9430198/updating-a-docx-files-page-header-using-apache-poi – Hirak May 22 '14 at 11:11
  • When 'm assigning document object to the XWPFHeaderFooterPolicy it is showing Nullpointer Exception. Can you please send some sample snippet for adding the Header and footer to the document which is being created in the above scenario. please suggest on this . – JgSudhakar May 22 '14 at 12:26