根据java类名找出当前是哪个Excel中的sheet

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.elex.exceltools</groupId>
    <artifactId>ExcelTools</artifactId>
    <version>1.0</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <dependencies>

        <!-- commons-beanutils -->
        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.9.3</version>
        </dependency>
        <!-- commons-lang3 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.8.1</version>
        </dependency>
        <!--commons-collections4 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-collections4</artifactId>
            <version>4.2</version>
        </dependency>

        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
            <scope>compile</scope>
        </dependency>

        <!--poi-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>5.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>5.1.0</version>
        </dependency>

        <!--fastjson-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.79</version>
        </dependency>

        <!--freemaker-->
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.31</version>
        </dependency>
        
    </dependencies>

    <build>
        <!-- 第1步: 最后打包出来要发布的jar包名字-->
        <finalName>ExcelTools</finalName>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.3.0</version>
                <configuration>
                    <!--第2步: 配置程序启动入口-->
                    <archive>
                        <manifest>
                            <mainClass>com.elex.exceltools.Main</mainClass>
                        </manifest>
                    </archive>

                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>

                    <appendAssemblyId>false</appendAssemblyId>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id> <!-- this is used for inheritance merges -->
                        <phase>package</phase> <!-- bind to the packaging phase -->
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>


</project>

Main.java

package com.elex.exceltools;

import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackageAccess;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {

        String path = "E:\\02_my_work_jianbing\\slgconfiguration\\excel";
        String targetName = "RaftCrops";

        path = System.getProperty("path", "");
        targetName = System.getProperty("targetName", "");

        System.out.println("path=" + path);
        System.out.println("targetName=" + targetName);
        if (path.length() == 0 || targetName.length() == 0) {
            System.out.println("指定下目录和查找文件名");
            return;
        }

        String ret = find(path, targetName);
        System.out.println("------查找结果----");
        System.out.println(ret);
    }

    private static String find(String path, String targetName) throws Exception {
        Map<String, Set<String>> map = getMap(path);

        for (String excelName : map.keySet()) {
            Set<String> sheetNames = map.get(excelName);
            if (sheetNames.contains(targetName)) {
                return excelName;
            }
        }

        return "未找到";
    }


    private static Map<String, Set<String>> getMap(String path) throws Exception {
        // 获取到所有的excel
        List<File> outFiles = new ArrayList<>();
        getFiles(path, outFiles);

        Map<String, Set<String>> map = new HashMap<>();

        for (File file : outFiles) {
            OPCPackage pkg = OPCPackage.open(file.getAbsolutePath(), PackageAccess.READ);

            try (XSSFWorkbook workbook = new XSSFWorkbook(pkg)) {
                int numberOfSheets = workbook.getNumberOfSheets();
                for (int i = 0; i < numberOfSheets; i++) {
                    XSSFSheet sheet = workbook.getSheetAt(i);
                    String sheetName = sheet.getSheetName();
                    if (sheetName.contains("#")) {
                        continue;
                    }
                    map.computeIfAbsent(file.getAbsolutePath(), k -> new HashSet<>())
                            .add(sheetName);
                }
            }
        }

        return map;
    }


    private static void getFiles(String path, List<File> outFiles) {
        File file = new File(path);

        File[] files = file.listFiles();
        for (File fileTmp : files) {
            if (fileTmp.isFile()) {
                if (fileTmp.getName().contains(".xlsx") && !fileTmp.getName().contains("~")) {
                    outFiles.add(fileTmp);
                }
            } else {
                getFiles(fileTmp.getAbsolutePath(), outFiles);
            }
        }

    }
}

FindSheet.bat

@echo off
chcp 936

:: 执行根目录
set base_path=%~dp0

set /p input=name:

rem RaftCrops

java -jar -Dpath=E:\02_my_work_jianbing\slgconfiguration\excel -DtargetName=%input% ExcelTools.jar
pause

使用:

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/229237.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

word一键接受所有修订并保留修订痕迹

