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:wordDocument
xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml">
<w:body>
<w:p>
<w:r>
<w:t>Hello World.</w:t>
</w:r>
</w:p>
</w:body>
</w:wordDocument>

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.
http://www.microsoft.com/downloads/details.aspx?familyid=fe118952-3547-420a-a412-00a2662442d9&displaylang=en

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.io.File;
import java.io.FileWriter;
import java.io.IOException;
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();
pmap.put("progid","Word.Document");
doc.addProcessingInstruction("mso-application",pmap);

Element root = DocumentHelper.createElement("w:wordDocument");
//Set up the necesary namespaces
root.setQName(new QName("wordDocument",new Namespace("w","http://schemas.microsoft.com/office/word/2003/wordml")));

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

root.add(e2);
doc.add(root);
e3.setText("Hello World");
e1.add(e3);
e.add(e1);
e2.add(e);

return doc;
}

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

}

}

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

You can download the source code.

 

One Stop for Keyboard Shortcuts........

Are you using keyboard shortcuts when using applications? If not you better start using them because they can save lot of time and make the work much easy.
In one of my previous posts I've put some shortcuts that you can use with Windows key. Today I found a wonderful website that contains huge list of shortcuts for various applications. It's called KeyXL. Hope you'll find it very useful.
 

I'm Back...

It has been more than 1 1/2 months since I've done my previous post. It has been very tough time for me with all of the academic activities in the university. Last semester was the toughest semester yet. Had to complete back to back assignments without break ant then had to prepare for the exam. Now it’s all finished.
With this exam our general degree finishes. But I’m planning to do the special as well, so I’ve another year to go before finishing. There is going to be two months break before start of 4th year. So I can have little bit of break and do few more blog posts :)