根据一列读取 excel 数据并提供给 java class
Read excel data depending on one column and feed to java class
我目前正在从 excel sheet 中提取我的数据,并使用 testNG 的 dataProvider 注释传递给我的测试。我从 excel 返回数据的代码如下。
public static Object [][] excelData(String filepath) throws Exception {
File excelFilePath = new File (filepath);
FileInputStream fis = new FileInputStream(excelFilePath);
HSSFWorkbook wb = new HSSFWorkbook(fis);
HSSFSheet ws = wb.getSheet("Data");
int rowNum = ws.getLastRowNum()+1;
int colNum = ws.getRow(0).getLastCellNum();
Object[][] data = new Object[rowNum][colNum];
for (int i=0; i<rowNum; i++){
HSSFRow row = ws.getRow(i);
for (int j=0; j<colNum; j++){
HSSFCell cell = row.getCell(j);
Object value =(Object) cellToString(cell);
data[i][j] = (Object)value;
// System.out.println("The value is " + value);
}
}
System.out.println(Arrays.deepToString(data));
return data;
}
.
public static Object cellToString(HSSFCell cell){
int type;
Object result;
type = cell.getCellType();
switch(type) {
case 0://numeric value in excel
result = cell.getNumericCellValue();
break;
case 1: //string value in excel
result = cell.getStringCellValue();
break;
case 2: //boolean value in excel
result = cell.getBooleanCellValue ();
break;
default:
throw new RuntimeException("There are not support for type with id ["+type+"] of cell");
}
return result;
}
当前 excel 如下所示
我需要根据下面的最后一列传递用户名和密码
您的代码当前读取所有单元格。
在 excel 文件中可能有不同大小的列。如果您只获得第一行的大小,您将丢失一些行的单元格值(大于第一行的列大小)。
您应该编写自己的方法来查找最大列大小,而不是 int colNum = ws.getRow(0).getLastCellNum();
,如下所示。调用这个方法int colNum = getMaxColumnNum(ws);
public static int getMaxColumnNum(HSSFSheet ws) {
int rowNum = ws.getLastRowNum() + 1;
int max = 0;
int temp = 0;
for (int i = 0; i < rowNum; i++) {
temp = ws.getRow(i).getLastCellNum();
if (max < temp)
max = temp;
}
return max;
}
另外你的方法名是cellToString()
但是它是returns对象,你应该把它的名字改成getCellValue()
.
我目前正在从 excel sheet 中提取我的数据,并使用 testNG 的 dataProvider 注释传递给我的测试。我从 excel 返回数据的代码如下。
public static Object [][] excelData(String filepath) throws Exception {
File excelFilePath = new File (filepath);
FileInputStream fis = new FileInputStream(excelFilePath);
HSSFWorkbook wb = new HSSFWorkbook(fis);
HSSFSheet ws = wb.getSheet("Data");
int rowNum = ws.getLastRowNum()+1;
int colNum = ws.getRow(0).getLastCellNum();
Object[][] data = new Object[rowNum][colNum];
for (int i=0; i<rowNum; i++){
HSSFRow row = ws.getRow(i);
for (int j=0; j<colNum; j++){
HSSFCell cell = row.getCell(j);
Object value =(Object) cellToString(cell);
data[i][j] = (Object)value;
// System.out.println("The value is " + value);
}
}
System.out.println(Arrays.deepToString(data));
return data;
}
.
public static Object cellToString(HSSFCell cell){
int type;
Object result;
type = cell.getCellType();
switch(type) {
case 0://numeric value in excel
result = cell.getNumericCellValue();
break;
case 1: //string value in excel
result = cell.getStringCellValue();
break;
case 2: //boolean value in excel
result = cell.getBooleanCellValue ();
break;
default:
throw new RuntimeException("There are not support for type with id ["+type+"] of cell");
}
return result;
}
当前 excel 如下所示
我需要根据下面的最后一列传递用户名和密码
您的代码当前读取所有单元格。
在 excel 文件中可能有不同大小的列。如果您只获得第一行的大小,您将丢失一些行的单元格值(大于第一行的列大小)。
您应该编写自己的方法来查找最大列大小,而不是 int colNum = ws.getRow(0).getLastCellNum();
,如下所示。调用这个方法int colNum = getMaxColumnNum(ws);
public static int getMaxColumnNum(HSSFSheet ws) {
int rowNum = ws.getLastRowNum() + 1;
int max = 0;
int temp = 0;
for (int i = 0; i < rowNum; i++) {
temp = ws.getRow(i).getLastCellNum();
if (max < temp)
max = temp;
}
return max;
}
另外你的方法名是cellToString()
但是它是returns对象,你应该把它的名字改成getCellValue()
.