Мой кусок кода пытается прочитать файл doc / docx. При вызове конструктора WordExtractor возникает ошибка двусмысленности

1

Я скопировал код ниже с веб-сайта. Он читает файл.doc/.docx на Java, используя пакет Apache POI.

WordExtractor we = new WordExtractor(doc); дает следующую ошибку: reference to WordExtractor is ambiguous. Both constructor WordExtractor(POIFSFileSystem) in WordExtractor and WordExtractor(HWPFDocument) in WordExtractor match. 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");

}

}

  • 1
    new WordExtractor((HWPFDocument)doc) должен сделать new WordExtractor((HWPFDocument)doc) дело.
  • 0
    @ theV0ID: я сделал это. Та же ошибка
Показать ещё 2 комментария
Теги:
apache-poi

2 ответа

1
Лучший ответ

Спасибо вам за помощь ! :)

Любой, у кого есть аналогичная проблема, может использовать следующие файлы 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 от → http://poi.apache.org/download.html

dom4j-1.6.jar от → http://www.java2s.com/Code/Jar/d/Downloaddom4j16jar.htm

3

Привет, этот код работает, вы можете написать этот,

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());

        }

    }
  • 0
    Та же ошибка Я пропускаю какой-либо пакет для импорта?
  • 0
    @AishwaryaTiwari Я использовал poi-3.11. Какой из них вы используете.
Показать ещё 1 комментарий

Ещё вопросы

Сообщество Overcoder
Наверх
Меню