Tuesday, September 27, 2016

WebDriver - Read from Excel

WebDriver - Read from Excel


Suppose we have an Excel like below and we want to read the values of this Excel using Selenium WebDriver:


Below is the code we used for reading the values from Excel:

ExcelFilePath is the path of Excel which we want to read. Excel_App represents the entire Microsoft Excel application. Excel_WB represents a workbook. Excel_WS represents our "Sheet1" in the workbook. In Excel_Range we are getting the cells that have contained a value at any time. Next we are getting the rowCount & colCount. Using these rowCount & colCount in the For loop we are printing the values of the cells. For more on Excel Object Model Overview you can see here.

After running the above code the values will be printed as shown below:


Make sure to add "Microsoft.CSharp" and "Microsoft.Office.Interop.Excel" to references along with other references for the above code to work.