In several projects we are faced with the request to export data to a spreadsheet (mostly excel) or build a document for ms-word. So our development team requested the following: “Build an extension that helps us generate word documents or export datasets to spreadsheets”.
After some research, I found a project called Apache POI.
Apache POI covers all the stories, which we try to implement. So let’s begin with the first story: “Generating a new ms-word document”.
The story is very simple, but also very powerful. In our example the starting point is a document like the OpenNTF Contributor License Agreement. This document is needed for a company to contribute code to OpenNTF. Typically, this document has some lines (fields), which you have to fill in with your values. Let’s try to automate this process. We will build a form in the XPages application, where you can fill in your company name, address and other things. Then we modify the standard OpenNTF form a little bit, as you can see on the screenshot below:
In your XPages form we implement the new POI Document element and then we do some wiring:
- We define where the modified OpenNTF Licence Agreement is. This could be a file resource in the application, or a document accessible via view and a predefined key.
- Defining “Bookmarks”:
As you see on the word document, we have defined several “<<NAME OF BOOKMARK>>” tags in the text. This text must be in the same style, so they are in the same “run” Element on the document.
- In our “POI Document” – Control could we now define a bookmark for each <<…>> element, which contains the name and a value.
- As you see, the value can be computed (so a binding to a viewScope variable is possible)
- We define a “download” button and connect the onClick event on a new action called “Generate Document”
The user can now fill in their values on the page and generate a customized word document. Developers are able to build in applications to produce customized documents in a very short time.
In the next blog entries, we will show you how we have build an extension of this function.
September 7, 2012 at 10:29 pm
NIce article and good explanation.
Can the same technique use for importing xls and word document where there is no MS word or excel is installed on the server?
September 7, 2012 at 10:34 pm
Yes. The 3. userstorie will cover the import of an excel file. Our beacon award finalist application use the import technic already. The hard job is now to find a way, how we can make this configurable in a XPages component, but we are working on that.
September 11, 2012 at 8:32 pm
A good introduction to the XPages Extensible API -> http://de.slideshare.net/muenzpraeger/uklug-2012-xpages-extensibility-api-going-deep
September 13, 2012 at 8:17 pm
As a novice XPage developer I was wondering how you have put the ‘Apache POI Document’ control together. Can you explain?
September 13, 2012 at 8:21 pm
I will document this in the next blog entries. It will cover how we build the kernel for the documentprocessing and the controlls. The full code with the source will be contributet to openNTF later this year (befor the openNTF context ends).
March 3, 2013 at 9:36 pm
I have tried to use the Xpages POI feature into our project. It works very well. But i have found out, that if using generating word document from the template document, it has to be the new docx or dotx form, not the old word 97-2003 form (.doc).
I have got this error:
POI 4 XPages -> ERROR
Error : Error during Documentgeneration
POI LIB : 220.127.116.11302211421
Is this restriction true, or should i set some other params? Thanx.
March 4, 2013 at 8:06 am
Yes we can only handle docx document in the actual relase. If the older format is also needed let us know and we make a evaluation if we can offer the same functionality.
March 4, 2013 at 6:52 pm
Thanx, thats fine for now.
I have tried this generating the word in IE 9, and the Xpage sends the ZIP file. Have you any experience with it?
March 5, 2013 at 9:08 am
We will check this.
April 20, 2013 at 2:04 pm
Hallo, hatten Sie die Zeit, das Problem mit MimeType anzuschauen? Danke.
June 12, 2013 at 3:13 pm
Wir konnten das Problem reproduzieren, die neuste Version auf openNTF hat einen entsprechenden Fix