我试图自动化一个网站,我试图从Excel和Excel中读取数据。在文本框中一个接一个地输入数据。
我正在使用' org.apache.poi'版本3.12。但是,当我尝试从excel输入数据时,我得到了" java.lang.ClassCastException:org.apache.poi.hssf.usermodel.HSSFCell无法转换为java.lang.String&# 34;错误。在google上搜索以下错误时,我发现我们需要使用' DataFormatter'格式化单元格。即使在使用DataFormatter之后,我仍然会看到以下错误。
从excel读取的代码如下所示
public static ArrayList iterateCells(String sheetName) {
// Array List to store the excel sheet data
ArrayList excelData = new ArrayList();
DataFormatter formatter = new DataFormatter();
try {
FileInputStream userDetailsFile = new FileInputStream("D:\\userLoginDetails.xls");
HSSFWorkbook wb = new HSSFWorkbook(userDetailsFile);
//Retrieve the first sheet of the workbook.
//HSSFSheet excelSheet = wb.getSheetAt(0);
int index = wb.getSheetIndex(sheetName);
Sheet sheet = wb.getSheetAt(index);
// Iterate through the sheet rows and cells.
// Store the retrieved data in an arrayList
Iterator rows = sheet.rowIterator();
while (rows.hasNext()) {
HSSFRow row = (HSSFRow) rows.next();
Iterator cells = row.cellIterator();
while (cells.hasNext()) {
HSSFCell cell = (HSSFCell) cells.next();
DataFormatter df = new DataFormatter();
df.formatCellValue(cell);
excelData.add(cell);
}
}
} catch(IOException ex) {
ex.printStackTrace();
}
return excelData;
}
我在网页中输入值的代码如下所示。
@Test(dependsOnMethods="AddCardioExerciseForTheDay")
public void AddStrengthExerciseForTheDay() throws Exception {
engineDriver.landingPageAfterLoggingIn.addExerciseAfterLoggingIn();
List colData = new ArrayList();
colData = wte.iterateCells("DataSheet");
String exerciseType = (String) colData.get(1);
engineDriver.exercisePage.selectStrengthExercise(exerciseType);
engineDriver.exercisePage.goToAddFoodPage();
}
有人能指出我哪里出错了。谢谢!
编辑:我在以下行中收到错误
String exerciseType = (String) colData.get(1);
堆栈追踪:
java.lang.ClassCastException: org.apache.poi.hssf.usermodel.HSSFCell cannot be cast to java.lang.String
at com.myfitnesspal.tests.MfpExerciseLoggingTest.AddStrengthExerciseForTheDay(MfpExerciseLoggingTest.java:107)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:643)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:820)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1128)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
at org.testng.TestRunner.privateRun(TestRunner.java:782)
at org.testng.TestRunner.run(TestRunner.java:632)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
at org.testng.SuiteRunner.run(SuiteRunner.java:268)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
at org.testng.TestNG.run(TestNG.java:1064)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:236)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:81)
答案 0 :(得分:1)
在单元格上使用getStringCellValue()
将其内容作为字符串。
在你的情况下,我认为它应该是:
String exerciseType = colData.get(1).getStringCellValue();