我正在尝试从Excel文件读取数据并将数据输入到我的Web应用程序,但它无法正常工作。
请查看我的代码。这是为excel创建的类</ p>
我使用了Apache POI
Excel类
package ReadExcel;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public class Excel_Data {
HSSFWorkbook wb;
HSSFSheet sheet1;
public Excel_Data(String fileName,String sheetName) throws Exception {
File src= new File("E:\\Jino_testing\\Git_Hub\\Zmarta_se\\resources\\Zmarta.xls");
FileInputStream fis = new FileInputStream(src);
HSSFWorkbook wb = new HSSFWorkbook(fis);
}
public String getData( int sheetNumber, int row,int column) {
HSSFSheet sheet1 =wb.getSheetAt(0);
String data =sheet1.getRow(row).getCell(column).getStringCellValue();
return data;
}
}
这是我正在运行的Test类
public void ApplyLoan() throws Exception {
//Reading Excel
Excel_Data read = new Excel_Data("Zmarta.xls","Co-Applicant" );
Thread.sleep(3000);
driver.findElement(apply).click();
driver.findElement(loan).click();
Thread.sleep(7000);
driver.findElement(amount).sendKeys(read.getData(0,1,1));
Thread.sleep(3000);
new Select (driver.findElement(years)).selectByVisibleText("14 år");
}
答案 0 :(得分:1)
您的代码中存在各种错误,此处是更正的代码...
这是您的Excel_Data
班级
public class Excel_Data {
HSSFWorkbook wb;
HSSFSheet sheet1;
HSSFCell cell;
public Excel_Data(String fileName) throws Exception {
FileInputStream fis = new FileInputStream(fileName);
wb = new HSSFWorkbook(fis);
}
public String getData(int sheetNumber, int row, int column) {
sheet1 = wb.getSheetAt(sheetNumber);
String data = "";
try{
cell = sheet1.getRow(row).getCell(column);
switch(cell.getCellTypeEnum()) {
case BOOLEAN:
data = cell.getRawValue();
break;
case NUMERIC:
data = cell.getRawValue();
break;
case STRING:
data = cell.getStringCellValue();
break;
default:
break;
}
}catch(Exception e){
e.printStackTrace();
}
return data;
}
}
以下是Test
类
public void ApplyLoan() throws Exception {
//Reading Excel
Excel_Data read = new Excel_Data("E:\\Jino_testing\\Git_Hub\\Zmarta_se\\resources\\Zmarta.xls");
Thread.sleep(3000);
driver.findElement(apply).click();
driver.findElement(loan).click();
Thread.sleep(7000);
driver.findElement(amount).sendKeys(read.getData(0,1,1));
Thread.sleep(3000);
new Select (driver.findElement(years)).selectByVisibleText("14 år");
}
注意 - &gt; HSSFWorkbook
是从.xls
文件中读取...使用XSSFWorkbook
来读取.xlsx
文件。