从Excel中找sheet

news/2024/7/21 6:42:10 标签: excel

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.niftyadmin.cn/n/5239859.html

相关文章

TensorRT_Win10上WSL实践篇

工程&#xff1a; 煤流检测 yolov8 的分割模型 环境&#xff1a; 首先打开WSL wsl -d Ubuntu-22.04 1. WSL升级WSL2&#xff08;必做&#xff09; WSL v1不支持直接使用Windows的GPU&#xff0c;就算做了下面的所有步骤也没办法用&#xff0c;torch.cuda.is_available()会永…

Diary18-Word文本部件

Word像乐高一样使用 一.文本部件 1.文档部件的作用 就是去打包我们的内容&#xff0c;方便后续的使用 2.如何创建文档部件 【创建文档部件】->【插入】->【文档部件】->【将所选内容保存到文档部件库】->【名称】 3.如何使用文档部件 【使用文档部件】->…

[STM32-1.点灯大师上线】

学习了江协科技的前4课&#xff0c;除了打开套件的第一秒是开心的&#xff0c;后面的时间都是在骂娘。因为51的基础已经几乎忘干净&#xff0c;c语言已经还给谭浩强&#xff0c;模电数电还有点底子&#xff0c;硬着头皮上吧。 本篇主要是讲述学习点灯的过程和疑惑解释。 1.工…

Ansys Speos SSS|执行 Camera Sensor模拟结果后处理

附件下载 联系工作人员获取附件 概述 本文是Speos Sensor System&#xff08;SSS&#xff09;的使用指南&#xff0c;这是一个强大的解决方案&#xff0c;用于camera sensor模拟结果的后处理。本文的目的是通过一个例子来理解如何正确使用SSS。当然本文描述的分析步骤适合任…

DPtech SSL VPN Service任意文件读取漏洞复现 [附POC]

文章目录 DPtech SSL VPN Service任意文件读取漏洞复现 [附POC]0x01 前言0x02 漏洞描述0x03 影响版本0x04 漏洞环境0x05 漏洞复现1.访问漏洞环境2.构造POC3.复现 DPtech SSL VPN Service任意文件读取漏洞复现 [附POC] 0x01 前言 免责声明&#xff1a;请勿利用文章内的相关技术…

2023年拼多多双11研究报告:销售额环比增长12%

随着2023年双11正式落下帷幕&#xff0c;拼多多电商也为今年双11购物狂欢节交上了一份答卷&#xff1a;实现278.6亿元交易额&#xff0c;环比一个月前增长12%。 面对这个销售增量的市场&#xff0c;有哪些类目和品牌脱颖而出&#xff0c;又呈现出怎样的发展趋势呢&#xff1f;…

4.8 构建onnx结构模型-Less

前言 构建onnx方式通常有两种&#xff1a; 1、通过代码转换成onnx结构&#xff0c;比如pytorch —> onnx 2、通过onnx 自定义结点&#xff0c;图&#xff0c;生成onnx结构 本文主要是简单学习和使用两种不同onnx结构&#xff0c; 下面以 Less 结点进行分析 方式 方法一&a…

分享88个选项卡TABJS特效,总有一款适合您

分享88个选项卡TABJS特效&#xff0c;总有一款适合您 88个选项卡TABJS特效下载链接&#xff1a;https://pan.baidu.com/s/1BFDqu6ltI3HVqlqMS-Aw5w?pwd6666 提取码&#xff1a;6666 Python采集代码下载链接&#xff1a;采集代码.zip - 蓝奏云 学习知识费力气&#xff0…