Connecting Tech Pros Worldwide Forums | Help | Site Map

Replace string in ms word file

Newbie
 
Join Date: May 2009
Posts: 1
#1: May 7 '09
Salue,

I need to add a functionality to my Java app by allowing the replacement of strings in ms word files.

Basically I need to use an original MS Word file as template and then populate it with data,
replacing some keywords. And I need to do this with Java.

I know MS Word is binary and not text file, so I was wondering if there is any Java API to let me do this?

Merci, Irene

Newbie
 
Join Date: Mar 2009
Posts: 3
#2: May 7 '09

re: Replace string in ms word file


Yes Irene, it is possible to use ms word templates and populate them with data.

Example:
Expand|Select|Wrap|Line Numbers
  1. import officetools.OfficeFile; // officetools.jar from this website http://www.dancrintea.ro/doc-to-pdf/ 
  2.  
  3. FileInputStream fis = new FileInputStream(new File("irene_template.doc"));
  4. FileOutputStream fos = new FileOutputStream(new File("irene_final_result.doc"));
  5.  
  6. OfficeFile f = new OfficeFile(fis,"localhost","8100", true);
  7.  
  8. f.replaceAll("Hello","Salue Irene");
  9.  
  10. f.write(fos);
  11.  
  12.  
Bonne chance :)
Newbie
 
Join Date: Jul 2009
Posts: 2
#3: Jul 2 '09

re: Replace string in ms word file


Thanks both for these messages, they really helped me and now OfficeTools is my default library when working with ms office files.

A bunch of nice operations are possible:
- replace strings in DOC files as in your example
- convert DOC, XLS or PPT to PDF
- read/write Excel files using simplified API like getCell and setCell
- show/hide Excel sheets(like secondary calculations)

The only downside is that it needs OpenOffice,
but the good part is that I can do all the above stuff on ms office files.
Reply