Java 我的代码试图读取doc/docx文件。调用WordExtractor构造函数时出现歧义错误

Java 我的代码试图读取doc/docx文件。调用WordExtractor构造函数时出现歧义错误,java,apache-poi,Java,Apache Poi,我从一个网站上复制了下面的代码。它使用ApachePOI包读取Java中的.doc/.docx文件 WordExtractor我们=新的WordExtractor(doc)给出以下错误: 对WordExtractor的引用不明确。WordExtractor中的构造函数WordExtractor(POIFSFileSystem)和WordExtractor中的WordExtractor(HWPDocument)都匹配。 对不起,我犯了一些愚蠢的错误,我正在做这件事。 谢谢大家!:) 代码: 包测试

我从一个网站上复制了下面的代码。它使用ApachePOI包读取Java中的.doc/.docx文件

WordExtractor我们=新的WordExtractor(doc)给出以下错误:
对WordExtractor的引用不明确。WordExtractor中的构造函数WordExtractor(POIFSFileSystem)和WordExtractor中的WordExtractor(HWPDocument)都匹配。

对不起,我犯了一些愚蠢的错误,我正在做这件事。 谢谢大家!:)

代码:

包测试部署;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.IOException;
导入java.util.List;
导入org.apache.poi.hwpf.hwpf文档;
导入org.apache.poi.hwpf.extractor.WordExtractor;
导入org.apache.poi.xwpf.usermodel.XWPFDocument;
导入org.apache.poi.xwpf.usermodel.XWPFParagraph;
/**
*
*@作者Aishwarya
*/
公共类MsFileReader{
公共静态void readDocFile(字符串文件名){
试一试{
文件=新文件(文件名);
FileInputStream fis=新的FileInputStream(file.getAbsolutePath());
HWPF文件文件=新的HWPF文件(fis);
WordExtractor we=新的WordExtractor(文档);
String[]段落=we.getParagraphText();
System.out.println(“段落总数”+段落长度);
用于(字符串段:段落){
System.out.println(第toString()段);
}
fis.close();
}捕获(例外e){
e、 printStackTrace();
}
}
公共静态void readDocxFile(字符串文件名){
试一试{
文件=新文件(文件名);
FileInputStream fis=新的FileInputStream(file.getAbsolutePath());
XWPF文件=新的XWPF文件(fis);
列表段落=document.getPages();
System.out.println(“段落总数”+段落大小());
对于(XWPF段落:段落){
System.out.println(para.getText());
}
fis.close();
}捕获(例外e){
e、 printStackTrace();
}
}
公共静态void main(字符串[]args){
readDocxFile(“C:\\Test.docx”);
readDocFile(“C:\\Test.doc”);
}

}

您好,这段代码正在运行,您可以编写这段代码

public static void readDocxFile(String fileName) {

    try {
        File file = new File(fileName);
        POIFSFileSystem fs = null;
        fs = new POIFSFileSystem(new FileInputStream(file.getAbsolutePath()));
        HWPFDocument doc = new HWPFDocument(fs);
        readParagraphs(doc);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static void readParagraphs(HWPFDocument doc) throws Exception{
        WordExtractor we = new WordExtractor(doc);

        /**Get the total number of paragraphs**/
        String[] paragraphs = we.getParagraphText();
        System.out.println("Total Paragraphs: "+paragraphs.length);

        for (int i = 0; i < paragraphs.length; i++) {

            System.out.println("Length of paragraph "+(i +1)+": "+ paragraphs[i].length());
            System.out.println(paragraphs[i].toString());

        }

    }
publicstaticvoidreaddocxfile(字符串文件名){
试一试{
文件=新文件(文件名);
POIFSFS=null;
fs=新的POIFSF文件系统(新的FileInputStream(file.getAbsolutePath());
HWPF文件文件=新的HWPF文件(fs);
阅读段落(doc);
}捕获(例外e){
e、 printStackTrace();
}
}
公共静态无效读取段落(HWPFDocument文档)引发异常{
WordExtractor we=新的WordExtractor(文档);
/**获取段落总数**/
String[]段落=we.getParagraphText();
System.out.println(“总段落:“+段落长度”);
对于(int i=0;i
您好,此代码正在运行,您可以编写此代码

public static void readDocxFile(String fileName) {

    try {
        File file = new File(fileName);
        POIFSFileSystem fs = null;
        fs = new POIFSFileSystem(new FileInputStream(file.getAbsolutePath()));
        HWPFDocument doc = new HWPFDocument(fs);
        readParagraphs(doc);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static void readParagraphs(HWPFDocument doc) throws Exception{
        WordExtractor we = new WordExtractor(doc);

        /**Get the total number of paragraphs**/
        String[] paragraphs = we.getParagraphText();
        System.out.println("Total Paragraphs: "+paragraphs.length);

        for (int i = 0; i < paragraphs.length; i++) {

            System.out.println("Length of paragraph "+(i +1)+": "+ paragraphs[i].length());
            System.out.println(paragraphs[i].toString());

        }

    }
publicstaticvoidreaddocxfile(字符串文件名){
试一试{
文件=新文件(文件名);
POIFSFS=null;
fs=新的POIFSF文件系统(新的FileInputStream(file.getAbsolutePath());
HWPF文件文件=新的HWPF文件(fs);
阅读段落(doc);
}捕获(例外e){
e、 printStackTrace();
}
}
公共静态无效读取段落(HWPFDocument文档)引发异常{
WordExtractor we=新的WordExtractor(文档);
/**获取段落总数**/
String[]段落=we.getParagraphText();
System.out.println(“总段落:“+段落长度”);
对于(int i=0;i
任何有类似问题的人都可以使用以下jar文件来编写上述代码

  • poi-3.11-20141221.jar

  • poi-ooxml-3.11-20141221.jar

  • poi-ooxml-schemas-3.11-20141221.jar

  • poi-scratchpad-3.11-20141221.jar

  • xmlbeans-2.6.0.jar

    发件人:

  • dom4j-1.6.jar
    from:


任何有类似问题的人都可以使用以下jar文件来编写上述代码

  • poi-3.11-20141221.jar

  • poi-ooxml-3.11-20141221.jar

  • poi-ooxml-schemas-3.11-20141221.jar

  • poi-scratchpad-3.11-20141221.jar

  • xmlbeans-2.6.0.jar

    发件人:

  • dom4j-1.6.jar
    from:


新的WordExtractor((HWPFDocument)doc)
应该可以做到。@theV0ID:我做到了。同样的错误。你一定错过了什么。根据在线提供的类参考,类
HWPFDocument
甚至不支持
POIFSFileSystem
接口。谢谢大家的帮助!我解决了这个问题。缺少一些导入。
new WordExtractor((HWPFDocument)doc)
应该可以解决这个问题。@theV0ID:我做到了。同样的错误。你一定错过了什么。根据在线提供的类参考,类
HWPFDocument
甚至不支持
POIFSFileSystem
接口。谢谢大家的帮助!我解决了这个问题。缺少一些导入。相同的错误。我是否缺少任何要导入的包???@AishwaryaTiwari我使用poi-3.11。您使用的是哪一个。相同的错误。我是否缺少任何要导入的包???@AishwaryaTiwari我使用poi-3.11。哪一个