目的&#xff1a;让word修订插入的内容在接受修订后保留痕迹。 文章目录 目的&#xff1a;让word修订插入的内容在接受修订后保留痕迹。1. 打开批注的word文件2. 同时按住&#xff1a;*AltF11*&#xff0c;然后右键&#xff1a;Normal -->插入--> 模块3. 在出现的代码框中…

模块式雨水调蓄池由若干个模块组合成一个水池使用寿命达70年以上

模块式雨水调蓄池是一种先进的雨水收集和利用系统&#xff0c;它由若干个模块组合而成&#xff0c;每个模块都具有一定的储水能力和调蓄功能。这种调蓄池具有使用寿命长、适应性强、综合成本低等优点&#xff0c;因此在城市雨水管理和水资源利用方面具有广泛的应用前景。 模块…

CentOS服务自启权威指南:手动启动变为开机自启动(以Jenkins服务为例)

前言 CentOS系统提供了多种配置服务开机自启动的方式。本文将介绍其中两种常见的方式&#xff0c; 一种是使用Systemd服务管理器配置&#xff0c;不过&#xff0c;在实际中&#xff0c;如果你已经通过包管理工具安装的&#xff0c;那么服务通常已经被配置为Systemd服务&#…

积累这 4 种资源才是你的个人竞争力

在我们离开校园&#xff0c;踏入职场之后&#xff0c;总是会听到这样的论调&#xff1a;我们需要不断成长&#xff0c;提升自己的个人核心竞争力&#xff0c;才能在这个残酷的社会中混下去&#xff0c;混得更好。 那到底什么是个人核心竞争力呢&#xff1f;关于这个问题的答案…

【算法题】找出符合要求的字符串子串(js)

题解&#xff1a; function solution(str1, str2) {const set1 new Set([...str1]);const set2 new Set([...str2]);return [...set1].filter((item) > set2.has(item)).sort();}console.log(solution("fach", "bbaaccedfg"));//输入:fach// bbaacced…

JAVA使用POI向doc加入图片

JAVA使用POI向doc加入图片 前言 刚来一个需求需要导出一个word文档&#xff0c;文档内是系统某个界面的各种数据图表&#xff0c;以图片的方式插入后导出。一番查阅资料于是乎着手开始编写简化demo,有关参考poi的文档查阅 Apache POI Word(docx) 入门示例教程 网上大多数是XXX…

Swing程序设计(9)复选框,下拉框

文章目录 前言一、复选框二、下拉框总结 前言 该篇文章简单介绍了Java中Swing组件里的复选框组件、列表框组件、下拉框组件&#xff0c;这些在系统中都是常用的组件。 一、复选框 复选框&#xff08;JCheckBox&#xff09;在Swing组件中的使用也非常广泛&#xff0c;一个方形方…

Vulnhub-DC-9 靶机复现完整过程

一、搭建环境 kali的IP地址是&#xff1a;192.168.200.14 DC-9的IP地址暂时未知 二、信息收集 1、探索同网段下存活的主机 arp-scan -l #2、探索开放的端口 开启端口有&#xff1a;80和22端口 3、目录扫描 访问80 端口显示的主页面 分别点击其他几个页面 可以看到是用户…

《Linux源码趣读》| 好书推荐

目录 一. &#x1f981; 前言二. &#x1f981; 像小说一样趣读 Linux 源码三. &#x1f981; 学习路线 一. &#x1f981; 前言 最近、道然科技给狮子送了两本书&#xff1a;一本是付东来的《labuladong的算法笔记》、一本是闪客著的《Linux源码趣读》&#xff0c;《labulado…

零基础如何入门HarmonyOS开发?

HarmonyOS鸿蒙应用开发是当前非常热门的一个领域&#xff0c;许多人都想入门学习这个技术。但是&#xff0c;对于零基础的人来说&#xff0c;如何入门确实是一个问题。下面&#xff0c;我将从以下几个方面来介绍如何零基础入门HarmonyOS鸿蒙应用开发学习。 一、了解HarmonyOS鸿…

