Java Reporting With MS Word

Isn't it wonderful if we can generate MS Word documents as reports from our Java application.... There are number of way to generate MS Word documents from Java such as using Jakarta POI (Poor Obfuscation Implementation) from the Apache Jakarta Project.
But there is a much easier way to do it, by using WordprocessingML. It is an XML schema developed my Microsoft to create word documents and their formating. Using the XML tags in WordprocessingML we can create our word document very easily.
Here is a small code fragment that displays "Hello World"...

<?xml version="1.0"?>
<?mso-application progid="Word.Document"?>
<w:t>Hello World.</w:t>

Save this as .xml and open using MS word. You will be able to see output like in this figure.

Lets analyze important tags in the code.
  • <?mso-application progid="Word.Document"?> is a processing instruction that tells Windows to treat this XML file as a Word document. You must put it in the document for it to be recognized by Word and Windows. The text-related elements for WordprocessingML have the prefix w.
  • <wordDocument/> defines a Word document. You can see that w's namespace is defined here.
  • <body> is a container for text in the document.
  • <p> defines a paragraph.
  • <r> defines a run of other elements. Inside this tag, you should place tags like <t>, <br>, and so on. All elements inside a <r> element can have a common set of properties.
  • <t> is the element that encloses actual text.
You can find the complete Office 2003 XML Reference Schemas from the Microsoft web site.

Now you know the basics of WordprocessingML. Next thing to do is to use some king of XML library and generate the XML document according to WordprocessingML Reference Schema. Here I'm using dom4j XML library.

import java.util.HashMap;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Namespace;
import org.dom4j.QName;

* @author sandarenu
public class WordMLTest
* Create the xml for head part of the word xml
* Add processing and schema info.
* Created on: Jun 23, 2007
* @Author sandarenu
* @return
public static Document createDocument()
Document doc = DocumentHelper.createDocument();
HashMap pmap = new HashMap();

Element root = DocumentHelper.createElement("w:wordDocument");
//Set up the necesary namespaces
root.setQName(new QName("wordDocument",new Namespace("w","")));

Element e2 = DocumentHelper.createElement("w:body");
Element e = DocumentHelper.createElement("w:p");
Element e1 = DocumentHelper.createElement("w:r");
Element e3 = DocumentHelper.createElement("w:t");

e3.setText("Hello World");

return doc;

* Created on: Sep 29, 2007
* @Author sandarenu
* @param args
public static void main(String[] args)
Document doc = createDocument();
File outputFile = new File("c:\\hello.xml");
FileWriter out = new FileWriter(outputFile);
catch (IOException e)



Using this simple logic you can generate very complex word reports using Java. Have fun....

You can download the source code.


Reader Comments

could you please tell me one thing.
how can i read the document using dom4j.
means i want to print the "hello world" string inthe above example.

is this works?
because, here i am using prefix.
do you have any idea, how to fetch the element value using prefix.

Tribune to Heros

Tribune to Heros
We will never forget the sacrifices you made to protect our country....


Get Google Toolbar Button

Enter your email address:

Delivered by FeedBurner