根据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;
        }

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

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

        List<String> list = new ArrayList<>();

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

        return list;
    }


    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

使用:

  • 7
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值