0

I have code that gets data from a particular cell in a spreadsheet, but now I need to loop through a spreadsheet which has multiple rows of data in the first 2 columns:

Column A | Column B

user1 | password1

user2 | password 2

I need the value of A1 and B1 (username and password), use these values for creating a new user in my Selenium script, then move to the next row in the spreadsheet and get the values from A2 and B2, create another user using these values etc.

Below is my code for getting the value from a specified cell. I think I need to use usedRange to get the values from all occupied cells, but I don't know how to get the value from column A into a variable, strUsername, and the value from column B into a variable, strPassword, then move to the next row and repeat the operation. Any help appreciated. Thank you.

Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;

string str;

xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Open(@"C:\\Environments.xlsx", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);

xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
var cell = (Excel.Range)xlWorkSheet.Cells[19, 1];

str = (string)(cell as Excel.Range).Value2;

//Other processing goes here 

xlWorkBook.Close();
Michael Piraner
  • 67
  • 3
  • 11

0 Answers0