2010-04-07 4 views
26

Je cherche un exemple de code simple ou un tutoriel complet comment créer un fichier docx avec Apache POI et son openxml4j sous-jacent.Comment puis-je créer un simple fichier docx avec Apache POI?

J'ai essayé le code suivant (avec un lot de l'aide de l'Assistant de contenu, merci Eclipse!) Mais le code ne fonctionne pas correctement.

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); 

L'exception levée est la suivante:

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) 

Est-ce que quelqu'un peut me aider à mes besoins (très simples)?

+0

D'où puis-je obtenir la bibliothèque pour cela? – AkashG

Répondre

31

Voici comment vous pouvez créer un simple fichier docx avec 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(); 
+0

Pouvons-nous s'il vous plaît supprimer cette question? C'est trop embarrassant ... Merci Valentin! – guerda

+7

haha ​​ne vous inquiétez pas, il ya beaucoup de questions plus sillier ici (et POI n'est pas vraiment facile à utiliser) –

+3

Il m'a juste aidé, alors que je n'avais pas votre erreur, c'est un excellent résultat de recherche Google pour un très simple exemple d'utilisation de POI. – cgp

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?"); 

    } 
    } 
Questions connexes