java读取word并搜索内容

package com.data.util;

import org.apache.poi.POIXMLDocument;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class FileContentSearch2 {

    private static String FileName = "D:\\clouddataword";// 文件夹名字

    private static String FileEnd = "docx";// 文件名称后缀 txt sql bat

    private static String SearchStr = "哈";//要查找的字符串

    private static Boolean IngronCase = true;// 是否区分大小写

    private static List<String> pathlist = new ArrayList<>();

    public static void main(String[] args) {


        //解析docx模板并获取document对象
        XWPFDocument document;
        //获取XWPFRun对象输出整个文本内容
        StringBuffer tempText = new StringBuffer();
        pathlist = getFileList(FileName);
        for (int k = 0; k < pathlist.size(); k++) {
            File file = new File(pathlist.get(k));
            if (file.exists()) {
                try {
                    document = new XWPFDocument(POIXMLDocument.openPackage(file.toString()));
                    //获取整个文本对象
                    List<XWPFParagraph> allParagraph = document.getParagraphs();
                    for (XWPFParagraph xwpfParagraph : allParagraph) {
                        List<XWPFRun> runList = xwpfParagraph.getRuns();
                        for (XWPFRun xwpfRun : runList) {
                            tempText.append(xwpfRun.toString());
                            if (tempText != null) {
                                if (IngronCase) {
                                    if (tempText.toString().contains(SearchStr)) {
                                        System.out.println("找到了"+tempText+"文件"+file.getName());
                                        break;
                                    }
                                } else {
                                    if (tempText.toString().toLowerCase().contains(SearchStr.toLowerCase())) {
                                        System.out.println("找到了");
                                        break;
                                    }
                                }
                            }
                            tempText.setLength(0);
                        }
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }        //文档内容
                //return tempText.toString();
            }
        }
    }

    public static List<String> getFileList(String strPath) {
        File dir = new File(strPath);
        File[] files = dir.listFiles(); // 该文件目录下文件全部放入数组
        if (files != null) {
            for (int i = 0; i < files.length; i++) {
                String fileName = files[i].getName();
                if (files[i].isDirectory()) { // 判断是文件还是文件夹
                    getFileList(files[i].getAbsolutePath()); // 获取文件绝对路径
                } else if (fileName.endsWith("." + FileEnd)) { // 判断文件名是否以.avi结尾
                    String strFileName = files[i].getAbsolutePath();
                    pathlist.add(strFileName);
                } else {
                    continue;
                }
            }

        }
        return pathlist;
    }
}

你可能感兴趣的:(笔记)