Pages

Sunday, June 12, 2011

Using apache POI to write to word documents

A simple Hello World example for writing  into word files with apache POI
If you use maven add this 2 dependencies(Otherwise download and necessary libraries and add them to your build path):
 <dependency>   
   <groupId>org.apache.poi</groupId>   
   <artifactId>poi</artifactId>   
   <version>3.9</version>   
  </dependency>   
  <dependency>   
   <groupId>org.apache.poi</groupId>   
   <artifactId>poi-ooxml</artifactId>   
   <version>3.9</version>   
  </dependency>   

Here a simple Hello World! program:


 package com.djordje;   
  import java.io.File;   
  import java.io.FileOutputStream;   
  import org.apache.poi.xwpf.usermodel.XWPFDocument;   
  import org.apache.poi.xwpf.usermodel.XWPFParagraph;   
  import org.apache.poi.xwpf.usermodel.XWPFRun;   
  public class App {   
    public void newWordDoc(String filename, String fileContent)   
         throws Exception {   
       XWPFDocument document = new XWPFDocument();   
       XWPFParagraph tmpParagraph = document.createParagraph();   
       XWPFRun tmpRun = tmpParagraph.createRun();   
       tmpRun.setText(fileContent);   
       tmpRun.setFontSize(18);   
       FileOutputStream fos = new FileOutputStream(new File(filename + ".doc"));   
       document.write(fos);   
       fos.close();   
    }   
    public static void main(String[] args) throws Exception {   
         App app = new App();   
         app.newWordDoc("testfile", "Hello World!");   
    }   
  }   

16 comments:

  1. Replies
    1. It is possible, you need to investigate see this link:
      http://poi.apache.org/spreadsheet/quick-guide.html#Images

      Delete
  2. I get...
    Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlException
    at com.rix.radicalWordTemplate.CreateTablesWithPOI.main(CreateTablesWithPOI.java:15)
    Caused by: java.lang.ClassNotFoundException: org.apache.xmlbeans.XmlException
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    ... 1 more

    ReplyDelete
    Replies
    1. Sorry I updated the post, with the full example.
      You should be able to follow.

      Delete
    2. Even i am facing the same exception. I am using "poi-ooxml-3.7-beta3" and "poi-3.10-beta2-20130904" jars. Apart from these, any other jars required?

      Delete
  3. I run this code and my doc file like this:
    PK еvMA docProps/core.xmlґ▒мJд0 FВ}┼░}⌡єUяпv e@P ╟┐Б.євІьЭ░D;ЎҐ≥нXQ\╨╪Ынw╦э■╚² я;8? ]a√Q▄@Kс

    ReplyDelete
    Replies
    1. I can't understand the question.
      Are you using some special characters?
      Try in english.

      Delete
  4. I am trying to pass a String (initialized elsewhere)inside setText to print in the .doc but I get an error.
    Is there a way to do that?

    I mean:
    String s = "Some data that i get from a list";
    tmpRun.setText(s);

    ReplyDelete
    Replies
    1. I see some comments about difficulties with this hello world program.
      I just repeated the example to double check that there are no mistakes in the post. I just edited it with the hold example that works.
      Please double check that you are using the right libraries.
      Find more information about the apache POI project at:
      http://poi.apache.org/hwpf/index.html

      Delete
  5. Hi,
    Is this work with .doc file also

    ReplyDelete
    Replies
    1. I updated this post recently because some people were complaining about it not working(Probably they were not using the right packages).
      This should work now, the best thing you can do, is try to find out.
      If you still have some issues, I suggest you to read through Apache POI's documentation http://poi.apache.org/ if you see that you need maybe some other libraries, you can easily get them from the mvn central repo(If you are using maven)at http://search.maven.org/

      Delete
  6. You can write data to existing MS word file by using Aspose.Words for Java Library. It offers many other features also like creating word file from scratch, reading , modifying and converting word files to other documents.

    ReplyDelete
    Replies
    1. You are right. Aspose is a great tool to work with office docs of all kinds.
      If you have the money to afford it definitely is a great tool :)

      Delete
    2. yes you are right i have purchased their silver package and its working very good for me.

      Delete
  7. java.lang.NoClassDefFoundError: org.apache.poi.xwpf.usermodel.XWPFDocument
    this error i m facing while using xdocreport libs on android
    please help me to solve it

    ReplyDelete

Share with your friends