从Excel中找sheet

pom.xml



    4.0.0

    com.elex.exceltools
    ExcelTools
    1.0

    
        UTF-8
        11
        11
    

    

        
        
            commons-beanutils
            commons-beanutils
            1.9.3
        
        
        
            org.apache.commons
            commons-lang3
            3.8.1
        
        
        
            org.apache.commons
            commons-collections4
            4.2
        

        
        
            org.projectlombok
            lombok
            1.18.12
            compile
        

        
        
            org.apache.poi
            poi
            5.1.0
        
        
            org.apache.poi
            poi-ooxml
            5.1.0
        

        
        
            com.alibaba
            fastjson
            1.2.79
        

        
        
            org.freemarker
            freemarker
            2.3.31
        
        
    

    
        
        ExcelTools

        
            
                org.apache.maven.plugins
                maven-assembly-plugin
                3.3.0
                
                    
                    
                        
                            com.elex.exceltools.Main
                        
                    

                    
                        jar-with-dependencies
                    

                    false
                
                
                    
                        make-assembly 
                        package 
                        
                            single
                        
                    
                
            

        
    


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> map = getMap(path);

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

        return "未找到";
    }


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

        Map> 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 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

使用:

从Excel中找sheet_第1张图片

你可能感兴趣的:(#,java写工具,excel)