【问题标题】:How to read docx file content in java api using poi jar如何使用 poi jar 在 java api 中读取 docx 文件内容
【发布时间】:2013-05-04 19:27:14
【问题描述】:

我已经读完了 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

请帮忙 谢谢

【问题讨论】:

    标签: java apache-poi docx readfile


    【解决方案1】:

    Apache POI FAQ! 对此进行了介绍!您想要的条目是 我正在使用 poi-ooxml-schemas jar,但我的代码因“java.lang.NoClassDefFoundError: org/openxmlformats/schemas/something”而失败强>

    简短的回答是将poi-ooxml-schemas jar 切换为完整的ooxml-schemas-1.1 jar。完整答案是given in the FAQ

    【讨论】:

    • 非常感谢。按照您的建议更换罐子后,我得到了输出。它正在工作!!!!。如果您对如何解析 docx 内容有任何线索,请帮助我。我必须在文件中找到确切的单词并需要对其进行修改。
    • 查看 Apache POI 附带的示例,以及 Apache POI 中的文本提取器,它们应该为您提供许多类似的代码供您查看。如果这没有帮助,您需要提出一个新问题
    • 另外,如果这个答案已经为您解决了问题,请点击答案旁边的勾“接受”它以将其标记为正确
    【解决方案2】:

    读取excel或docx文件,如果你想解决错误,你需要添加所有jar,然后你不会得到任何错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-27
      • 1970-01-01
      • 2013-06-17
      • 1970-01-01
      • 2019-11-28
      • 2012-07-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多