①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