36

I'm searching for a simple example code or a complete tutorial how to create a docx file with Apache POI and its underlying openxml4j.

I tried the following code (with a lot of help from the Content Assist, thanks Eclipse!) but the code does not work correctly.

String tmpPathname = aFilename + ".docx";
File tmpFile = new File(tmpPathname);

ZipPackage tmpPackage = (ZipPackage) OPCPackage.create(tmpPathname);
PackagePartName tmpFirstPartName = PackagingURIHelper.createPartName("/FirstPart");
PackagePart tmpFirstPart = tmpPackage.createPart(tmpFirstPartName, "ISO-8859-1");

XWPFDocument tmpDocument = new XWPFDocument(tmpPackage); //Exception
XWPFParagraph tmpParagraph = tmpDocument.createParagraph();
XWPFRun tmpRun = tmpParagraph.createRun();
tmpRun.setText("LALALALAALALAAAA");
tmpRun.setFontSize(18);
tmpPackage.save(tmpFile);

The thrown exception is the following:

Exception in thread "main" java.lang.NullPointerException
    at org.apache.poi.POIXMLDocumentPart.read(POIXMLDocumentPart.java:235)
    at org.apache.poi.POIXMLDocument.load(POIXMLDocument.java:196)
    at org.apache.poi.xwpf.usermodel.XWPFDocument.<init>(XWPFDocument.java:94)
    at DocGenerator.makeDocxWithPoi(DocGenerator.java:64)
    at DocGenerator.main(DocGenerator.java:50)

Does anybody can help me with my (really simple) requirements?

guerda
  • 23,388
  • 27
  • 97
  • 146
  • From where do i get the library for this? – AkashG Jan 30 '14 at 06:45
  • @AkashG you may need several libraries. Most / all of the Apache POI / OOXML stuff is here: https://mvnrepository.com/artifact/org.apache.poi (OOXML is the open-source standard on which Word is built.) – Sarah Messer Aug 16 '19 at 20:04

2 Answers2

44

Here is how you can create a simple docx file with POI :

XWPFDocument document = new XWPFDocument();
XWPFParagraph tmpParagraph = document.createParagraph();
XWPFRun tmpRun = tmpParagraph.createRun();
tmpRun.setText("LALALALAALALAAAA");
tmpRun.setFontSize(18);
document.write(new FileOutputStream(new File("yourpathhere")));
document.close();
Valentin Rocher
  • 11,667
  • 45
  • 59
  • Can we please delete this question? It's too embarassing... Thanks Valentin! – guerda Apr 07 '10 at 13:34
  • 8
    haha don't worry, there's plenty of sillier questions here (and POI is not really that easy to use) – Valentin Rocher Apr 07 '10 at 14:33
  • 4
    It just helped me out, while I wasn't having your error, it's a great Google search result for a very simple example of using POI. – cgp Nov 03 '10 at 13:50
  • For me it doesn't work, it produces a document with screwed encoding. No matter if the stream is UTF-8 or cp1250, the document in LibreOffice (OpenOffice) isn't decoded properly – lisak May 26 '11 at 14:31
  • From where do i get the library for this? – AkashG Jan 30 '14 at 06:45
  • and how to execute this in android, i mean is it possible to create docx file at run-time through the above code in android? – AkashG Jan 30 '14 at 07:01
  • Have you even tried googling for "Apache poi"? http://stackoverflow.com/search?q=android+poi – guerda Jan 30 '14 at 14:48
  • @guerda Hey I have POI version 3.10 and its not working for XWPF. Please tell me which exact JAR file I have to use for this. – SoulRayder Feb 19 '14 at 04:30
  • @guerda : nvm: had not added the ooxml jar.. working now.. thanks anyways – SoulRayder Feb 19 '14 at 04:34
1
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 DocFile {   
    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("C:\\Users\\amitabh\\Pictures\\pics\\"+filename + ".doc"));   
       document.write(fos);   
       fos.close();   
    }   
    public static void main(String[] args) throws Exception {   
         DocFile app = new DocFile();   
         app.newWordDoc("testfile", "Hi hw r u?");   

    }   
  }   
Amitabh Ranjan
  • 1,500
  • 3
  • 23
  • 39