2013-05-10 62 views
3

我已經完成閱讀doc文件,現在我正在嘗試閱讀docx文件內容。當我搜索我發現很多的示例代碼時,沒有任何工作。檢查代碼以供參考...如何閱讀使用poi jar的java api中的docx文件內容

import java.io.*; 
import org.apache.poi.xwpf.usermodel.XWPFDocument; 
import org.apache.poi.xwpf.extractor.XWPFWordExtractor; 
import com.itextpdf.text.pdf.PdfWriter; 
import com.itextpdf.text.Document; 
import com.itextpdf.text.Paragraph; 

public class createPdfForDocx { 

public static void main(String[] args) { 
InputStream fs = null; 
    Document document = new Document(); 
    XWPFWordExtractor extractor = null ; 

try { 

    fs = new FileInputStream("C:\\DATASTORE\\test.docx"); 
    //XWPFDocument hdoc=new XWPFDocument(fs); 
    XWPFDocument hdoc=new XWPFDocument(OPCPackage.open(fs)); 
    //XWPFDocument hdoc=new XWPFDocument(fs); 
    extractor = new XWPFWordExtractor(hdoc); 
    OutputStream fileOutput = new FileOutputStream(new  File("C:/DATASTORE/test.pdf")); 
    PdfWriter.getInstance(document, fileOutput); 
    document.open(); 
    String fileData=extractor.getText(); 
    System.out.println(fileData); 
    document.add(new Paragraph(fileData)); 
    System.out.println(" pdf document created"); 
     } catch(IOException e) { 
      System.out.println("IO Exception"); 
      e.printStackTrace(); 
      } catch(Exception ex) { 
      ex.printStackTrace(); 
      }finally { 
       document.close(); 
      } 
}//end of main() 
}//end of class 

上面的代碼我得到以下異常:

org.apache.poi.POIXMLException: java.lang.reflect.InvocationTargetException 
at org.apache.poi.xwpf.usermodel.XWPFFactory.createDocumentPart(XWPFFactory.java:60) 
at org.apache.poi.POIXMLDocumentPart.read(POIXMLDocumentPart.java:277) 
at org.apache.poi.POIXMLDocument.load(POIXMLDocument.java:186) 
at org.apache.poi.xwpf.usermodel.XWPFDocument.<init>(XWPFDocument.java:107) 
at pagecode.createPdfForDocx.main(createPdfForDocx.java:20) 
Caused by: java.lang.reflect.InvocationTargetException 
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:67) 
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 
at java.lang.reflect.Constructor.newInstance(Constructor.java:521) 
at org.apache.poi.xwpf.usermodel.XWPFFactory.createDocumentPart(XWPFFactory.java:58) 
... 4 more 
Caused by: java.lang.NoSuchMethodError: org/openxmlformats/schemas/wordprocessingml/x2006/main/CTStyles.getStyleList()Ljava/util/List; 
at org.apache.poi.xwpf.usermodel.XWPFStyles.onDocumentRead(XWPFStyles.java:78) 
at org.apache.poi.xwpf.usermodel.XWPFStyles.<init>(XWPFStyles.java:59) 
... 9 more 

請幫 謝謝

回答

6

這是包括在Apache POI FAQ!你想要的條目是我使用的是POI-OOXML-模式罐子,但我的代碼與失敗「java.lang.NoClassDefFoundError:組織/ openxmlformats /模式/ 東西

簡短的回答是切換poi-ooxml-schemas罐子以供完整的ooxml-schemas-1.1罐子使用。完整的答案是given in the FAQ

+0

非常感謝。根據你的建議更換罐子後,我得到的輸出。它的工作!如果您對如何解析docx內容有任何線索,請幫助我。我必須在文件中找到確切的單詞並需要修改它。 – nagesh 2013-05-13 05:56:18

+0

查看Apache POI附帶的示例以及Apache POI中的文本提取器,它們應該給你很多類似的代碼來查看。如果這沒有幫助,您需要提出一個新問題 – Gagravarr 2013-05-13 09:55:26

+0

另外,如果此答案已爲您解決問題,請單擊答案旁邊的複選框將其「接受」以標記爲正確 – Gagravarr 2013-05-13 09:56:02

1

對於閱讀Excel或DOCX文件如果你想解決錯誤,你需要添加所有的罐子,那麼你不會得到任何錯誤。