How to write to the .doc file using apache POI

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jettimadhu
    New Member
    • Oct 2009
    • 5

    #1

    How to write to the .doc file using apache POI

    I am trying to edit the word document(.doc/.docx) content using apache poi jar, But i unable to edit the document.
    Is there any possible way to edit word document using poi?
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    #2
    Read documents here

    Regards
    Dheeraj Joshi

    Comment

    • jettimadhu
      New Member
      • Oct 2009
      • 5

      #3
      Hi ,thanks for reply. Here i am posting the problem i faced while writing the .doc file.

      public class Headers {

      public void newWordDoc(Stri ng baseFilename, String outputFilename) {
      POIFSFileSystem fs = null;
      HWPFDocument doc = null;
      try {
      fs = new POIFSFileSystem (new FileInputStream (baseFilename)) ;
      doc = new HWPFDocument(fs );

      // Get the Range object encapsulating the text for the entire document.
      Range range = doc.getRange();
      public class Headers {

      public void newWordDoc(Stri ng baseFilename, String outputFilename) {
      POIFSFileSystem fs = null;
      HWPFDocument doc = null;
      try {
      fs = new POIFSFileSystem (new FileInputStream (baseFilename)) ;
      doc = new HWPFDocument(fs );

      // Get the Range object encapsulating the text for the //entire document.
      Range range = doc.getRange();
      if((range.text( )).contains("SA SI"))
      range.replaceTe xt("SASI", "${56565656 }");
      doc.write(new FileOutputStrea m(outputFilenam e));
      }
      catch(Exception ex) {
      System.out.prin tln("Caught an: " + ex.getClass().g etName());
      System.out.prin tln("Message: " + ex.getMessage() );
      System.out.prin tln("Stacktrace follows........ .....");
      ex.printStackTr ace(System.out) ;
      }
      }

      public static void main(String[] args) {
      Headers wt = new Headers();
      wt.newWordDoc("/home/bvritic/Desktop/docs/LatestResume.do c", "/home/bvritic/Desktop/docs/LatestResume.do c");
      }
      }

      I am trying to replace the string SASI with "${56565656 }". It is working fine for first time.But i run the same program second time
      i got the following error:

      05-20 19:07:20.197: WARN/System.err(4885 ): java.io.IOExcep tion: Invalid header signature; read 0x0D9D80E29C80E 20D, expected 0xE11AB1A1E011C FD0
      05-20 19:07:20.374: WARN/System.err(4885 ): at org.apache.poi. poifs.storage.H eaderBlockReade r.<init>(Header BlockReader.jav a:107)
      05-20 19:07:20.374: WARN/System.err(4885 ): at org.apache.poi. poifs.filesyste m.POIFSFileSyst em.<init>(POIFS FileSystem.java :151)
      05-20 19:07:20.374: WARN/System.err(4885 ): at org.apache.poi. hwpf.HWPFDocume nt.verifyAndBui ldPOIFS(HWPFDoc ument.java:132)
      05-20 19:07:20.374: WARN/System.err(4885 ): at org.apache.poi. hwpf.HWPFDocume nt.<init>(HWPFD ocument.java:14 5)

      Please suggest any procedure to follow?
      Thanks
      Jetti.

      Comment

      Working...