2010-04-07 17 views
26

Sto cercando un codice di esempio semplice o un'esercitazione completa su come creare un file docx con POI Apache e il suo sottostante openxml4j.Come posso creare un semplice file docx con POI Apache?

Ho provato il seguente codice (con un sacco di aiuto da Content Assist, grazie Eclipse!) Ma il codice non funziona correttamente.

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'eccezione generata è la seguente:

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) 

Qualcuno mi può aiutare con le mie esigenze (Really Simple)?

+0

Da dove posso ottenere la libreria per questo? – AkashG

risposta

31

Ecco come si può creare un file docx semplice con 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

Possiamo cancellare questa domanda? È troppo imbarazzante ... Grazie Valentin! – guerda

+7

haha ​​non ti preoccupare, ci sono un sacco di domande stupide qui (e POI non è così facile da usare) –

+3

Mi ha semplicemente aiutato, mentre non avevo il tuo errore, è un ottimo risultato di ricerca di Google per un molto semplice esempio di utilizzo del 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?"); 

    } 
    } 
Problemi correlati