首页 > 其他分享 >Excel word pdf查找

Excel word pdf查找

时间:2023-11-13 14:58:46浏览次数:27  
标签:word String fileMap beginKey Excel System import println pdf

import org.apache.commons.lang.StringUtils;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.poi.ooxml.POIXMLDocument;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xssf.extractor.XSSFExcelExtractor;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

/**

  • @author

  • @title: aa

  • @projectName

  • @description: TODO

  • @date 2022/10/14 19:27
    */
    public class OfficeTextSearch {

    private static final String SUCCESS = "success";

    private static final String CONTINUE = "continue";

    private static final String EXIT = "exit";

    public static void main(String[] args) {
    String pdfFileDir = "D:\用户\luofu704\Desktop\1031";
    String excelFileDir = "D:\fintchFile\allexcel";
    String wordFileDir = "D:\fintchFile\word";
    //默认前后50个字符
    int percount = 200;
    int aftercount = 200;
    //查找关键字
    String beginKey = null;
    //读取所有文件放到map中 key为path value 为 内容
    LinkedHashMap<String, String> fileMap = new LinkedHashMap<>();
    //读取pdf文件
    //readPDFFileMap(fileMap,pdfFileDir);
    //读取excel文件
    //readExcelFileMap(fileMap,excelFileDir);
    //读取word文件
    readWordFileMap(fileMap,wordFileDir);
    while(true){
    Scanner scanner = new Scanner(System.in);
    if(StringUtils.isEmpty(beginKey)){
    System.err.println("请输入查找的关键字(或者输入exit退出):");
    beginKey = scanner.nextLine();
    }
    if(StringUtils.isEmpty(beginKey)){
    System.err.println("请输入查找的关键字,关键字不能为空:");
    beginKey = scanner.nextLine();
    }
    if(EXIT.equals(beginKey)){
    return;
    }
    if(StringUtils.isNotBlank(beginKey)){
    findKeyContent(percount, aftercount, beginKey, fileMap);
    }
    //清除关键字
    beginKey = null;
    System.err.println(">>>>>>>>>>>>>>>>>>>>>>>>下一题<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
    }
    }

    public static void findKeyContent(int percount, int aftercount, String beginKey, LinkedHashMap<String, String> fileMap) {
    Set<Map.Entry<String, String>> entries = fileMap.entrySet();
    for (Map.Entry<String, String> entry : entries) {
    String message = getByStartAndEndKey(entry.getValue(), beginKey, entry.getKey(),percount,aftercount);
    if(SUCCESS.equals(message)){
    return;
    }else if(CONTINUE.equals(message)) {
    continue;
    }
    }
    }
    public static String getByStartAndEndKey(String content, String beginKey,String filePath,int percount,int aftercount) {
    //beginKey 开始关键字字符串
    int begin = content.indexOf(beginKey);
    if (begin > 0) {
    int strBegin = 0;
    int strEnd = content.length();
    //输出前后个字符串
    if (begin > percount) {
    //前10个字符
    strBegin = begin - percount;
    }
    if (begin + aftercount < strEnd) {
    strEnd = begin + aftercount;
    }
    String matchContent = content.substring(strBegin, strEnd);
    System.out.println();
    System.err.println("========================= 匹配文件: " + filePath + "================================================== ");
    System.out.println(matchContent);
    Scanner scanner = new Scanner(System.in);
    System.err.println("是否继续往下查找y/n:");
    String keyborad = scanner.nextLine();
    if(StringUtils.isEmpty(keyborad)){
    System.err.println("请输入y/n:");
    keyborad = scanner.nextLine();
    }
    if ("Y".equals(keyborad.toUpperCase())) {
    content = content.substring(begin + 1);
    String byStartAndEndKey = getByStartAndEndKey(content, beginKey,filePath, percount, aftercount);
    //多次搜索跳出循环
    if(SUCCESS.equals(byStartAndEndKey)){
    return SUCCESS;
    }
    } else if ("N".equals(keyborad.toUpperCase())) {
    return SUCCESS;
    }
    //继续下一个文件的检索
    return CONTINUE;
    } else {
    return CONTINUE;
    }
    }
    private static Map readPDFFileMap(LinkedHashMap<String, String> fileMap, String fileDir) {
    //pdf 解析
    File file = new File(fileDir);
    File[] files = file.listFiles();
    PDDocument doc = null;
    StringBuffer sb = new StringBuffer();
    for (File item : files) {
    try {
    if (item.isDirectory()) {
    continue;
    }
    String filePath = item.getAbsolutePath();
    doc = PDDocument.load(new FileInputStream(filePath));
    int numberOfPages = doc.getNumberOfPages();
    PDFTextStripper pts = new PDFTextStripper();
    pts.setSortByPosition(true);
    for (int i = 1; i < numberOfPages+1; i++) {
    pts.setStartPage(i);
    pts.setEndPage(i);
    String text = pts.getText(doc);
    sb.append("文件:"+filePath+" 第"+i+"页:"+text).append("\n");
    }
    fileMap.put(filePath, sb.toString());
    //System.out.println(sb.toString());
    } catch (Exception e) {
    System.err.println("##############文件解析异常:" + item.getName());
    } finally {
    try {
    doc.close();
    } catch (IOException e) {
    System.err.println("##############文件流工具关闭失败!!!");
    }
    }
    }
    return fileMap;
    }
    private static Map readExcelFileMap(LinkedHashMap<String, String> fileMap, String fileDir) {
    OPCPackage opcPackage = null;
    //word 解析
    File file = new File(fileDir);
    File[] files = file.listFiles();
    for (File item : files) {
    try {
    if (item.isDirectory()) {
    continue;
    }
    String filePath = item.getAbsolutePath();
    opcPackage = POIXMLDocument.openPackage(filePath);
    XSSFExcelExtractor xe = new XSSFExcelExtractor(opcPackage);
    xe.setFormulasNotResults(true);
    xe.setIncludeSheetNames(true);
    String content = xe.getText();;
    fileMap.put(filePath, content);
    } catch (Exception e) {
    System.err.println("##############文件解析异常:" + item.getName());
    } finally {
    try {
    opcPackage.close();
    } catch (IOException e) {
    System.err.println("##############文件流工具关闭失败!!!");
    }
    }
    }
    return fileMap;
    }
    private static Map readWordFileMap(LinkedHashMap<String, String> fileMap, String fileDir) {
    OPCPackage opcPackage = null;
    //word 解析
    File file = new File(fileDir);
    File[] files = file.listFiles();
    for (File item : files) {
    try {
    if (item.isDirectory()) {
    continue;
    }
    String filePath = item.getAbsolutePath();
    opcPackage = POIXMLDocument.openPackage(filePath);
    XWPFWordExtractor xwpfWordExtractor = new XWPFWordExtractor(opcPackage);
    String content = xwpfWordExtractor.getText();
    fileMap.put(filePath, content);
    } catch (Exception e) {
    System.err.println("##############文件解析异常:" + item.getName());
    } finally {
    try {
    opcPackage.close();
    } catch (IOException e) {
    System.err.println("##############文件流工具关闭失败!!!");
    }
    }
    }
    return fileMap;
    }

    /*

    com.itextpdf
    itextpdf
    5.5.13.2
    jar


    org.apache.pdfbox
    pdfbox
    2.0.9


    org.apache.pdfbox
    fontbox
    2.0.9

    org.apache.poi poi 4.1.1 org.apache.poi poi-ooxml 4.1.1 commons-lang commons-lang 2.6 javac -classpath .;D:\Fsearch\lib\poi-4.1.2.jar;D:\Fsearch\lib\poi-ooxml-4.1.2.jar;D:\Fsearch\lib\poi-ooxml-schemas-4.1.2.jar;D:\Fsearch\lib\rt.jar;D:\Fsearch\lib\commons-lang-2.6.jar;D:\Fsearch\lib\xmlbeans-3.1.0.jar -d D:\Fsearch\out aa.java */

}

标签:word,String,fileMap,beginKey,Excel,System,import,println,pdf
From: https://www.cnblogs.com/leifonlyone/p/17829050.html

相关文章

  • WordPress主题警告:侧边栏字符串偏移非法
    "侧边栏字符串偏移非法"警告通常是由于在WordPress主题的侧边栏中使用了不正确的代码或字符引起的。这可能是一个语法错误、字符编码问题或标签的闭合问题。要解决这个问题,可以尝试以下几个步骤:1.检查语法错误:打开你的WordPress主题文件,找到侧边栏的相关代码,并确保没有任何语法错......
  • 不会这5个Excel函数,别说你会做数据分析?
    当涉及数据分析时,Excel是一个非常有用的工具,而掌握一些核心函数将大大提高你在数据处理和分析方面的能力。以下是我对五个重要的Excel函数的详细介绍:1.VLOOKUP函数VLOOKUP函数是Excel中最常用的查找函数之一。它允许你在一个范围内搜索特定的数值,并返回该数值所在行的其他......
  • 虚拟机,宝塔面板,wordpress
    1.进入并安装宝塔面板,linux面板安装脚本;2.选择对应的Linux版本,复制命令到虚拟机命令行执行;编者用的是centos,选择第一个脚本;3.安装结束后,会得到如下信息,复制内网面板地址,在运行虚拟机的主机上的浏览器中,进入该地址,输入对应的用户名username,密码password;安装插件选择对应的LNMP......
  • springboot集成EasyPoi,实现Excel/Word的导入导出
    前言在日常工作中,我们经常需要进行Excel或Word文档的导入和导出。这需要我们编写大量的代码和复杂的操作,从而增加了我们工作的难度。而EasyPoi就是针对这种情况而开发出来的一个简单易用的Java导入导出工具。EasyPoi是一个开源的Java导入导出工具,它提供了非常简单易用的API,可以让......
  • 图片转Excel的python小工具
    安装软件:pipinstallcvpipinstallpaddlepaddle pipinstallpandas-ihttps://pypi.douban.com/simple pipinstallpaddleocr==2.6.0.2-ihttps://pypi.tuna.tsinghua.edu.cn/simplepipinstallpaddleocr paddleocr-2.7.0.3-py3-none-any.whl 代码: pic2Excel.......
  • excel对比两个文档,判断范围内的取值是否在另一个列表内存在(vlookup函数的使用)
    背景:sheet1表为原始数据:sheet2表为新的数据副本,目标是查询sheet2列表中是否存在sheet1表的数据,并且标记出来,且获取sheet2列表的一些数据至sheet1列表中,补充D与E两列的数据情况: 一、vlookup函数介绍:作用:垂直查找(按列号查找)函数说明:vlookup(lookup_value,table_array,col_......
  • 212-c# url下载pdf,url请求,有参数,且携带cookies
    usingSystem;usingSystem.Net;usingSystem.Net.Http;usingSystem.Net.Http.Headers;usingSystem.Threading.Tasks;classProgram{staticvoidMain(){//设置要下载的PDF文件的URLstringpdfUrl="https://example.com/path/to/your/pdf......
  • MATLAB将数据写入Excel表格并添加表头
    MATLAB中写入Excel表格的函数为:xlswrite(filename,data,sheet,Range)其中的函数参数如下:filename:文件名,可以是绝对路径也可以是相对路径data:写入表格文件的数据sheet:写入的工作表,指的是sheet1、sheet2或者自己命名的工作表Range:写入的单元格区域,比如A4,指的是从A4开始写入数......
  • Word查找替换中的正则表达式
    正则表达式,多么高大上的一个叫法啊……高大上有的时候,等同于难度大……难度大有的时候,等同于高高在上……好了,又回到高大上了……其实,是工具就是要用,裱上个“太难”的框子没什么意思,还是来点实在的……********************************************************************......
  • 历时三年,写的一本数据结构与算法pdf,开源了!
    前言大家好,我是bigsai,很早就在写博客,将文章整理成了一个pdf,并且开源到github上!自己写东西断断续续也不少时间了,也写了不少东西(虽然是偏向小白),这个其实花费的时间还是比较多的,这次的话主要将数据结构与算法中一些文章整理出来,初步整理成一版pdf,先分享给大家。因为在整理pdf方......