POI中的多列写入

我尝试使用Apache POI将2个List <WebElement>列表写入Excel文件。 但我的方法只保存//column 2值。 //column 1数据未保存在Excel文件中。

请帮我解决这个问题。

 public void FourthExcel(String classNameOne,String classNameTwo) { WebDriverWait wait = new WebDriverWait(driver, 40); wait.pollingEvery(2, TimeUnit.SECONDS); String filePath = System.getProperty("user.dir"); //Book one XSSFWorkbook workbookOne = new XSSFWorkbook(); XSSFSheet sheetOne = workbookOne.createSheet("Expenses Sheet"); CommonClass.sleepTime(3000); WebElement expencesTble = driver.findElement(expencesTable); wait.until(ExpectedConditions.elementToBeClickable(expencesTble)); List<WebElement> ColOneList = driver.findElements(By.className(classNameOne)); List<WebElement> ColTwoList = driver.findElements(By.className(classNameTwo)); List<Object> ColOneobjectList = Arrays.asList(ColOneList.toArray()); List<Object> ColTwoobjectList = Arrays.asList(ColTwoList.toArray()); int ColumnOneSize = ColOneList.size()-1; for (int i = 0; i <= ColumnOneSize; i++) { out.println("For"); out.println(ColOneList.get(i).getText()); //Column 1 XSSFRow rowOneColZero = sheetOne.createRow(i); rowOneColZero.createCell(0).setCellValue(String.valueOf(ColOneobjectList.get(i))); //Column 2 XSSFRow rowOneCOlOne = sheetOne.createRow(i); rowOneCOlOne.createCell(1).setCellValue((RichTextString) ColTwoobjectList.get(i)); } try { out.println("try"); FileOutputStream fileOut = new FileOutputStream(filePath + "\\testExcel.xls"); workbookOne.write(fileOut); workbookOne.close(); } catch (IOException e) { out.println(e); } } 

Excel文件看起来像这样:

A1数据不写入excel文件。

在这里输入图像说明

for循环中,您可以重写XSSFRow行对象,请这样写:

  for (int i = 0; i <= ColumnOneSize; i++) { out.println("For"); out.println(ColOneList.get(i).getText()); //create row XSSFRow row= sheetOne.createRow(i); //to Column 1 row.createCell(0).setCellValue(String.valueOf( ColOneobjectList.get(i))); //to Column 2 row.createCell(1).setCellValue((RichTextString) ColTwoobjectList.get(i)); }