Connecting Tech Pros Worldwide Help | Site Map

Replace string in ms word file

  #1  
Old May 7th, 2009, 08:03 AM
Newbie
 
Join Date: May 2009
Posts: 1
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
  #2  
Old May 7th, 2009, 08:10 AM
Newbie
 
Join Date: Mar 2009
Posts: 3

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 :)
  #3  
Old July 2nd, 2009, 03:11 PM
Newbie
 
Join Date: Jul 2009
Posts: 2

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Editor to clean up MS Word-generated HTML table Greg Lovern answers 10 November 9th, 2007 05:15 AM
textarea fields --> export to ms word --> word is stretch Eric Layman answers 6 March 3rd, 2007 07:25 AM
Search & Replace in MS Word Puzzle Ola K answers 2 December 10th, 2006 10:05 PM
How to print tab in the middle of string in a report? Galina answers 1 November 12th, 2005 08:15 PM