2010-04-07 45 views
26

Estoy buscando un código de ejemplo simple o un tutorial completo sobre cómo crear un archivo docx con Apache POI y su subyacente openxml4j.¿Cómo puedo crear un archivo docx simple con Apache POI?

He intentado el siguiente código (con mucho de la ayuda de Content Assist, gracias Eclipse!) Pero el código no funciona correctamente.

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

La excepción lanzada es la siguiente:

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) 

¿Alguien puede ayudarme con mis necesidades (muy simples)?

+0

De dónde puedo obtener la biblioteca para esto? – AkashG

Respuesta

31

Aquí es cómo se puede crear un archivo docx sencillo con PDI:

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

¿Podemos por favor eliminar esta pregunta? Es demasiado vergonzoso ... Gracias Valentin! – guerda

+7

jaja no se preocupe, hay un montón de preguntas más tontas aquí (y POI no es tan fácil de usar) –

+3

Me ayudó, aunque no tuve su error, es un gran resultado de búsqueda de Google para un muy simple ejemplo de uso 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?"); 

    } 
    } 
Cuestiones relacionadas