我得问题是这样的:比如说有个文件夹,文件夹中还有其他子文件夹,每个子文件夹中都有一个info.xls文件,java上是如何实现读取该文件夹下所有的info.xls文件的?
我的理想方案是这样的,提交一个文件夹目录,程序就读取该文件夹下所有的info.xls文件,然后作相关处理
源代码是这样的:
public void importExcel(String file) throws SQLException {
Connection con = null;
CallableStatement cstmt = null;
ResultSet rs = null;
POIFSFileSystem fs = null;
HSSFWorkbook wb = null;
try {
fs = new POIFSFileSystem(new FileInputStream(file));
wb = new HSSFWorkbook(fs);
} catch (IOException e) {
e.printStackTrace(); }
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row = null;
HSSFCell cell = null;
String name = "";
double value;
int rowNum, cellNum;
int i;
rowNum = sheet.getLastRowNum();
for (i = 0; i <= rowNum; i++) {
row = sheet.getRow(i);
//cellNum = row.getLastCellNum();
cell = row.getCell((short) 0);
name = cell.getStringCellValue();
cell = row.getCell((short) 1);
value = cell.getNumericCellValue();
try {
con = DBConnection.getConnection();
cstmt = con.prepareCall(IConstants.SP_IMPORTEXCEL_KEY);
cstmt.setString(1, name);
cstmt.setDouble(2, value);
cstmt.execute();
} catch (SQLException e) {
e.printStackTrace();
logger.error(e.getMessage());
throw e;
} finally {
try { if (cstmt != null) {
cstmt.close();
}
if (con != null) {
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
logger.error(e.getMessage());
throw e;
} }
} } }
以上是我写的源代码,现在只能实现提交某个xls文件,然后作相应的导数据可操作,可是现在我的xls文件很多,而且分布在很多文件夹中,所以想作一个批处理,读取文件夹下所有的info.xls文件,希望哪位大虾可以指点一二
我的理想方案是这样的,提交一个文件夹目录,程序就读取该文件夹下所有的info.xls文件,然后作相关处理
源代码是这样的:
public void importExcel(String file) throws SQLException {
Connection con = null;
CallableStatement cstmt = null;
ResultSet rs = null;
POIFSFileSystem fs = null;
HSSFWorkbook wb = null;
try {
fs = new POIFSFileSystem(new FileInputStream(file));
wb = new HSSFWorkbook(fs);
} catch (IOException e) {
e.printStackTrace(); }
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row = null;
HSSFCell cell = null;
String name = "";
double value;
int rowNum, cellNum;
int i;
rowNum = sheet.getLastRowNum();
for (i = 0; i <= rowNum; i++) {
row = sheet.getRow(i);
//cellNum = row.getLastCellNum();
cell = row.getCell((short) 0);
name = cell.getStringCellValue();
cell = row.getCell((short) 1);
value = cell.getNumericCellValue();
try {
con = DBConnection.getConnection();
cstmt = con.prepareCall(IConstants.SP_IMPORTEXCEL_KEY);
cstmt.setString(1, name);
cstmt.setDouble(2, value);
cstmt.execute();
} catch (SQLException e) {
e.printStackTrace();
logger.error(e.getMessage());
throw e;
} finally {
try { if (cstmt != null) {
cstmt.close();
}
if (con != null) {
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
logger.error(e.getMessage());
throw e;
} }
} } }
以上是我写的源代码,现在只能实现提交某个xls文件,然后作相应的导数据可操作,可是现在我的xls文件很多,而且分布在很多文件夹中,所以想作一个批处理,读取文件夹下所有的info.xls文件,希望哪位大虾可以指点一二
this.getAllFiles(rf);private void getAllFiles(File rf){File[] fs = rf.listFiles();for(int i=0;i<fs.length;i++ ){
if( fs[i].isFile() ){//如果是文件,看看是不是自己要找的文件
if(fs[i].getName.equals("you file name")){
system.out.println("get one your need");
}
}else{
this.getAllFiles(fs[i]);//如果是目录,递归
}
}
}
以上,没有仔细考虑,大概思路是这样的,具体你自己调一下