How to create a Word Document using Apache POI?

How to create a Word Document using Apache POI?

I am developing a Resume Editor for Atlassian Confluence as Commercial Plugin.

I am sorry I had to ask this but I do not find tutorials witch can help me.


Your attached code file "DownloadAsMicrosoftWordDocument.java.txt" has a coding for file download functionality; no Word document creation.

As you looking for Word Document creation, please find references below:

HWPF Reference(.doc): POI trunk doesn't have examples as XWPF do, However POI Scratchpad has Testcases around it, please find

  • POI Scratchpad
  • Javadoc HWPFDocument
  • Javadoc org.apache.poi.hwpf.usermodel

XWPF Reference(.docx): Examples from Apache POI SVN Repo

  • SimpleDocument.java
  • SimpleImages.java
  • SimpleTable.java
  • UpdateEmbeddedDoc.java

And also refer POI Javadocs for XWPF (Word Document).

I hope it will provide startup for you!


package org.poi.images;

import java.io.File;   
  import java.io.FileOutputStream;   
  import org.apache.poi.xwpf.usermodel.XWPFDocument;   
  import org.apache.poi.xwpf.usermodel.XWPFParagraph;   
  import org.apache.poi.xwpf.usermodel.XWPFRun;   
  public class DocFile {   
    public void newWordDoc(String filename, String fileContent)   
         throws Exception {   
       XWPFDocument document = new XWPFDocument();   
       XWPFParagraph tmpParagraph = document.createParagraph();   
       XWPFRun tmpRun = tmpParagraph.createRun();   
       tmpRun.setText(fileContent);   
       tmpRun.setFontSize(18);   
       FileOutputStream fos = new FileOutputStream(new File("C:\\Users\\amitabh\\Pictures\\pics\\"+filename + ".doc"));   
       document.write(fos);   
       fos.close();   
    }   
    public static void main(String[] args) throws Exception {   
         DocFile app = new DocFile();   
         app.newWordDoc("testfile", "Hi hw r u?");   

    }   
  }