如何使用POI api读取java中的doc和docx文件

时间:2013-07-12 14:22:36

标签: java apache-poi docx doc

我正在尝试阅读doc和docx文件。这是代码:

  static String distination="E:\\         
  static String docFileName="Requirements.docx";
 public static void main(String[] args) throws FileNotFoundException, IOException {
    // TODO code application logic here
    ReadFile rf= new ReadFile();
    rf.ReadFileParagraph(distination+docFileName);


  }
  public void ReadFileParagraph(String path) throws FileNotFoundException, IOException
    {
        FileInputStream fis;
        File file = new File(path);
        fis=new FileInputStream(file.getAbsolutePath());
           String filename=file.getName();

        String fileExtension=fileExtension(path);
        if(fileExtension.equals("doc"))
        {
             HWPFDocument document=new HWPFDocument(fis);
             WordExtractor DocExtractor = new WordExtractor(document);
             ReadDocFile(DocExtractor,filename);

        }
        else if(fileExtension.equals("docx"))
        {

            XWPFDocument documentX = new XWPFDocument(fis);            
            List<XWPFParagraph> pera =documentX.getParagraphs();
            ReadDocXFile(pera,filename);
        }
        else
        {
            System.out.println("format does not match");
        }

    }
    public void ReadDocFile(WordExtractor extractor,String filename)
    {

        for (String paragraph : extractor.getParagraphText()) {
            System.out.println("Peragraph: "+paragraph);
        }
    }
    public void ReadDocXFile(List<XWPFParagraph> extractor,String filename)
    {

        for (XWPFParagraph paragraph : extractor) {
          System.out.println("Question: "+paragraph.getParagraphText());
        }

    }
    public String fileExtension(String filename)
    {

       String extension = filename.substring(filename.lastIndexOf(".") + 1, filename.length());
       return extension;
    }

当我想阅读docx文件时,此代码会出现异常:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlException
    at l3s.readfiles.db.ReadFile.ReadFileParagraph(ReadFile.java:52)
    at autometictagdetection.TagDetection.main(TagDetection.java:36)
Caused by: java.lang.ClassNotFoundException: org.apache.xmlbeans.XmlException
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    ... 2 more
Java Result: 1

另一个问题是当我想要读取一个Doc文件时,它会很好地读取一些文件,但对于某些文件,它会给出一个类似的例外

    Exception in thread "main" org.apache.poi.hwpf.OldWordFileFormatException: The               document is too old - Word 95 or older. Try HWPFOldDocument instead?
    at org.apache.poi.hwpf.HWPFDocument.<init>(HWPFDocument.java:222)
    at org.apache.poi.hwpf.HWPFDocument.<init>(HWPFDocument.java:186)
    at org.apache.poi.hwpf.HWPFDocument.<init>(HWPFDocument.java:174)
    at l3s.readfiles.db.ReadFile.ReadFileParagraph(ReadFile.java:44)
    at autometictagdetection.TagDetection.main(TagDetection.java:36)
Java Result: 1

我在http://poi.apache.org/hwpf/index.html中看到POI API支持单词6和单词95。 请有人能解决这两个问题吗?

2 个答案:

答案 0 :(得分:0)

重新发布第一期,我猜您需要参考项目中的依赖性。

我想:

poi-ooxml-schemas xmlbeans,位于poi-ooxml-schemas-version-yyyymmdd.jar

(来自Apache POI page)。

Here是Apache XMLBeans页面。

我无法列出您需要的每个库,但您可以通过maven找出...

答案 1 :(得分:0)

需要核心maven依赖项,这是问题1的解决方案

<dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.15</version>
        </dependency>
        <!-- For .DOCX FILES -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.15</version>
        </dependency>
       <!-- For .DOC FILES -->
        <dependency>
           <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>3.9</version>
        </dependency>
  

对于问题2,从原始源代码开始,POI似乎不支持太老的文档

  /**
   * This constructor loads a Word document from a specific point
   *  in a POIFSFileSystem, probably not the default.
   * Used typically to open embeded documents.
   *
   * @param directory The DirectoryNode that contains the Word document.
   * @throws IOException If there is an unexpected IOException from the passed
   *         in POIFSFileSystem.
   */
  public HWPFDocument(DirectoryNode directory) throws IOException
  {
    // Load the main stream and FIB
    // Also handles HPSF bits
    super(directory);

    // Is this document too old for us?
    if(_fib.getFibBase().getNFib() < 106) {
        throw new OldWordFileFormatException("The document is too old - Word 95 or older. Try HWPFOldDocument instead?");
    }

Source code for HWPFDocument