Replace string in ms word file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • iComas
    New Member
    • May 2009
    • 1

    Replace string in ms word file

    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
  • cmdr
    New Member
    • Mar 2009
    • 3

    #2
    solution

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

    Example:
    Code:
    import officetools.OfficeFile; // officetools.jar from this website http://www.dancrintea.ro/doc-to-pdf/ 
     
    FileInputStream fis = new FileInputStream(new File("irene_template.doc"));
    FileOutputStream fos = new FileOutputStream(new File("irene_final_result.doc"));
     
    OfficeFile f = new OfficeFile(fis,"localhost","8100", true);
     
    f.replaceAll("Hello","Salue Irene");
    
    f.write(fos);
    Bonne chance :)

    Comment

    • sgilberta
      New Member
      • Jul 2009
      • 7

      #3
      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.

      Comment

      • rextyler
        New Member
        • Dec 2009
        • 1

        #4
        I was looking especially for convertion to pdf, but replacing is really great, I did not know it was possible. Good stuff. Thx for sharing.

        Comment

        Working...