java - 如何使用poi jar在java api中读取docx文件内容

标签 java apache-poi docx readfile

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

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

关于java - 如何使用poi jar在java api中读取docx文件内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16476711/

相关文章:

java - 我需要将多个图像下载到目录,以便可以离线访问内容

excel - 格式化 lucee 电子表格时出错 : The maximum number of cell styles was exceeded

java - 将 VBA 脚本添加到 Office Word 文档

ruby - 在 Ruby 中将 DOCX 转换为 DOC

Java:记住递归方法中上一步的结果

java - 尽管有最大长度,但由于没有最大长度的后视而抛出模式语法异常

java - 在没有项目的情况下在 Eclipse 中导航导入

java - 如何在java中使用apache poi更新docx文件的元数据?

java - Apache POI 在空电子表格中看到列?

ms-word - 如何从 Word 2007 文档链接到 chm 文件主题?