用java实现,在某个全是excel文件的目录下,查找其中excel文件是否拥有某个字段,拥有则打出该excel文件名以及路径


①java文件

package zjexcel;


import java.io.*;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;

import org.apache.poi.ss.usermodel.*;

public class read {
    static String keyword="";
    //配置文件,其中配置路径,以及要查找的value值
     public  void load3() throws Exception{
        
            Properties p=new Properties();
            InputStream is=ClassLoader.getSystemResourceAsStream("data.properties");
            p.load(is);
            Enumeration enu = p.propertyNames();
         //   p.list(System.out) ;
            while(enu.hasMoreElements())  
           {  
                String thisKey = (String)enu.nextElement();  
                //System.out.println("key="+thisKey);  
                //System.out.println("value="+p.getProperty(thisKey));             
            }
            String proPath = p.getProperty("filepath");
            String keyword = p.getProperty("value");
            read.test(proPath,keyword);

        }
    
    
    // 递归读取某个目录下的所有文件
     private  static void test(String fileDir,String keyword) {  
        
            List<File> fileList = new ArrayList<File>();  
            File file = new File(fileDir);  
            File[] files = file.listFiles();// 获取目录下的所有文件或文件夹  
            if (files == null) {// 如果目录为空,直接退出  
                return;  
            }  
            // 遍历,目录下的所有文件  
            for (File f : files) {  
                if (f.isFile()) {  
                    fileList.add(f);  
                } else if (f.isDirectory()) {
                    String absolutePath = f.getAbsolutePath();
                    System.out.println(f.getAbsolutePath());  
                    test(f.getAbsolutePath(), absolutePath);  
                }  
            }  
            for (File f1 : fileList) {  
//                System.out.println(f1.getName());
                String fileName = f1.getName();
                String path=fileDir;
                String readPath=path+fileName;
//                System.out.println(readPath);
                read.read1(readPath,keyword);
            }  
        }  
    
    
    
    
    
    
    //读取excel并且查找
    public static  void read1(String readpath,String keyword) {  
        File file = new File(readpath);  
        InputStream inputStream = null;  
        Workbook workbook = null;  
        try {  
            inputStream = new FileInputStream(file);  
            workbook = WorkbookFactory.create(inputStream);  
            inputStream.close();  
            //工作表对象  
            Sheet sheet = workbook.getSheetAt(0);  
            //总行数  
            int rowLength = sheet.getLastRowNum()+1;  
            //工作表的列  
            Row row = sheet.getRow(0);  
            //总列数  
            int colLength = row.getLastCellNum();  
            //得到指定的单元格  
            Cell cell = row.getCell(0);  
            //得到单元格样式  
            CellStyle cellStyle = cell.getCellStyle();  
           // System.out.println("行数:" + rowLength + ",列数:" + colLength);  
            for (int i = 0; i < rowLength; i++) {  
                row = sheet.getRow(i);  
                for (int j = 0; j < colLength; j++) {  
                    cell = row.getCell(j);   
                    //将所有的需要读的Cell表格设置为String格式  
                    if (cell != null)  
                        cell.setCellType(CellType.STRING);
                    
                    String stringCellValue = cell.getStringCellValue();
                 
                    if(stringCellValue.contains(keyword))
                    System.out.println(file+"内包含"+"------------"+keyword);
                    //System.out.print(cell.getStringCellValue() + "\t");  
                }  
                System.out.println();  
            }  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
 
    public static void main(String[] args) throws Exception {
        
        new read().load3();
    
        
    }  
}


②data.properties文件

filepath=C:/Users/jack/Desktop/zjtest/

value=111















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值