我从网站复制了以下代码。它使用Apache POI包读取Java中的.doc / .docx文件。

WordExtractor we = new WordExtractor(doc);给出以下错误:
reference to WordExtractor is ambiguous. Both constructor WordExtractor(POIFSFileSystem) in WordExtractor and WordExtractor(HWPFDocument) in WordExtractor match.

抱歉,您有任何愚蠢的错误,我是第一次阅读.doc。
谢谢你们 ! :)

码:

package testdeployment;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;


/**
 *
 * @author Aishwarya
 */
public class MsFileReader {
public static void readDocFile(String fileName) {

    try {
        File file = new File(fileName);
        FileInputStream fis = new FileInputStream(file.getAbsolutePath());

        HWPFDocument doc = new HWPFDocument(fis);

        WordExtractor we = new WordExtractor(doc);

        String[] paragraphs = we.getParagraphText();

        System.out.println("Total no of paragraph "+paragraphs.length);
        for (String para : paragraphs) {
            System.out.println(para.toString());
        }
        fis.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

public static void readDocxFile(String fileName) {

    try {
        File file = new File(fileName);
        FileInputStream fis = new FileInputStream(file.getAbsolutePath());

        XWPFDocument document = new XWPFDocument(fis);

        List<XWPFParagraph> paragraphs = document.getParagraphs();

        System.out.println("Total no of paragraph "+paragraphs.size());
        for (XWPFParagraph para : paragraphs) {
            System.out.println(para.getText());
        }
        fis.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static void main(String[] 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());

        }

    }

关于java - 我的一段代码试图读取doc/docx文件。调用WordExtractor构造函数会产生歧义错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27659223/

10-08 22:54