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):
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!");
}
}
what about images???
ReplyDeleteIt is possible, you need to investigate see this link:
Deletehttp://poi.apache.org/spreadsheet/quick-guide.html#Images
I get...
ReplyDeleteException 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
Sorry I updated the post, with the full example.
DeleteYou should be able to follow.
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?
DeleteI run this code and my doc file like this:
ReplyDeletePK еvMA docProps/core.xmlґ▒мJд0 FВ}┼░}⌡єUяпv e@P ╟┐Б.євІьЭ░D;ЎҐ≥нXQ\╨╪Ынw╦э■╚² я;8? ]a√Q▄@Kс
I can't understand the question.
DeleteAre you using some special characters?
Try in english.
I am trying to pass a String (initialized elsewhere)inside setText to print in the .doc but I get an error.
ReplyDeleteIs there a way to do that?
I mean:
String s = "Some data that i get from a list";
tmpRun.setText(s);
I see some comments about difficulties with this hello world program.
DeleteI 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
Hi,
ReplyDeleteIs this work with .doc file also
I updated this post recently because some people were complaining about it not working(Probably they were not using the right packages).
DeleteThis 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/
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.
ReplyDeleteYou are right. Aspose is a great tool to work with office docs of all kinds.
DeleteIf you have the money to afford it definitely is a great tool :)
yes you are right i have purchased their silver package and its working very good for me.
Deletejava.lang.NoClassDefFoundError: org.apache.poi.xwpf.usermodel.XWPFDocument
ReplyDeletethis error i m facing while using xdocreport libs on android
please help me to solve it
I have the same problem, are you solved it?
Delete