我正在尋找一個簡單的示例代碼或完整的教程,瞭解如何使用Apache POI及其基礎openxml4j
創建docx
文件。如何使用Apache POI創建簡單的docx文件?
我試了下面的代碼(很多來自內容助手的幫助,感謝Eclipse!),但代碼無法正常工作。
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);
拋出的異常如下:
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)
是否有人能幫助我與我的(很簡單)的要求?
從哪裏獲得這個圖書館? – AkashG 2014-01-30 06:45:23