程序覆盖旧数据而不是将新数据放入新行

时间:2015-11-21 12:34:36

标签: java selenium-webdriver apache-poi

我使用Selenium webdriver和JAVA创建了程序。我从网站获取数据并放入excel。我正在编写数据,如输入10个值然后更改xpath并再次放入新的10个值。但是当我尝试设置新值时,我的程序会覆盖旧的10个值。所以在excel中我只得到10个值而不是20个。

代码是:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;  
import org.apache.poi.ss.usermodel.Workbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Retrieve {

    public static WebDriver driver;
    public static int rowcount=0;
    public static int cellcount=0;
    public static String data[] = new String[10];

    public static void main(String[] args) throws IOException {
        openurl();
        write_data();
    }

    public static void openurl() {
        System.setProperty("webdriver.chrome.driver", "E:\\Selenium-Webdriver\\Chrome_Driver\\chromedriver.exe");
        driver = new ChromeDriver();

        driver.get(URL);

        data[0] = driver.findElement(By.xpath("//*[@id='wpv-view-layout-34546-CPID79']/table/tbody/tr[1]/td[2]/a/b")).getText();
        data[1] = driver.findElement(By.xpath("//*[@id='wpv-view-layout-34546-CPID79']/table/tbody/tr[2]/td[2]/a/b")).getText();
        data[2] = driver.findElement(By.xpath("//*[@id='wpv-view-layout-34546-CPID79']/table/tbody/tr[3]/td[2]/a/b")).getText();
        data[3] = driver.findElement(By.xpath("//*[@id='wpv-view-layout-34546-CPID79']/table/tbody/tr[4]/td[2]/a/b")).getText();
        data[4] = driver.findElement(By.xpath("//*[@id='wpv-view-layout-34546-CPID79']/table/tbody/tr[5]/td[2]/a/b")).getText();
        data[5] = driver.findElement(By.xpath("//*[@id='wpv-view-layout-34546-CPID79']/table/tbody/tr[6]/td[2]/a/b")).getText();
        data[6] = driver.findElement(By.xpath("//*[@id='wpv-view-layout-34546-CPID79']/table/tbody/tr[7]/td[2]/a/b")).getText();
        data[7] = driver.findElement(By.xpath("//*[@id='wpv-view-layout-34546-CPID79']/table/tbody/tr[8]/td[2]/a/b")).getText();
        data[8] = driver.findElement(By.xpath("//*[@id='wpv-view-layout-34546-CPID79']/table/tbody/tr[9]/td[2]/a/b")).getText();
        data[9] = driver.findElement(By.xpath("//*[@id='wpv-view-layout-34546-CPID79']/table/tbody/tr[10]/td[2]/a/b")).getText();
    }


    public static void write_data() throws IOException {
        File file = new File("C:\\Users\\Rahul\\Documents\\Practice.xls");
        FileInputStream inputStream = new FileInputStream(file);
        Workbook MyWorkbook = null;
        MyWorkbook = new HSSFWorkbook(inputStream);
        Sheet sheet = MyWorkbook.getSheet("sheet1");
        Row row = sheet.getRow(0);
        try{
            int lendgth = data.length;
            for(int i=0;i<data.length;i++) {
                Row newrow = sheet.createRow(rowcount+1);
                Cell cell = newrow.createCell(cellcount);
                if(!data[i].equals("") || data[i].equals(null)) {
                    if(row.equals(null)){
                        newrow.createCell(rowcount);
                        cell.setCellValue(data[i]);
                        rowcount++;
                    }
                }
            }
        }catch(Exception E) {
            E.printStackTrace();
        }

        inputStream.close();
        FileOutputStream outputStream = new FileOutputStream(file);
        MyWorkbook.write(outputStream);
        outputStream.close();
    }
}

1 个答案:

答案 0 :(得分:0)

在编写数据时,尝试查找excel表中的最后一行并提供该行号(如果需要,可以增加)而不是总是给出第0行(行row = sheet.getRow(0);)< / p>

感谢