|
Val's Blog
Lots of stuff for Web 2.0 freaks and Java addicts
|
|
|
Jane Cleland-Huang: "Software development should integrate and consider project metrics that assess its financial impact." |
[ Login ] |
|
Ever needed to generate Word documents from your Java applications? There are only a few tools that allow you to do that. The Apache POI team has developed an API for manipulating Excel files (org.apache.poi.hssf packages) and is currently developing one for Word files also (org.apache.poi.hwpf packages). However, the latter is still in early beta stages and is not bundled with the 2.0 release, so you have to go into CVS and get the files by yourself. If this suffices, then you are good to go. Otherwise I can propose another solution. A German software company, called Müller & Stein Software, proposes on its website a free component (yes, as in "free" beer) that you can call directly from your Java application to generate any kind of Word files. The downside is that it won't let you read the Word file, though. You're gonna have to use the POI HWPF API if you need that functionality. The way the whole thing works is incredibly easy. In the bundle, you will find three key files:
All you have to do is to define bookmarks (Insert/Bookmark) in the template Word file and give them a name. This name will let you identify what goes where when you generate the Word file. Then, from you Java application, you can write code like:
Let me explain a little further what this code does:
Before calling
As you can see this Java2Word component is very simple, yet very powerful. Play around with it and you won't be disappointed. The installation is straightforward and the driver is very fast. In my case, I have to generate 50 pages Word files, and the whole thing takes like four to five seconds.
TrackBacks[0]
Comments[53]
Posted by val on October 16, 2004 8:52:08 AM CEST
Reply |
Permalink
Re: Generate Word documents from your Java applications
Another approach is to use JooTemplates to create an OpenOffice.org file. From there, JooConverter can get you to Word, or PDF, or a few other formats. Not as easy, but completely open source, for places where that's important.
Comment from Anonymous on October 16, 2004 12:27:10 PM CEST
Thank you for your input. I will look into that solution as well :)
Comment from Val on October 16, 2004 12:54:41 PM CEST
Hi,
I am able to generate word document with WordApi.exe. But is it possible to insert image, page-break with this. I need to publish variable no of pages. Is it possible to repeat some "Section" ? Pls reply soon.
Comment from Srinath on June 24, 2005 12:00:33 PM CEST
Hi,
I was trying the sample to generate the word document the same SampleTemplate.dot, that I downloaded and I wrote my own Java Class as follows import de.must.util.WordProcessing; public class WriteToDoc { public static void main(String args[]) { WordProcessing.createNewDocumentFromTemplate("SampleTemplate"); WordProcessing.typeTextAtBookmark("AddressLine1", "O'Reilly & Associated, Inc."); WordProcessing.typeTextAtBookmark("AddressLine2", "Mr Miller"); WordProcessing.typeTextAtBookmark("AddressLine3", "101 Moris Street"); WordProcessing.typeTextAtBookmark("AddressLine4", "Sebastopol, CA 95472-9902"); WordProcessing.typeTextAtBookmark("Salutation", "Dear Mr Miller,"); WordProcessing.exec(); } } and I kept the de.must.util.WordProcessing class file in the class path and compiled successfully. But I am getting an error at runtime as follows A window with title Protocol of Word Processing Control is getting opened and in that window error msg is displayed as "Template SampleTemplate not found" I kept the Sample Template.dot file in the same directory as WriteToDoc.class file is placed. Please let me know the solution for this. thanks, Pramod.
Comment from Pramod on November 3, 2004 9:03:28 AM CET
Hello Pramod ,
1).Is it possible to see the generated document in MS Word ?
I got some info that its a bit complicated process to open the generated word doc in MS Word , using this api.
2) Also , are the images being rendered properly.
Please respond man , its kinda urgent
Regards,
Aash K.
aashish_kohli@satyam.com
Comment from Anonymous on December 9, 2004 4:54:14 PM CET
Hi,
I got it working by placing the SampleTemplate.dot file in the default Templates directory ..Documents and Settings\Administrator\Application Data\Microsoft\Templates Copied the SampleTemplate.dot file to above folder and got the Word Document created.. Thanks for API, Pramod.
Comment from Pramod on November 3, 2004 10:40:56 AM CET
Hi pramod ,
I have downloaded this api[mull-und-stein] , but while i'm tyring to install WordApi.exe , i'm getting an error ...
It says ..."The input file 'WordInp.txt' couldnot be found"
Please tell how did you installed this api , and also the compiling instructions.
Comment from Anonymous on December 10, 2004 6:54:06 AM CET
Here's an even better way to do this, and doesn't require Windoze to run the exe;
http://blogs.msdn.com/dotnetinterop/archive/2005/03/29/403331.aspx
Comment from Anonymous on May 5, 2005 3:31:13 PM CEST
I am New to java.I have got a similar problem.
the error is something like this Pls help me out.
writetodoc.java:7: cannot resolve symbol
symbol : variable WordProcessing
location: class WriteToDoc
WordProcessing.createNewDocumentFromTemplate("SampleTemplate");
^
writetodoc.java:8: cannot resolve symbol
symbol : variable WordProcessing
location: class WriteToDoc
WordProcessing.typeTextAtBookmark("AddressLine1", "O'Reilly & Associated, Inc."
;
^
writetodoc.java:9: cannot resolve symbol
symbol : variable WordProcessing
location: class WriteToDoc
WordProcessing.typeTextAtBookmark("AddressLine2", "Mr Miller");
^
writetodoc.java:10: cannot resolve symbol
symbol : variable WordProcessing
location: class WriteToDoc
WordProcessing.typeTextAtBookmark("AddressLine3", "101 Moris Street");
^
writetodoc.java:11: cannot resolve symbol
symbol : variable WordProcessing
location: class WriteToDoc
WordProcessing.typeTextAtBookmark("AddressLine4", "Sebastopol, CA 95472-9902");
^
writetodoc.java:12: cannot resolve symbol
symbol : variable WordProcessing
location: class WriteToDoc
WordProcessing.typeTextAtBookmark("Salutation", "Dear Mr Miller,");
^
writetodoc.java:13: cannot resolve symbol
symbol : variable WordProcessing
location: class WriteToDoc
WordProcessing.exec();
^
Comment from Bijoy on November 4, 2004 7:14:28 AM CET
¡¡Puta madre no encuentro el driver HWPF!!
Comment from Vergudo on November 11, 2004 6:19:28 PM CET
You can specify the Template file and WordAPI.exe are located but you cannot modify the directory location of the WordInp.txt. unless you compile WordAPI.exe yourself You can implictly specify where the Word Template can be loaded from by specifying its location by passing the path+filename: Example: wp.createNewDocumentFromTemplate("C:\\SomeDirectory\\DifferentTemplate.dot"); You can implictly specify where to call the WordAPI.exe by specifying its location by passing the path+filename to: Example String[] cmd = {"C:\\SomeDirectory\\WordAPI.exe"}; ... Runtime.getRuntime().exec(cmd); If you change the location of the WordInp.txt you will get the "The input file 'WordInp.txt'" error. Please ensure that File is instantiated this way wordInput = new File("WordInp.txt");
Comment from Mark Gajudo on January 26, 2005 10:17:59 PM CET
I'm new in java programming.I following above. I've already three key files (WordAPI.exe,WordProcessing.java andSampleTemplate.dot)and also follow in WriteToDoc.java(Pramod) but when I compile, program said
WriteToDoc.java:1: package de.must.util does not exist
import de.must.util.WordProcessing;
^
how can i solve this problem? please help me.
Comment from kwan on March 8, 2005 10:55:33 AM CET
What is the file "WordInput.txt" for??
Where do I get one? I don't understand what it's for, because I've never seen such a file.
Comment from bodo on March 22, 2005 2:44:31 PM CET
Hello
I need Help. Everything seems to work fine, no error messages form the application - but it doesn't create any word files, it doesn't print anything. After execute the programm ends, and thats it.
What is wrong? I was watching the Task Manager. WINWORD.EXE is started, after exec it uses 99% of CPU power for a 2 - 3 seconds, then nothing happens. winword.exe never ends. When I execute the programm again I get 2 WINWORD.EXE at tha Task Manager..then 3, 4... nothing else
What is wrong? I'm using the example code from above.
Comment from Bodo on March 22, 2005 3:24:18 PM CET
With this API I can write a "table" of variable number of collumns and rows?
There is any API to do this?
Comment from Alessandro Kieras on April 22, 2005 4:15:12 PM CEST
Any news on this?....
Comment from Aneesh on June 22, 2005 12:25:45 PM CEST
I want to generate a word document from dynamic data.Please help
Comment from Anonymous on September 30, 2005 10:28:48 PM CEST
doesnt seem that this API will help us in creating a table in the output word document? does it? any other ideas?
Comment from Anonymous on September 18, 2006 11:15:43 PM CEST
Hello
I want to mention that it works now on my system. I used Windows 2000 and Office 97 and it didn't work. Now I upgradet to Office 2003 and it works. (Müller and Stein's page says it should work with Word 97, but it didn't)
Comment from Bodo on March 23, 2005 11:57:54 AM CET
how webpage converted to word document for print
Comment from vipin on March 23, 2005 12:28:39 PM CET
Bado did you find any other solution where you could generate word document irrespective of Word 97 or latest??
Thanks
Comment from Murugesh on April 1, 2005 9:15:05 AM CEST
A better way to do this may be to use the WordProcessingML schema, and just generate XML. No special libs required.
click here for an example
Comment from Dino on April 1, 2005 1:58:04 PM CEST
It is working fine. I would like to summarize how it worked out.
1. Downloaded source - Tried to install wordapi.exe. It throwed some error. Dont mind about that.
2. Compile WordProcessing.java
3. Store all the three key files in the same directory.
4. Create another java file with the source code given.
5. If the template file is stored in same directory then pass the whole path along with the createNewDocumentFromTemplate("c:\...") method or store it in default template directory.
6. Compile and run....
Doc created :)
Comment from Murugesh on April 1, 2005 2:02:50 PM CEST
I am calling this from a JSP on an ORION server, how do I specify that the word file be saved locally?
Comment from thomas on April 7, 2005 9:11:19 PM CEST
Hi,
I have downloaded all 3 mentioned files...what r the locations to place them...procedures to follow then???
Comment from Harinath on April 15, 2005 3:42:10 PM CEST
Hi,
Is there a way to generate WordDocs using Java from a Unix Server running on SunOS.
Is there a way to use templates too for the word docs creation.
Regards,
Ashish Kumar
Comment from Anonymous on July 5, 2005 8:26:44 AM CEST
I am able to Generate the Word Documemt. But when i use "closeDocument()" or "quitAppliation()" only the generated Document is getting closed not the Word Application. Every time i generate the document a new Word Application is opened which is never closed.
How do I close the Application.
Thanks in Advance
Comment from Anonymous on July 12, 2005 8:22:44 AM CEST
"I am able to Generate the Word Documemt. But when i use "closeDocument()" or "quitAppliation()" only the generated Document is getting closed not the Word Application. Every time i generate the document a new Word Application is opened which is never closed. How do I close the Application. "
Is there a solution regarding this?
thanks Nicolás Fonnegra
Comment from Nicolás Fonnegra on July 25, 2005 6:15:17 PM CEST
I'm taking an old report document and making a template from it. I am able to create bookmarks and use the files to write to them, unless one is in a Word textbox. Is there a way to reference a bookmark inside a textbox?
Comment from handy on July 20, 2005 9:56:17 PM CEST
On another note is there someplace I can see what methods are inside the WordAPI.exe? As this is open source, if I can see the methods inside I could possibly write a java method to utilize one (assuming there is something in there to add text inside a textbox
thanks
Comment from handy on July 20, 2005 9:58:21 PM CEST
Hi all, I want to use this API but I cannot understand if I have to install the "WordAPI.exe" BEFORE using WordProcessing.java. Can anybody explain how to:
1. Install the API
2. Reference user tempates
3. Create Docs whit bookmarks filled
The problem is that if I simply double-click on WordAPI.exe I have the usual problem related to WordInput.txt (file not found). What is this file??? Where is it?
Thanx a lot,
Marco
Comment from Marco on July 28, 2005 3:02:08 PM CEST
Hello, I have exactly the same problem whith this "WordInput.txt". Please, help us...
Comment from Fenrir on July 29, 2005 2:53:37 PM CEST
How to do the same thing over HTTP. I want to do the same process in the client side using some server side application. Please help.
Comment from Aquacoolz on August 4, 2005 3:56:31 PM CEST
hi,
i'd like to write a word document making a mailing, ie. merging content of a document model with txt file containing fields values.
An other question is to know if there is a word property for the txt file path and if we can modify it in the word file.
thanx
Comment from beubz on August 24, 2005 10:23:05 AM CEST
Hi all, i want to make a merging between a word model document and a field values txt file using java.
Other thing, is it possible to write word property about the path of the txt file
any ideas
thanx
Comment from beubz on August 24, 2005 10:29:26 AM CEST
Hello,
Can I force it to create and open the documents on the client machines (online users accessing the application)?
Comment from Anonymous on September 20, 2005 11:42:11 AM CEST
I have similar kind of problem,
I have to open a word document in read only format, which is located in unix Operating System(different location), my java applications runs on WinX.
Can anybody throw any light on it,
it would be helpful if any body send a sample code.........
Comment from raj on October 27, 2005 11:33:03 AM CEST
i have a question. can I use the same thing using JBOSS, I want to create a report in word and show it in the browser. how can I do that? thanx
Comment from pilar on November 16, 2005 7:54:33 PM CET
I am having similar a problem to the problem Bodo had in March. I get no errors, I see WordAPI.exe and WinWord.exe start in the Task Manager, but a document is never produced. I have tried to use the "printToPrinterToSelectByUserAndForget" method and the printer select dialog does not appear. I am calling the class from within JavaScript in another application. I think this class would really solve some problems for us, anyone have some suggestions?
Comment from geg on November 29, 2005 2:37:28 PM CET
Hi, WordAPI.exe, looks like it will work only on Windows.I have a requirement to create a word document within a UNIX enviornment.The file is later fed to Microsoft Windows, but before that, it has to be created within Unix.
Can anybody help please?
Comment from parshu on January 31, 2006 5:24:41 PM CET
Where can I get the following
WordAPI.exe
WordProcessing.java
SampleTemplate.dot
Can you forward the same to my email id so that I can make use of the api .
Comment from Bharath on February 16, 2006 3:40:41 AM CET
Hi,
I'm going to migrate my Weblogic 7.0 J2EE application into JBoss. JCom doesn't come with JBoss and I'm thinking in using the proposed in this forum but I really don't know how to go about it. I have a Unix server for the Web application and a COM server where I save my word douments.
Does anybody have an example of implementation?
Kind Regards
Comment from Oz on March 28, 2006 5:07:28 PM CEST
I forgot to say that my templates have pictures and the tags are like «n» where n is a natural
Thanks in advance
Comment from Oz on March 28, 2006 5:12:01 PM CEST
can u suggest me a logic to access a word application using java code. It will be very much helpfull if source code is provided.
Comment from karthikeyan on March 30, 2006 8:10:52 AM CEST
i doin a project to make file browser in java.ll u plz guide me how to open a word file?
Comment from ritika verma on April 3, 2006 7:16:21 PM CEST
I am running a program that would make use of the created word document..
I need to know a way to wait, till the time the word document has been created successfully..
Is there an api that helps in this.
Comment from mali on July 21, 2006 11:46:36 AM CEST
Please anybody upload working example?
It will be appreciated.
Comment from Anadn on August 16, 2006 8:53:36 PM CEST
iam using tomcat server,& need to generate word document at clientside.i can create the word doc wen the oath of wordapi is hardcoded like
String cmd = "C:\\Tomcat 4.1\\webapps\\ajax1\\WEB-INF\\WordAPI.exe";
but how to access the wordApi.exe file from client machine.
doe any body know how to do this
Thank U
Comment from Heidi on August 23, 2006 8:36:57 AM CEST
You would be best to create the document server side and stream it to the client once it is created, that way the api etc only need to live in the server classpath and not the client's one. the only assumption then being that the user has MSWord on their client. Else you will find massive portability problems. Hope this hel, S
Comment from Shaunybee on November 8, 2006 12:30:16 PM CET
I can read word document using jakarta POI API's. But i want to search and replace some of text in that document.
How to modify same Word documents using jakarta POI??
Comment from Rahul on August 25, 2006 2:08:14 PM CEST
I can read word document using
org.apache.poi.hwpf.HWPFDocument class
i get whole of text
but i want to replace some text
how can i do that??
if u know abt this plx let me know...
Thanks in advance
Comment from Rahul on August 28, 2006 7:36:57 AM CEST
Hi, I am able to generate word document with WordApi.exe.
But I try to created word document with header WordProcessing.typeTextAtBookmark("AddressLine1", "My header") and get Bookmark AddressLine1 in template not found. Dose anyone have idea? Help!
Comment from Anonymous on September 25, 2006 9:51:38 PM CEST
Hi I am trying to generate the word doc but i m not understanding wats happening
any one pls figure it out
/*
* WordAPI.java
*
* Created on May 30, 2006, 10:50 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package com.arbitron.specialnoticespage;
import java.io.IOException;
/**
*
* @author rsoni
*/
public class WordAPI {
/** Creates a new instance of WordAPI */
public WordAPI(String output, String Survey, String Market, String Year, String[] cmdMacro, String[] dataWord) {
String cmd = "C:\\DocumentsandSettings\\elthot\\Desktop\\BackUp\\JavaWord\\WordAPI.exe";
WordProcessing.createNewDocumentFromTemplate("C:\\Documents and Settings\\elthot\\Desktop\\BackUp\\Project #6-SSA\\SpecialPlate.dot");
/////////////////////
WordProcessing.executeMacro("InitializeForm");
WordProcessing.typeTextAtBookmark("Survey", Survey);
WordProcessing.typeTextAtBookmark("Market", Market);
WordProcessing.typeTextAtBookmark("Year", Year);
WordProcessing.executeMacro("InitializeFormData");
for(int x=0; x<cmdMacro.length; x++){
WordProcessing.executeMacro(cmdMacro[x]);
}
int SectionCount=1;
int SubtitleCount=1;
int NoticeCount=1;
int TableHeaderCount=1;
int TableValueCount=1;
int RowCount=1;
int ColumnCount=1;
int y=0;
for(int x=0; x<dataWord.length; x++){
if(cmdMacro[y].compareTo("SectionHead")==0){
WordProcessing.typeTextAtBookmark("Section"+String.valueOf(SectionCount), dataWord[x]);
///////////////////////////////////////////
System.out.println("Section"+String.valueOf(SectionCount)+" "+dataWord[x]);
///////////////////////////////////////////
SectionCount++;
} else if(cmdMacro[y].compareTo("SubTitle")==0){
WordProcessing.typeTextAtBookmark("Subtitle"+String.valueOf(SubtitleCount), dataWord[x]);
///////////////////////////////////////////
System.out.println("Subtitle"+String.valueOf(SubtitleCount)+" "+dataWord[x]);
///////////////////////////////////////////
SubtitleCount++;
} else if(cmdMacro[y].compareTo("Notice")==0){
WordProcessing.typeTextAtBookmark("Notice"+String.valueOf(NoticeCount), dataWord[x]);
///////////////////////////////////////////
System.out.println("Notice"+String.valueOf(NoticeCount)+" "+dataWord[x]);
///////////////////////////////////////////
NoticeCount++;
} else if(cmdMacro[y].compareTo("ResetTable")==0){
RowCount=1;
ColumnCount=1;
y++;
while(cmdMacro[y].compareTo("AddRow")==0){
RowCount++;
y++;
}
while(cmdMacro[y].compareTo("AddColumn")==0){
ColumnCount++;
y++;
}
if((cmdMacro[y].compareTo("Table_Border")==0)||(cmdMacro[y].compareTo("Table_NoBorder")==0)){
for(int i=0; i<ColumnCount; i++){
WordProcessing.typeTextAtBookmark("TableHeader"+String.valueOf(TableHeaderCount), dataWord[x]);
//////////////////////////////////////////////////////
// System.out.println("TableHeader"+String.valueOf(TableHeaderCount)+" "+dataWord[x]);
//////////////////////////////////////////////////////
TableHeaderCount++;
x++;
}
for(int i=0; i<(ColumnCount*RowCount-ColumnCount); i++){
WordProcessing.typeTextAtBookmark("TableValue"+String.valueOf(TableValueCount), dataWord[x]);
///////////////////////////////////////////////////////
// System.out.println("TableValue"+String.valueOf(TableValueCount)+" "+dataWord[x]);
///////////////////////////////////////////////////////
TableValueCount++;
x++;
}
x--;
}
}
else if(cmdMacro[y].compareTo("BackSpace")==0){
x--;
}
y++;
}
WordProcessing.saveDocumentAs(output);
WordProcessing.exec(cmd);
// try {
// Runtime.getRuntime().exec(cmd);
// } catch (IOException ex) {
// ex.printStackTrace();
// }
}
}
Comment from Anonymous on November 30, 2006 4:47:34 PM CET
TrackBack to http://radio.javaranch.com/val/addTrackBack.action?entry=1097909528000
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Content © Val | Powered by Pebble 1.9.1 |