认识系统服务daemons

什么是daemon与服务&#xff08;service) 常驻内存的是进程&#xff0c;可以提供一些系统或网络功能&#xff0c;这就是服务。实现service的程序称为daemon。也就是说要想提供某种服务&#xff0c;daemon实在后台运行的。 daemon的分类&#xff1a; 1&#xff09;可独立启动…

CSS——sticky定位

1. 大白话解释sticky定位 粘性定位通俗来说&#xff0c;它就是相对定位relative和固定定位fixed的结合体&#xff0c;它的触发过程分为三个阶段 在最近可滚动容器没有触发滑动之前&#xff0c;sticky盒子的表现为相对定位relative【第一阶段】&#xff0c; 但当最近可滚动容…

class062 宽度优先遍历及其扩展【算法】

class062 宽度优先遍历及其扩展【算法】 算法讲解062【必备】宽度优先遍历及其扩展 code1 1162. 地图分析 // 地图分析 // 你现在手里有一份大小为 n x n 的 网格 grid // 上面的每个 单元格 都用 0 和 1 标记好了其中 0 代表海洋&#xff0c;1 代表陆地。 // 请你找出一个海…

初识Linux:权限(2)

目录 权限 用户&#xff08;角色&#xff09; 文件权限属性 文件的权限属性&#xff1a; 有无权限的区别&#xff1a; 身份匹配&#xff1a; 拥有者、所属组的修改&#xff1a; 八进制的转化&#xff1a; 文件的类型&#xff1a; x可执行权限为什么不能执行&#xf…

flstudio21破解汉化版2024最新水果编曲使用教程

​ 如果你一直梦想制作自己的音乐(无论是作为一名制作人还是艺术家)&#xff0c;你可能会想你出生在这个时代是你的幸运星。这个水果圈工作室和上一版之间的改进水平确实令人钦佩。这仅仅是FL Studio 21所提供的皮毛。你的音乐项目的选择真的会让你大吃一惊。你以前从未有过这…

3_CSS层叠样式表基础

第3章-CSS层叠样式表基础 学习目标(Objective) 掌握标签选择器的使用掌握类选择器的使用了解id选择器和通配符选择器掌握font属性和color属性的应用 1.HTML的局限性 如果要改变下高度或者变一个颜色&#xff0c;就需要大量重复操作 总结&#xff1a; HTML满足不了设计者的需…

初识优先级队列与堆

1.优先级队列 由前文队列queue可知&#xff0c;队列是一种先进先出(FIFO)的数据结构&#xff0c;但有些情况下&#xff0c;操作的数据可能带有优先级&#xff0c;一般出队列时&#xff0c;可能需要优先级高的元素先出队列&#xff0c;在此情况下&#xff0c;使用队列queue显然不…

git clone 命令

git clone 是一个用于克隆&#xff08;clone&#xff09;远程 Git 仓库到本地的命令。 git clone 可以将一个远程 Git 仓库拷贝到本地&#xff0c;让自己能够查看该项目&#xff0c;或者进行修改。 git clone 命令&#xff0c;你可以复制远程仓库的所有代码和历史记录&#xf…

python基于轻量级GhostNet模型开发构建23种常见中草药图像识别系统

轻量级识别模型在我们前面的博文中已经有过很多实践了&#xff0c;感兴趣的话可以自行移步阅读&#xff1a; 《移动端轻量级模型开发谁更胜一筹&#xff0c;efficientnet、mobilenetv2、mobilenetv3、ghostnet、mnasnet、shufflenetv2驾驶危险行为识别模型对比开发测试》 《基…

目标检测——OverFeat算法解读

论文&#xff1a;OverFeat: Integrated Recognition, Localization and Detection using Convolutional Networks 作者&#xff1a;Pierre Sermanet, David Eigen, Xiang Zhang, Michael Mathieu, Rob Fergus, Yann LeCun 链接&#xff1a;https://arxiv.org/abs/1312.6229 文章…