1、所需jar包
2、代码示例
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;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
public class WordDocumentKeywordChecker {
public static void main(String[] args) {
String filePath = "path/to/your/document.docx"; // 可以是.doc或.docx
String keyword = "your_keyword";
try {
boolean containsKeyword = checkDocumentForKeyword(filePath, keyword);
System.out.println("Document contains keyword: " + containsKeyword);
} catch (IOException e) {
e.printStackTrace();
}
}
public static boolean checkDocumentForKeyword(String filePath, String keyword) throws IOException {
File file = new File(filePath);
String fileName = file.getName().toLowerCase();
if (fileName.endsWith(".doc")) {
return checkDocForKeyword(filePath, keyword);
} else if (fileName.endsWith(".docx")) {
return checkDocxForKeyword(filePath, keyword);
} else {
throw new IllegalArgumentException("文件类型不支持: " + fileName);
}
}
private static boolean checkDocForKeyword(String filePath, String keyword) throws IOException {
FileInputStream fis = new FileInputStream(filePath);
HWPFDocument document = new HWPFDocument(fis);
WordExtractor extractor = new WordExtractor(document);
String[] paragraphs = extractor.getParagraphText();
for (String paragraph : paragraphs) {
if (paragraph != null && paragraph.contains(keyword)) {
fis.close();
return true;
}
}
fis.close(); // 不要忘记关闭文件输入流
return false;
}
private static boolean checkDocxForKeyword(String filePath, String keyword) throws IOException {
FileInputStream fis = new FileInputStream(filePath);
XWPFDocument document = new XWPFDocument(fis);
List<XWPFParagraph> paragraphs = document.getParagraphs();
for (XWPFParagraph paragraph : paragraphs) {
String text = paragraph.getText();
if (text != null && text.contains(keyword)) {
fis.close();
return true;
}
}
fis.close(); // 如果没有找到关键字,也要确保关闭文件输入流
return false;
}
}