我从网站上复制了以下代码。它使用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");
}
}
答案 0 :(得分: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());
}
}
答案 1 :(得分:1)
谢谢你的帮助! :)
任何有类似问题的人都可以使用以下jar文件来获取上述代码。
POI-3.11-20141221.jar POI-OOXML-3.11-20141221.jar POI-OOXML-架构 - 3.11-20141221.jar POI暂存器,3.11-20141221.jar XMLBeans的-2.6.0.jar 来自 - &gt; http://poi.apache.org/download.html
DOM4J-1.6.jar 来自 - &gt; http://www.java2s.com/Code/Jar/d/Downloaddom4j16jar.htm