Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a simple docx file with Apache POI?

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?

like image 485
guerda Avatar asked Apr 07 '10 12:04

guerda


People also ask

What is Apache POI DOCX?

What is Apache POI? Apache POI is a popular API that allows programmers to create, modify, and display MS-Office files using Java programs. It is an open source library developed and distributed by Apache Software Foundation to design or modify MS-Office files using Java program.


2 Answers

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(); 
like image 144
Valentin Rocher Avatar answered Sep 17 '22 11:09

Valentin Rocher


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?");         }      }    
like image 28
Amitabh Ranjan Avatar answered Sep 16 '22 11:09

Amitabh Ranjan