不使用java读取文件.xslx中的第一项

时间:2017-04-25 20:59:15

标签: java arraylist selenium-webdriver hashmap

任何人都可以知道我做错了什么吗? 我创建了一个数据库连接并将数据(登录名,密码)保存到File.xlsx 写入后文件的示例

Login1 | Password1 
Login2 | Password2 
Login3 | Password3

然后它将文件中的值检索到测试中。但是,测试开始从

下载数据
Login2 | Password2
Login3 | Passrowd3

虽然

Login1 | Password1

从文件下载数据时省略。

有人可以指导我犯错吗?我是初学者,对于丑陋的代码感到抱歉。

ReadData.java

public class ReadData {
public ReadData() throws Exception {
    super();
}

public ArrayList readExcelData(int colNo) throws IOException, InvalidFormatException{
    OPCPackage pkg = OPCPackage.open(new File("C:\\Users\\Damian6666\\workspace846\\ChangePasswordAutomat1\\File.xlsx"));
    XSSFWorkbook wb = new XSSFWorkbook(pkg);

    XSSFSheet s = wb.getSheet("EMP_DETAILS");

    Iterator<Row> rowIterator = s.iterator();
    rowIterator.next();

    ArrayList<String> list = new ArrayList<String>();
    while(rowIterator.hasNext()){
        list.add(rowIterator.next().getCell(colNo).getStringCellValue());
    }
    System.out.println("List :::: "+list);
    return list;
}

public void tc() throws IOException, InterruptedException, InvalidFormatException{
    WebDriver driver = new FirefoxDriver();
    driver.get("https://accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=https://mail.google.com/mail/&ss=1&scc=1&ltmpl=default&ltmplcache=2&emr=1&osid=1#identifier");
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    driver.manage().window().maximize();

    ArrayList<String> userName = readExcelData(0);
    ArrayList<String> password = readExcelData(1);

    for(int i =0;i<userName.size();i++){
   // driver.findElement(By.id("f_login")).clear();
    driver.findElement(By.id("Email")).sendKeys(userName.get(i));
    driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
    driver.findElement(By.id("next")).click();
//  driver.findElement(By.id("f_password")).clear();
    driver.findElement(By.id("Passwd")).sendKeys(password.get(i));
    driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);

    driver.findElement(By.id("signIn")).click();
    driver.findElement(By.xpath(".//*[@id='gb']/div[1]/div[1]/div[2]/div[4]/div[1]/a/span")).click();
    driver.findElement(By.id("gb_71")).click();
    driver.findElement(By.id("account-chooser-link")).click();
    driver.findElement(By.id("account-chooser-add-account")).click();

    Thread.sleep(6000);
    }
}

}

JDBCConnectionDB.java

 public class JDBCConnectionDB extends ReadData {

public JDBCConnectionDB() throws Exception {
    super();
}



@Test
public void test() {    

    Connection connection = null;
    Statement stmt = null;
    int count =0;

    try{
    // Register JDBC driver
    Class.forName("org.postgresql.Driver");
    System.out.println("PostgreSQL JDBC Driver Registered!");

    // Open a connection
    System.out.println("Connecting to a selected database...");
    connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/mydb", "postgres", "");
    System.out.println("Connected database successfully...");

    // Execute a query
    System.out.println("Creating statment...");
    stmt = connection.createStatement();

    // Create 2007 format Workbook and Worksheet objects 
    XSSFWorkbook new_workbook = new XSSFWorkbook(); // create a blank workbook object
    XSSFSheet sheet = new_workbook.createSheet("EMP_DETAILS"); // create a worksheet with caption score_details

    // Define the SQL query
    String sql = "SELECT login, password FROM \"Permissions\"";
    ResultSet rs = stmt.executeQuery(sql);

    // Create Map for Excel Data
    Map<String, Object[]> excel_data = new HashMap<String, Object[]>();
    int row_counter = 0;
    //Extract data from result set
    while(rs.next()){
        row_counter = row_counter+1;
        String login = rs.getString("login");
        String password = rs.getString("password");
        excel_data.put(Integer.toString(row_counter), new Object[] {login, password});                      
    }
    rs.close();

    // Load data into logical worksheet
    Set<String> keyset = excel_data.keySet();
    int rownum = 0;
    for(String key : keyset){ // loop through the data and add them to the cell

        Row row = sheet.createRow(rownum++);
        Object [] objArr = excel_data.get(key);
        int cellnum = 0;
        for(Object obj : objArr){
            Cell cell = row.createCell(cellnum++);
            if(obj instanceof Double)
                cell.setCellValue((Double)obj);
            else
                cell.setCellValue((String)obj);
        }
    }
    FileOutputStream output_file = new FileOutputStream(new File("File.xlsx")); // create XLSX file
    new_workbook.write(output_file); // write excel document to output stream
    output_file.close(); // close the file

    ReadData data = new ReadData();
    data.tc();

    }catch(SQLException se){
        // Handle errors for JDBC
        se.printStackTrace();
    }catch (Exception e) {
        // Handle errors for Class.forName
        e.printStackTrace();
    }finally {
        // finally block used to close resources
        try {
            if(stmt!=null)
                connection.close();
        } catch (SQLException se) {
            // TODO: handle exception
        }//do nothing
        try {
            if(connection!=null)
                connection.close();
        } catch (SQLException se) {
            se.printStackTrace();       
        }//end finally try
    } // end try            
    System.out.println("Goodbye");
}   
} //end JDBCConnectionDB*/

1 个答案:

答案 0 :(得分:0)

readExcelData方法中,在循环文件行之前调用rowIterator.next();

Iterator<Row> rowIterator = s.iterator();
rowIterator.next();

这个电话似乎是多余的,看起来会跳过第一行。请尝试删除第一个rowIterator.next();电话。