以下是我的代码。我该如何阅读这些数据?
import java.io.File;
import java.io.FileInputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Yoda {
public static void main(String[] args)
{
// Launch FireFox
WebDriver driver = new FirefoxDriver();
//Enter YODA URL
driver.get("https://q1yda1m2.madc.att.com:20621/YodaGUI/index.jsp");
//Enter UserID
driver.findElement(By.name("userid")).sendKeys("xx3517");
//Enter Password
driver.findElement(By.name("password")).sendKeys("123123a");
//Click on Submit button after entering User ID and Password
driver.findElement(By.name("btnSubmit")).click();
//Click on Remind Me later
driver.findElement(By.name("successOK")).click();
//Click on Search Button on YODA home page
driver.findElement(By.className("MenuBarItemSubmenu")).click();
driver.findElement(By.xpath("//*[text()='Fulfillment Details']")).click();
driver.findElement(By.name("location")).sendKeys("Q400");
driver.findElement(By.name("activity")).sendKeys("OY");
driver.findElement(By.name("orderID")).sendKeys("3955349");
driver.findElement(By.id("fulfillment_submit")).click();
}
}
答案 0 :(得分:1)
你的问题有点令人困惑。是否要从excel文件(粘贴的图像)中读取数据?然后你有正确的导入(apache poi),虽然你没有使用它。
要从excel中读取,请输入以下代码:
try {
FileInputStream fStream = new FileInputStream(new File(
"sample.xlsx"));
// Create workbook instance referencing the file created above
XSSFWorkbook workbook = new XSSFWorkbook(fStream);
// Get your first or desired sheet from the workbook
XSSFSheet sheet = workbook.getSheetAt(0); // getting first sheet
// Iterate through the rows present in sheet
Iterator<Row> rIterator = sheet.iterator();
while (rIterator.hasNext()) {
Row row = rIterator.next();
Iterator<Cell> cIterator = row.cellIterator();// Iterate through
// the columns
// present in
// that row
while (cIterator.hasNext()) {
Cell cell = cIterator.next();
if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC)
System.out.println(cell.getNumericCellValue() + "\t");
else if (cell.getCellType() == Cell.CELL_TYPE_STRING)
System.out.println(cell.getStringCellValue() + "\t");
}
System.out.println(" ");
}
fStream.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
上面的代码将读取“Sample.xlsx”文件并处理它以在控制台上输出它的内容。
另外,请检查此链接:how to read all cell value using Apache POI?
修改强>
如果您确定要从哪个列中读取值,则可以使用这种更简单的方法:
package example;
import java.io.File;
import java.io.FileInputStream;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ReadFromExcel {
public static void main(String[] args) {
try {
FileInputStream fStream = new FileInputStream(new File(
"sample.xlsx"));
// Create workbook instance referencing the file created above
XSSFWorkbook workbook = new XSSFWorkbook(fStream);
// Get your first or desired sheet from the workbook
XSSFSheet sheet = workbook.getSheetAt(0); // getting first sheet
XSSFRow row = sheet.getRow(1);
XSSFCell cell1 = row.getCell(0);
XSSFCell cell2 = row.getCell(1);
XSSFCell cell3 = row.getCell(2);
String location = cell1.toString();
String activity = cell2.toString();
String order = cell3.toString();
System.out.println(location + order + activity);
fStream.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
现在,只需通过SendKeys()传递字符串“location”,“order”和“activity”的值即可。
编辑2
您的最终代码应如下所示:
package example;
import java.io.File;
import java.io.FileInputStream;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Yoda {
public static void main(String[] args)
{
String location = null;
String activity = null;
String order = null;
// Launch FireFox
WebDriver driver = new FirefoxDriver();
// Enter YODA URL
driver.get("https://q1yda1m2.madc.att.com:20621/YodaGUI/index.jsp");
// Enter UserID
driver.findElement(By.name("userid")).sendKeys("xx3517");
// Enter Password
driver.findElement(By.name("password")).sendKeys("123123a");
// Click on Submit button after entering User ID and Password
driver.findElement(By.name("btnSubmit")).click();
// Click on Remind Me later
driver.findElement(By.name("successOK")).click();
try {
FileInputStream fStream = new FileInputStream(new File(
"C:\\Users\\xxxx\\Desktop\\sample.xlsx")); //Enter the path to your excel here
// Create workbook instance referencing the file created above
XSSFWorkbook workbook = new XSSFWorkbook(fStream);
// Get your first or desired sheet from the workbook
XSSFSheet sheet = workbook.getSheetAt(0); // getting first sheet
XSSFRow row = sheet.getRow(1);
XSSFCell cell1 = row.getCell(0);
XSSFCell cell2 = row.getCell(1);
XSSFCell cell3 = row.getCell(2);
location = cell1.toString();
activity = cell2.toString();
order = cell3.toString();
fStream.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Click on Search Button on YODA home page
driver.findElement(By.className("MenuBarItemSubmenu")).click();
driver.findElement(By.xpath("//*[text()='Fulfillment Details']"))
.click();
driver.findElement(By.name("location")).sendKeys(location);
driver.findElement(By.name("activity")).sendKeys(activity);
driver.findElement(By.name("orderID")).sendKeys(order);
driver.findElement(By.id("fulfillment_submit")).click();
}
}
答案 1 :(得分:0)
有多种方法可以从excel读取数据,其中一种最简单的方法是:
try
{
File file = new File("D:/TestData.xlsx");
FileInputStream iFile = new FileInputStream(file);
XSSFWorkbook wb = new XSSFWorkbook(iFile);
XSSFSheet sheet = wb.getSheet("Sheet1");
int rowCount = sheet.getLastRowNum();
System.out.println("the no of rows are : " + rowCount);
for (int row=1; row<=rowCount; row++)
{
String location = sheet.getRow(row).getCell(0).getStringCellValue();
String activity = sheet.getRow(row).getCell(1).getStringCellValue();
int order=(int) sheet.getRow(row).getCell(2).getNumericCellValue();
System.out.println(location + " , " + activity + " , " +order );
}
iFile.close();
}
catch (IOException e)
{
e.printStackTrace();
}
答案 2 :(得分:0)
package package1;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class yoda2 {
public static void main(String[] args) {
// Launch FireFox WebDriver driver = new FirefoxDriver();
// Enter YODA URL
driver.get("https://q1yda1m2.madc.att.com:20621/YodaGUI/index.jsp");
// Enter UserID
driver.findElement(By.name("userid")).sendKeys("xx3517");
// Enter Password driver.findElement(By.name("password")).sendKeys("123123a");
// Click on Submit button after entering User ID and Password driver.findElement(By.name("btnSubmit")).click();
// Click on Remind Me later driver.findElement(By.name("successOK")).click();
try { File file = new File("D:/TestData.xlsx");
FileInputStream iFile = new FileInputStream(file);
XSSFWorkbook wb = new XSSFWorkbook(iFile);
XSSFSheet sheet = wb.getSheet("Sheet1");
int rowCount = sheet.getLastRowNum();
System.out.println("the no of rows are : " + rowCount);
for (int row=1; row<=rowCount; row++)
{ String location = sheet.getRow(row).getCell(0).getStringCellValue();
String activity = sheet.getRow(row).getCell(1).getStringCellValue();
int order=(int) sheet.getRow(row).getCell(2).getNumericCellValue();
System.out.println(location + " , " + activity + " , " +order );
}
iFile.close();
}
catch (IOException e) { e.printStackTrace();
}
// Click on Search Button on YODA home page
driver.findElement(By.className("MenuBarItemSubmenu")).click();
driver.findElement(By.xpath("//*[text()='Fulfillment Details']")).click();
driver.findElement(By.name("location")).sendKeys(location);
driver.findElement(By.name("activity")).sendKeys(activity);
driver.findElement(By.name("orderID")).sendKeys(order);
driver.findElement(By.id("fulfillment_submit")).click(); } }