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 {// 获取到所有的excelList<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=%~dp0set /p input=name:rem RaftCropsjava -jar -Dpath=E:\02_my_work_jianbing\slgconfiguration\excel -DtargetName=%input% ExcelTools.jar
pause
使用: