簡體   English   中英

如何使用poi jar讀取java api中的docx文件內容

[英]How to read docx file content in java api using poi jar

我已經閱讀了doc文件,現在我正在嘗試閱讀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

請幫忙謝謝

這在Apache POI FAQ中有所介紹! 你想要的條目是我正在使用poi-ooxml-schemas jar,但我的代碼失敗了“java.lang.NoClassDefFoundError:org / openxmlformats / schemas / something

簡短的回答是為完整的ooxml-schemas-1.1 jar切換poi-ooxml-schemas jar。 常見問題解答中給出了完整的答案

要讀取excels或docx文件,如果你想解決錯誤,你需要添加所有的罐子然后你不會得到任何錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM