【问题标题】:Error Using HWPFDocument [closed]使用 HWPFDocument 时出错 [关闭]
【发布时间】:2017-02-24 03:52:16
【问题描述】:

我一直在尝试读取 .doc 和 .docx 文件并将文件中的文本分配给 java 中的 String 变量,但我一直遇到错误

线程“main”java.lang.Error 中的异常:未解决的编译问题: org.apache.poi.poifs.filesystem.POIFSFileSystem 类型无法解析。它是从所需的 .class 文件中间接引用的 org.apache.poi.poifs.filesystem.DirectoryNode 类型无法解析。它是从所需的 .class 文件中间接引用的

我有以下代码来测试程序

 import java.io.*;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;

public class ReadDocFile
{
    public static void main(String[] args)
    {
        File file = null;
        WordExtractor extractor = null;
        try
        {

            file = new File("c:\\test.doc");
            FileInputStream fis = new FileInputStream(file.getAbsolutePath());
            HWPFDocument document = new HWPFDocument(fis);
            extractor = new WordExtractor(document);
            String[] fileData = extractor.getParagraphText();
            for (int i = 0; i < fileData.length; i++)
            {
                if (fileData[i] != null)
                    System.out.println(fileData[i]);
            }
        }
        catch (Exception exep)
        {
            exep.printStackTrace();
        }
    }
}

我从

下载了一个 .jar 文件

https://mvnrepository.com/artifact/org.apache.poi/poi-scratchpad/3.9

我认为我导入的 .jar 文件不完整。如果是这样,谁能给我一个完整库的链接?

【问题讨论】:

    标签: java apache-poi


    【解决方案1】:

    您需要将 poi-3.15.jar 包含到您的类路径中。

    您可以找到所有 poi jars & dependencies 单个下载here

    如果你使用的是ma​​ven

       <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.15</version>
        </dependency>
    

    【讨论】:

    • 您好 Sundararaj Govindasamy,
    • 我下载了 poi-3.15 文件夹,但那里没有 .jar 文件,所以我真的不知道该怎么办。对不起,我是这个东西的菜鸟。如何将 poi 库导入 eclipse?
    • 如果您点击我回答中的下载链接会怎样?它将下载一个包含所有内容的 zip。要将下载的 jar 添加到 eclipse 中,您可以参考此 *.com/a/11463590/1401019 如果有帮助,您可以投票接受此答案
    • 下载的 .zip 文件不包含任何 .jar 文件,所以我不知道如何使用它。
    • 我对 org.apache.poi v. 3.15 和 4.1.0 有同样的问题。它在导入最后一个(今天的)库版本时对我有用 - 4.1.1 重建 gradle 项目对我不起作用。对我来说奇怪的是,我尝试使用 cuonglv.com/2019/03/20/… 中提到的相同库版本,但我不能使用提到的 v 3.15 中的相同类。尝试导入 4.1.1。