用poi和Loop填充Excel文件

我是DDT的新手,我试图把我的testing结果写在一个excel文件中。

这是一个简单的代码,它从ListArray中取值,并将它们发送到google.translate,然后捕获结果并将它们添加到另一个List。 当我遍历包含结果的列表时,它只显示捕获的最后结果,我在做什么错我的代码? 我知道我错了,但我只是无法弄清楚

任何帮助都感激不尽。 这是整个代码

public class writeFileOutPut { WebDriver driver; WebElement element; String baseUrl = "http://translate.google.com/"; @BeforeSuite public void beforeSuite() { driver = new FirefoxDriver(); driver.get(baseUrl); } @Test public void f() throws Exception{ List<String> sourceWords = new ArrayList<String>(); sourceWords.add("house"); sourceWords.add("car"); sourceWords.add("bed"); int listLength = sourceWords.size(); System.out.println("Array length is " + sourceWords.size()); for(String temp : sourceWords){ driver.findElement(By.id("gt-tl-gms")).click(); driver.findElement(By.xpath("/html/body/div[12]/table/tbody/tr/td[4]/div/div[7]/div")).click(); driver.findElement(By.id("source")).sendKeys(temp); Thread.sleep(1000); String transRes = driver.findElement(By.id("result_box")).getText(); List<String> transWords = new ArrayList<String>(); transWords.add(transRes); System.out.println(transWords); driver.findElement(By.id("source")).clear(); HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet worksheet = workbook.createSheet("POI WorkSheet"); for(int i=0; i<listLength; i++){ HSSFRow row = worksheet.createRow(i); HSSFCell cell = row.createCell(0); cell.setCellValue(transRes); } FileOutputStream fos = new FileOutputStream("My/writeDataTest3.xls"); workbook.write(fos); fos.close(); } } @AfterSuite public void afterSuite() { driver.quit(); } 

}

根据我的理解,你正在试图将结果(翻译的词)存储到列表“transWords”,当你遍历这个列表时,你只能find最后一次search。

这是因为你已经将列表初始化置于for循环中。

 List<String> transWords = new ArrayList<String>(); 

把这个代码放在for循环之前,你的列表将包含所有的结果。

我还build议您在完成search后将结果写入Excel表格。 在for循环之外(因为您将结果保存在transWords列表中,并且可以一次存储到Excel表格中)。

所以你@Test可以写成如下:

 @Test public void f() throws Exception{ //create source words list List<String> sourceWords = new ArrayList<String>(); sourceWords.add("house"); sourceWords.add("car"); sourceWords.add("bed"); System.out.println("Array length is " + sourceWords.size()); List<String> transWords = new ArrayList<String>(); //Iterate through the sourceWords list, search for each word and add result to transWords. for(String temp : sourceWords){ driver.findElement(By.id("gt-tl-gms")).click(); driver.findElement(By.xpath("/html/body/div[12]/table/tbody/tr/td[4]/div/div[7]/div")).click(); driver.findElement(By.id("source")).sendKeys(temp); Thread.sleep(1000); String transRes = driver.findElement(By.id("result_box")).getText(); transWords.add(transRes); driver.findElement(By.id("source")).clear(); } //create workbook Workbook workbook = new HSSFWorkbook(); Sheet sheet = workbook.createSheet("new sheet"); //add all the results int i=0; for (String transWord : transWords) Row row = sheet.createRow((short)i++); Cell cell = row.createCell(0); cell.setCellValue(transword); } //write to file FileOutputStream fileOut = new FileOutputStream("My/writeDataTest3.xls"); workbook.write(fileOut); fileOut.close(); } 

让我知道这是否适合你。

for (String temp : sourceWords)循环之外(即之前)移动以下初始化:

  1. List<String> transWords = new ArrayList<String>();

  2. HSSFWorkbook workbook = new HSSFWorkbook();

  3. HSSFSheet worksheet = workbook.createSheet("POI WorkSheet");

顺便说一句,我不熟悉这些HSSF类,但我怀疑你在内部循环内for (int i=0; i<listLength; i++)将会对你的worksheet有什么影响…