模块的摘要,它将书名,工作表名称和列名称作为参数,并期望模块返回所需的行索引。
public int getExcelData(String WBookName, String sheetName, String columnName) {
int colNum = -1;
int rowIndex = -1;
InputStream inputStream = getClass().getClassLoader().getResourceAsStream("/"+WBookName+".xls");
try {
excelWBook = new XSSFWorkbook(inputStream);
Log.info(WBookName+"Excel File loaded Successfully");
} catch (IOException e) {
Log.fatal("Unable to load"+WBookName+" Excel File");
e.printStackTrace();
}
excelWSheet = excelWBook.getSheet(sheetName);
row = excelWSheet.getRow(0);
for(int i=0; i<row.getLastCellNum();i++) {
if(row.getCell(i).getStringCellValue().trim().equals(columnName)){
colNum = i;
}
}
{
//code for getting last non-empty row for a columnName (Possible using Iterator?)
}
return rowIndex;
答案 0 :(得分:2)
由于Apache POI的方法为Sheet#getLastRowNum()
,我会使用for循环从<form>
向后excelWSheet.getLastRowNum()
,并询问每一行是否存在值。
代码将类似(您应该自己尝试,我现在只是“在浏览器中编程”)这个:
0