簡體   English   中英

從Excel讀取所有數據

[英]Read all the data from the excel

我想從第一行讀取數據並執行操作,然后寫入結果,然后讀取第二行,直到Excel文件中沒有更多數據。

我的問題:

  • 迭代器為什么不起作用?
  • (Selenium的) driver.findElement為什么不執行應做的工作?

主程序

  @Test
    public void test() throws Exception {
        String sPath = "default.xlsx";
        DataHelper.setExcelFile(sPath, "sheet1");
        XSSFRow row;
        XSSFCell cell;
        boolean flag = true;
        XSSFSheet sheet = ExcelWSheet;
        Iterator rows = sheet.rowIterator();
        while (rows.hasNext()) {
            row = (XSSFRow) rows.next();
            Iterator cells = row.cellIterator();
            List <String> obj = new ArrayList<String>();
            while (cells.hasNext()) {
               cell = (XSSFCell) cells.next();
               String cellobj = cell.getStringCellValue();
               if(flag)
               {
                   driver.findElement(By.id("text")).sendKeys(cell);
                   driver.findElement(By.id("text")).click();
                   driver.findElement(By.id("text")).clear;
               }
            }
        }   
    }

DataHelper.java

public class DataHelper {
    public static XSSFSheet ExcelWSheet;
    private static XSSFWorkbook ExcelWBook;
    private static XSSFCell cell;
    private static String path;

    public static void setExcelFile(String path, String SheetName) throws Exception {
        path = path;
        FileInputStream ExcelFile = new FileInputStream(path);
        ExcelWBook = new XSSFWorkbook(ExcelFile);
        ExcelWSheet = ExcelWBook.getSheet(SheetName);

    }

}

Excel數據

郵箱,密碼

你好123

你好1,123

在進行一般清理(過度使用靜態等)之后,迭代器可以很好地工作。 請參見下面的代碼。

關於Selenium電話,我不具備幫助的知識。 我建議您下次將此問題發布為兩個獨立的問題。 我知道的是,如果您想查看文件本身的任何更改,則需要將工作簿顯式保存到文件中。 我懷疑那是您想要的。 那里有很多容易找到和理解的例子。

public void test() throws Exception {
    String sPath = "default.xlsx";
    DataHelper dataHelper = new DataHelper(sPath, "sheet1");
    XSSFSheet sheet = dataHelper.getExcelWSheet();
    Iterator<Row> rows = sheet.rowIterator();
    while (rows.hasNext()) {
        Row row = rows.next();
        Iterator<Cell> cells = row.cellIterator();
        while (cells.hasNext()) {
            Cell cell = cells.next();
            String cellobj = cell.getStringCellValue();
            if(flag) {
                driver.findElement(By.id("text")).sendKeys(cell);
                driver.findElement(By.id("text")).click();
                driver.findElement(By.id("text")).clear;
            }                
        }
    }
}

public class DataHelper {
    private XSSFSheet excelWSheet;
    private XSSFWorkbook excelWBook;

    public DataHelper(String path, String SheetName) throws Exception {
        FileInputStream ExcelFile = new FileInputStream(path);
        excelWBook = new XSSFWorkbook(ExcelFile);
        excelWSheet = excelWBook.getSheet(SheetName);
    }

    public XSSFSheet getExcelWSheet() {
        return excelWSheet;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM