如何使用selenium从excel复制特定的整行

时间:2016-01-06 05:26:04

标签: selenium-webdriver apache-poi

我在编码时遇到问题,要从Excel工作表中选择特定的整行并将其粘贴到Web应用程序页面中。

1 个答案:

答案 0 :(得分:0)

您可以使用rowIterator逐行读取,并根据条件选择所需的特定行。

选择所需的行后,使用cellIterator作为Selenium命令的输入值,以便在网页中发布值。

使用Apache POI的示例read excel程序:

package com.howtodoinjava.demo.poi;
//import statements
public class ReadExcelDemo
{
    public static void main(String[] args)
    {
        try
        {
            FileInputStream file = new FileInputStream(new File("howtodoinjava_demo.xlsx"));

            //Create Workbook instance holding reference to .xlsx file
            XSSFWorkbook workbook = new XSSFWorkbook(file);

            //Get first/desired sheet from the workbook
            XSSFSheet sheet = workbook.getSheetAt(0);

            //Iterate through each rows one by one
            Iterator<Row> rowIterator = sheet.iterator();
            while (rowIterator.hasNext())
            {
                Row row = rowIterator.next();
                //For each row, iterate through all the columns
                Iterator<Cell> cellIterator = row.cellIterator();

                while (cellIterator.hasNext())
                {
                    Cell cell = cellIterator.next();
                    //Check the cell type and format accordingly
                    switch (cell.getCellType())
                    {
                        case Cell.CELL_TYPE_NUMERIC:
                            System.out.print(cell.getNumericCellValue() + "t");
                            break;
                        case Cell.CELL_TYPE_STRING:
                            System.out.print(cell.getStringCellValue() + "t");
                            break;
                    }
                }
                System.out.println("");
            }
            file.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

来源:http://howtodoinjava.com/2013/06/19/readingwriting-excel-files-in-java-poi-tutorial/