简体   繁体   中英

How to read an excel and fetch multiple values and concatenate them using data-provider in selenium web driver and testNG

I want to read an excel and fetch 10 records at a time and concatenate them too using data-provider in selenium web driver and testNG. For example, Excel sheet having two columns ie, col 1, col 2 and 10 rows. Now I want to pass the fetched result from an excel as value into the text-box using selenium web-driver ie, "A,B,C,D,E,F,G,H,J,I," as value. Here, row1 col1 contains "A" and row 1 col 2 contains "," . Similarly, it is like this for rest of the rows and cols. Can you please provide me the code for this logic.

Connect to excel file using JDBC driver and use it as database by SQL:

how-to-connect-to-excel-spreadsheet

read and write data from excel   
 import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;

    import org.apache.poi.xssf.usermodel.XSSFCell;
    import org.apache.poi.xssf.usermodel.XSSFRow;
    import org.apache.poi.xssf.usermodel.XSSFSheet;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;

    public class ExcelReader {
        public FileInputStream fis = null;
        public FileOutputStream fout = null;
        private XSSFWorkbook workbook = null;
        private XSSFSheet sheet = null;
        private XSSFRow row = null;
        private XSSFCell cell = null;
        String path = null;

        public ExcelReader() throws IOException {
            path = System.getProperty("user.dir") + "\\testdata\\testdata.xlsx";
            fis = new FileInputStream(path);//FileInputStream is used to read excel.
            workbook = new XSSFWorkbook(fis);//workbook is excel file include multiple sheet
            sheet = workbook.getSheetAt(0);//focusing on particular sheet in workbook.
        }
    //get number of rows in selected sheet.
        public int getSheetRows(String sheetName) {
            int index = workbook.getSheetIndex(sheetName);
            workbook.getSheetAt(index);
            return (sheet.getLastRowNum() + 1);
        }
    //get number of columns in selected sheet
        public int getSheetColumn(String sheetName) {
            int index = workbook.getSheetIndex(sheetName);
            workbook.getSheetAt(index);
            row = sheet.getRow(0);
            return (row.getLastCellNum());
        }
    //get the values in cell by using sheet name and row number and column number
        public String getCellData(String sheetName, int rowNum, int colNum) {
            int index = workbook.getSheetIndex(sheetName);
            sheet = workbook.getSheetAt(index);
            row = sheet.getRow(rowNum);
            cell = row.getCell(colNum);
            return (cell.getStringCellValue());
        }
    //get the values form cell by using the sheetname and row number and column name 
        public String getCellData(String sheetName, int rowNum, String ColName) {
            int CellNum = -1;
            int index = workbook.getSheetIndex(sheetName);
            sheet = workbook.getSheetAt(index);

            for (int i = 0; i < getSheetColumn(sheetName); i++) {
                row = sheet.getRow(0);
                cell = row.getCell(i);
                if (cell.getStringCellValue().equals(ColName)) {
                    CellNum = cell.getColumnIndex();
                    break;
                }
            }
            row = sheet.getRow(rowNum);
            cell = row.getCell(CellNum);
            return (cell.getStringCellValue());

        }
    //write the data into sheet.
        public void setData(String sheetName, int ColNum, int rowNum, String str) {
            int index = workbook.getSheetIndex(sheetName);
            sheet = workbook.getSheetAt(index);

            row = sheet.getRow(rowNum);
            cell = row.createCell(ColNum);
            cell.setCellValue(str);

            try {
                fout = new FileOutputStream(path);

                try {
                    workbook.write(fout);
                    fout.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } catch (FileNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

        }

        public static void main(String[] args) throws IOException {
            ExcelReader reader = new ExcelReader();

              System.out.println(reader.getSheetRows("LoginTest"));
              System.out.println(reader.getSheetColumn("LoginTest"));
              System.out.println(reader.getSheetRows("SignUPTest"));
              System.out.println(reader.getSheetColumn("SignUpTest"));
              System.out.println(reader.getCellData("LoginTest", 1, 0));
             System.out.println(reader.getCellData("LoginTest", 2, "password"));
            reader.setData("LoginTest", 1, 1, "Hello");
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM