How to read all the rows of the excel sheet and run in Selenium test script using C#?












1















I need help reading all the rows from excel sheet as a test data in my selenium test case.



I can read only one row of an excel sheet with the following code.



//set up test for selenium



 public void SetupTest()
{
selenium = new DefaultSelenium("localhost", 4444, "*googlechrome", "http://www.google.com/");
selenium.Start();
verificationErrors = new StringBuilder();
}


//Connects me to my excel sheet which is SampleTestData.xls



public void ConnectExcel()
{
excel.Application excelApp = new excel.Application();
excelApp.Visible = true;
string ExcelDataPath = @"C:SampleTestData.xls";
excel.Workbook excelWorkBook = excelApp.Workbooks.Open(ExcelDataPath, 0, false, 5, "", "", false, excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
excel.Sheets excelSheets = excelWorkBook.Worksheets;
excel.Worksheet DataworkSheet = (excel.Worksheet)excelSheets.get_Item("Sheet1");
System.String Search1 = ((excel.Range)DataworkSheet.get_Range("A1", Type.Missing)).Value2 as string;
}


//selenium code to run the test



public void StartTest()
{
selenium.open("");
selenium.WaitForPageToLoad("100000");
selenium.Type("gbqfif", search1);
selenium.Click("btnG");
selenium.WaitForPageToLoad("100000");

}


This code lets me select only one row that contains value of search1. However, I need to iterate it such a way that it will keep running StartTest() method until all the rows(50) in the excel sheet are entered.



Any help would be appreciated.










share|improve this question

























  • look into using a ForEach or a For Loop.. do a google search as well..

    – MethodMan
    Aug 1 '12 at 17:04











  • Look at this link and learn how to use Interop based on this example it will be a great learning experience.. stackoverflow.com/questions/11593628/…

    – MethodMan
    Aug 1 '12 at 17:07











  • Thanks DJ KRAZE, i looked at the thread but it's too complicated to understand. All i want is to loop over the excel rows which i am not able to do.

    – seleniumlover
    Aug 1 '12 at 17:09













  • I just posted 2 examples below.. it's not as complicated as you may think.. this is the easiest example I could come up with based on your fears of trying.. try the example and change it to fit your code sample above let me know if this works for you.. thanks

    – MethodMan
    Aug 1 '12 at 17:16











  • I am going to post an even easier example that you should be able to follow.. please try not to get frustrated... make sure to add the necessary references at the top of your form as well

    – MethodMan
    Aug 1 '12 at 18:05
















1















I need help reading all the rows from excel sheet as a test data in my selenium test case.



I can read only one row of an excel sheet with the following code.



//set up test for selenium



 public void SetupTest()
{
selenium = new DefaultSelenium("localhost", 4444, "*googlechrome", "http://www.google.com/");
selenium.Start();
verificationErrors = new StringBuilder();
}


//Connects me to my excel sheet which is SampleTestData.xls



public void ConnectExcel()
{
excel.Application excelApp = new excel.Application();
excelApp.Visible = true;
string ExcelDataPath = @"C:SampleTestData.xls";
excel.Workbook excelWorkBook = excelApp.Workbooks.Open(ExcelDataPath, 0, false, 5, "", "", false, excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
excel.Sheets excelSheets = excelWorkBook.Worksheets;
excel.Worksheet DataworkSheet = (excel.Worksheet)excelSheets.get_Item("Sheet1");
System.String Search1 = ((excel.Range)DataworkSheet.get_Range("A1", Type.Missing)).Value2 as string;
}


//selenium code to run the test



public void StartTest()
{
selenium.open("");
selenium.WaitForPageToLoad("100000");
selenium.Type("gbqfif", search1);
selenium.Click("btnG");
selenium.WaitForPageToLoad("100000");

}


This code lets me select only one row that contains value of search1. However, I need to iterate it such a way that it will keep running StartTest() method until all the rows(50) in the excel sheet are entered.



Any help would be appreciated.










share|improve this question

























  • look into using a ForEach or a For Loop.. do a google search as well..

    – MethodMan
    Aug 1 '12 at 17:04











  • Look at this link and learn how to use Interop based on this example it will be a great learning experience.. stackoverflow.com/questions/11593628/…

    – MethodMan
    Aug 1 '12 at 17:07











  • Thanks DJ KRAZE, i looked at the thread but it's too complicated to understand. All i want is to loop over the excel rows which i am not able to do.

    – seleniumlover
    Aug 1 '12 at 17:09













  • I just posted 2 examples below.. it's not as complicated as you may think.. this is the easiest example I could come up with based on your fears of trying.. try the example and change it to fit your code sample above let me know if this works for you.. thanks

    – MethodMan
    Aug 1 '12 at 17:16











  • I am going to post an even easier example that you should be able to follow.. please try not to get frustrated... make sure to add the necessary references at the top of your form as well

    – MethodMan
    Aug 1 '12 at 18:05














1












1








1


1






I need help reading all the rows from excel sheet as a test data in my selenium test case.



I can read only one row of an excel sheet with the following code.



//set up test for selenium



 public void SetupTest()
{
selenium = new DefaultSelenium("localhost", 4444, "*googlechrome", "http://www.google.com/");
selenium.Start();
verificationErrors = new StringBuilder();
}


//Connects me to my excel sheet which is SampleTestData.xls



public void ConnectExcel()
{
excel.Application excelApp = new excel.Application();
excelApp.Visible = true;
string ExcelDataPath = @"C:SampleTestData.xls";
excel.Workbook excelWorkBook = excelApp.Workbooks.Open(ExcelDataPath, 0, false, 5, "", "", false, excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
excel.Sheets excelSheets = excelWorkBook.Worksheets;
excel.Worksheet DataworkSheet = (excel.Worksheet)excelSheets.get_Item("Sheet1");
System.String Search1 = ((excel.Range)DataworkSheet.get_Range("A1", Type.Missing)).Value2 as string;
}


//selenium code to run the test



public void StartTest()
{
selenium.open("");
selenium.WaitForPageToLoad("100000");
selenium.Type("gbqfif", search1);
selenium.Click("btnG");
selenium.WaitForPageToLoad("100000");

}


This code lets me select only one row that contains value of search1. However, I need to iterate it such a way that it will keep running StartTest() method until all the rows(50) in the excel sheet are entered.



Any help would be appreciated.










share|improve this question
















I need help reading all the rows from excel sheet as a test data in my selenium test case.



I can read only one row of an excel sheet with the following code.



//set up test for selenium



 public void SetupTest()
{
selenium = new DefaultSelenium("localhost", 4444, "*googlechrome", "http://www.google.com/");
selenium.Start();
verificationErrors = new StringBuilder();
}


//Connects me to my excel sheet which is SampleTestData.xls



public void ConnectExcel()
{
excel.Application excelApp = new excel.Application();
excelApp.Visible = true;
string ExcelDataPath = @"C:SampleTestData.xls";
excel.Workbook excelWorkBook = excelApp.Workbooks.Open(ExcelDataPath, 0, false, 5, "", "", false, excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
excel.Sheets excelSheets = excelWorkBook.Worksheets;
excel.Worksheet DataworkSheet = (excel.Worksheet)excelSheets.get_Item("Sheet1");
System.String Search1 = ((excel.Range)DataworkSheet.get_Range("A1", Type.Missing)).Value2 as string;
}


//selenium code to run the test



public void StartTest()
{
selenium.open("");
selenium.WaitForPageToLoad("100000");
selenium.Type("gbqfif", search1);
selenium.Click("btnG");
selenium.WaitForPageToLoad("100000");

}


This code lets me select only one row that contains value of search1. However, I need to iterate it such a way that it will keep running StartTest() method until all the rows(50) in the excel sheet are entered.



Any help would be appreciated.







c# excel c#-4.0 selenium-rc selenium-webdriver






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 18 '17 at 4:00









Ripon Al Wasim

26.1k31127151




26.1k31127151










asked Aug 1 '12 at 16:49









seleniumloverseleniumlover

74314




74314













  • look into using a ForEach or a For Loop.. do a google search as well..

    – MethodMan
    Aug 1 '12 at 17:04











  • Look at this link and learn how to use Interop based on this example it will be a great learning experience.. stackoverflow.com/questions/11593628/…

    – MethodMan
    Aug 1 '12 at 17:07











  • Thanks DJ KRAZE, i looked at the thread but it's too complicated to understand. All i want is to loop over the excel rows which i am not able to do.

    – seleniumlover
    Aug 1 '12 at 17:09













  • I just posted 2 examples below.. it's not as complicated as you may think.. this is the easiest example I could come up with based on your fears of trying.. try the example and change it to fit your code sample above let me know if this works for you.. thanks

    – MethodMan
    Aug 1 '12 at 17:16











  • I am going to post an even easier example that you should be able to follow.. please try not to get frustrated... make sure to add the necessary references at the top of your form as well

    – MethodMan
    Aug 1 '12 at 18:05



















  • look into using a ForEach or a For Loop.. do a google search as well..

    – MethodMan
    Aug 1 '12 at 17:04











  • Look at this link and learn how to use Interop based on this example it will be a great learning experience.. stackoverflow.com/questions/11593628/…

    – MethodMan
    Aug 1 '12 at 17:07











  • Thanks DJ KRAZE, i looked at the thread but it's too complicated to understand. All i want is to loop over the excel rows which i am not able to do.

    – seleniumlover
    Aug 1 '12 at 17:09













  • I just posted 2 examples below.. it's not as complicated as you may think.. this is the easiest example I could come up with based on your fears of trying.. try the example and change it to fit your code sample above let me know if this works for you.. thanks

    – MethodMan
    Aug 1 '12 at 17:16











  • I am going to post an even easier example that you should be able to follow.. please try not to get frustrated... make sure to add the necessary references at the top of your form as well

    – MethodMan
    Aug 1 '12 at 18:05

















look into using a ForEach or a For Loop.. do a google search as well..

– MethodMan
Aug 1 '12 at 17:04





look into using a ForEach or a For Loop.. do a google search as well..

– MethodMan
Aug 1 '12 at 17:04













Look at this link and learn how to use Interop based on this example it will be a great learning experience.. stackoverflow.com/questions/11593628/…

– MethodMan
Aug 1 '12 at 17:07





Look at this link and learn how to use Interop based on this example it will be a great learning experience.. stackoverflow.com/questions/11593628/…

– MethodMan
Aug 1 '12 at 17:07













Thanks DJ KRAZE, i looked at the thread but it's too complicated to understand. All i want is to loop over the excel rows which i am not able to do.

– seleniumlover
Aug 1 '12 at 17:09







Thanks DJ KRAZE, i looked at the thread but it's too complicated to understand. All i want is to loop over the excel rows which i am not able to do.

– seleniumlover
Aug 1 '12 at 17:09















I just posted 2 examples below.. it's not as complicated as you may think.. this is the easiest example I could come up with based on your fears of trying.. try the example and change it to fit your code sample above let me know if this works for you.. thanks

– MethodMan
Aug 1 '12 at 17:16





I just posted 2 examples below.. it's not as complicated as you may think.. this is the easiest example I could come up with based on your fears of trying.. try the example and change it to fit your code sample above let me know if this works for you.. thanks

– MethodMan
Aug 1 '12 at 17:16













I am going to post an even easier example that you should be able to follow.. please try not to get frustrated... make sure to add the necessary references at the top of your form as well

– MethodMan
Aug 1 '12 at 18:05





I am going to post an even easier example that you should be able to follow.. please try not to get frustrated... make sure to add the necessary references at the top of your form as well

– MethodMan
Aug 1 '12 at 18:05












2 Answers
2






active

oldest

votes


















0














This answer may or may not help you but I am going to take a chance and post 2 samples of code that you can use..



Example #1



using System;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Excel.Application xlApp ;
Excel.Workbook xlWorkBook ;
Excel.Worksheet xlWorkSheet ;
Excel.Range range ;

string str;
int rCnt = 0;
int cCnt = 0;

xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Open("csharp.net-informations.xls", 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);

range = xlWorkSheet.UsedRange;

for (rCnt = 1; rCnt <= range.Rows.Count; rCnt++)
{
for (cCnt = 1; cCnt <= range.Columns.Count; cCnt++)
{
str = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value2 ;
MessageBox.Show(str);
}
}

xlWorkBook.Close(true, null, null);
xlApp.Quit();

releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
}

private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show("Unable to release the Object " + ex.ToString());
}
finally
{
GC.Collect();
}
}

}
}


Example #2



FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read);

//1. Reading from a binary Excel file ('97-2003 format; *.xls)
IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
//...
//2. Reading from a OpenXml Excel file (2007 format; *.xlsx)
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
//...
//3. DataSet - The result of each spreadsheet will be created in the result.Tables
DataSet result = excelReader.AsDataSet();
//...
//4. DataSet - Create column names from first row
excelReader.IsFirstRowAsColumnNames = true;
DataSet result = excelReader.AsDataSet();

//5. Data Reader methods
while (excelReader.Read())
{
//excelReader.GetInt32(0);
}

//6. Free resources (IExcelDataReader is IDisposable)
excelReader.Close();


Example #3



using (FileStream fileStream = File.Open(inputFilenames[0], FileMode.Open, FileAccess.Read))
{
IExcelDataReader excelReader;
if (Path.GetExtension(inputFilenames[0]) == ".xls")
excelReader = Factory.CreateReader(fileStream, ExcelFileType.Binary);
else
excelReader = Factory.CreateReader(fileStream, ExcelFileType.OpenXml);

excelReader.NextResult();
while (excelReader.Name != this.Worksheet)
excelReader.NextResult();

while (excelReader.Read())
{
if (FirstRowHasColumnNames)
{
FirstRowHasColumnNames = false;
}
else
{
//do stuff
var test = GetColumnData(excelReader, 1);
}
}

this.Save(outputFilename);
}


you can look at this link as well
Reading from Excel File






share|improve this answer


























  • Thanks DJ KRAZE for your answer. I need to declare variable for the row 1 inside while loop in example 1 so that the variable changes as per the iteration, how can i do that?

    – seleniumlover
    Aug 1 '12 at 17:18













  • be more specific.. are you referring to my example or something else.. edit your initial post with the loop that you are using so that I can help you from there.. thanks

    – MethodMan
    Aug 1 '12 at 17:22











  • I'm referring to your 1st example, how can you associate a variable to the row such that it reads all the row in the while loop.

    – seleniumlover
    Aug 1 '12 at 17:30













  • Also i'm getting error for IExcelDataReader. What reference do i need to add so that it is considered as an object?

    – seleniumlover
    Aug 1 '12 at 17:54











  • did you add a using statement at the top of the project something like this..this is called Aliasing using Excel = Microsoft.Office.Interop.Excel;

    – MethodMan
    Aug 1 '12 at 18:02



















0














Try this:-



        //Reading from a OpenXml Excel file (2007 format; *.xlsx)
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);

// DataSet - Create column names from first row
excelReader.IsFirstRowAsColumnNames = true;
DataSet result = excelReader.AsDataSet();

while (excelReader.Read())
{ // Reading from row and get columns by index
var test = excelReader.GetString(1);

}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f11763919%2fhow-to-read-all-the-rows-of-the-excel-sheet-and-run-in-selenium-test-script-usin%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    This answer may or may not help you but I am going to take a chance and post 2 samples of code that you can use..



    Example #1



    using System;
    using System.Windows.Forms;
    using Excel = Microsoft.Office.Interop.Excel;

    namespace WindowsApplication1
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
    Excel.Application xlApp ;
    Excel.Workbook xlWorkBook ;
    Excel.Worksheet xlWorkSheet ;
    Excel.Range range ;

    string str;
    int rCnt = 0;
    int cCnt = 0;

    xlApp = new Excel.ApplicationClass();
    xlWorkBook = xlApp.Workbooks.Open("csharp.net-informations.xls", 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);

    range = xlWorkSheet.UsedRange;

    for (rCnt = 1; rCnt <= range.Rows.Count; rCnt++)
    {
    for (cCnt = 1; cCnt <= range.Columns.Count; cCnt++)
    {
    str = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value2 ;
    MessageBox.Show(str);
    }
    }

    xlWorkBook.Close(true, null, null);
    xlApp.Quit();

    releaseObject(xlWorkSheet);
    releaseObject(xlWorkBook);
    releaseObject(xlApp);
    }

    private void releaseObject(object obj)
    {
    try
    {
    System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
    obj = null;
    }
    catch (Exception ex)
    {
    obj = null;
    MessageBox.Show("Unable to release the Object " + ex.ToString());
    }
    finally
    {
    GC.Collect();
    }
    }

    }
    }


    Example #2



    FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read);

    //1. Reading from a binary Excel file ('97-2003 format; *.xls)
    IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
    //...
    //2. Reading from a OpenXml Excel file (2007 format; *.xlsx)
    IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
    //...
    //3. DataSet - The result of each spreadsheet will be created in the result.Tables
    DataSet result = excelReader.AsDataSet();
    //...
    //4. DataSet - Create column names from first row
    excelReader.IsFirstRowAsColumnNames = true;
    DataSet result = excelReader.AsDataSet();

    //5. Data Reader methods
    while (excelReader.Read())
    {
    //excelReader.GetInt32(0);
    }

    //6. Free resources (IExcelDataReader is IDisposable)
    excelReader.Close();


    Example #3



    using (FileStream fileStream = File.Open(inputFilenames[0], FileMode.Open, FileAccess.Read))
    {
    IExcelDataReader excelReader;
    if (Path.GetExtension(inputFilenames[0]) == ".xls")
    excelReader = Factory.CreateReader(fileStream, ExcelFileType.Binary);
    else
    excelReader = Factory.CreateReader(fileStream, ExcelFileType.OpenXml);

    excelReader.NextResult();
    while (excelReader.Name != this.Worksheet)
    excelReader.NextResult();

    while (excelReader.Read())
    {
    if (FirstRowHasColumnNames)
    {
    FirstRowHasColumnNames = false;
    }
    else
    {
    //do stuff
    var test = GetColumnData(excelReader, 1);
    }
    }

    this.Save(outputFilename);
    }


    you can look at this link as well
    Reading from Excel File






    share|improve this answer


























    • Thanks DJ KRAZE for your answer. I need to declare variable for the row 1 inside while loop in example 1 so that the variable changes as per the iteration, how can i do that?

      – seleniumlover
      Aug 1 '12 at 17:18













    • be more specific.. are you referring to my example or something else.. edit your initial post with the loop that you are using so that I can help you from there.. thanks

      – MethodMan
      Aug 1 '12 at 17:22











    • I'm referring to your 1st example, how can you associate a variable to the row such that it reads all the row in the while loop.

      – seleniumlover
      Aug 1 '12 at 17:30













    • Also i'm getting error for IExcelDataReader. What reference do i need to add so that it is considered as an object?

      – seleniumlover
      Aug 1 '12 at 17:54











    • did you add a using statement at the top of the project something like this..this is called Aliasing using Excel = Microsoft.Office.Interop.Excel;

      – MethodMan
      Aug 1 '12 at 18:02
















    0














    This answer may or may not help you but I am going to take a chance and post 2 samples of code that you can use..



    Example #1



    using System;
    using System.Windows.Forms;
    using Excel = Microsoft.Office.Interop.Excel;

    namespace WindowsApplication1
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
    Excel.Application xlApp ;
    Excel.Workbook xlWorkBook ;
    Excel.Worksheet xlWorkSheet ;
    Excel.Range range ;

    string str;
    int rCnt = 0;
    int cCnt = 0;

    xlApp = new Excel.ApplicationClass();
    xlWorkBook = xlApp.Workbooks.Open("csharp.net-informations.xls", 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);

    range = xlWorkSheet.UsedRange;

    for (rCnt = 1; rCnt <= range.Rows.Count; rCnt++)
    {
    for (cCnt = 1; cCnt <= range.Columns.Count; cCnt++)
    {
    str = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value2 ;
    MessageBox.Show(str);
    }
    }

    xlWorkBook.Close(true, null, null);
    xlApp.Quit();

    releaseObject(xlWorkSheet);
    releaseObject(xlWorkBook);
    releaseObject(xlApp);
    }

    private void releaseObject(object obj)
    {
    try
    {
    System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
    obj = null;
    }
    catch (Exception ex)
    {
    obj = null;
    MessageBox.Show("Unable to release the Object " + ex.ToString());
    }
    finally
    {
    GC.Collect();
    }
    }

    }
    }


    Example #2



    FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read);

    //1. Reading from a binary Excel file ('97-2003 format; *.xls)
    IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
    //...
    //2. Reading from a OpenXml Excel file (2007 format; *.xlsx)
    IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
    //...
    //3. DataSet - The result of each spreadsheet will be created in the result.Tables
    DataSet result = excelReader.AsDataSet();
    //...
    //4. DataSet - Create column names from first row
    excelReader.IsFirstRowAsColumnNames = true;
    DataSet result = excelReader.AsDataSet();

    //5. Data Reader methods
    while (excelReader.Read())
    {
    //excelReader.GetInt32(0);
    }

    //6. Free resources (IExcelDataReader is IDisposable)
    excelReader.Close();


    Example #3



    using (FileStream fileStream = File.Open(inputFilenames[0], FileMode.Open, FileAccess.Read))
    {
    IExcelDataReader excelReader;
    if (Path.GetExtension(inputFilenames[0]) == ".xls")
    excelReader = Factory.CreateReader(fileStream, ExcelFileType.Binary);
    else
    excelReader = Factory.CreateReader(fileStream, ExcelFileType.OpenXml);

    excelReader.NextResult();
    while (excelReader.Name != this.Worksheet)
    excelReader.NextResult();

    while (excelReader.Read())
    {
    if (FirstRowHasColumnNames)
    {
    FirstRowHasColumnNames = false;
    }
    else
    {
    //do stuff
    var test = GetColumnData(excelReader, 1);
    }
    }

    this.Save(outputFilename);
    }


    you can look at this link as well
    Reading from Excel File






    share|improve this answer


























    • Thanks DJ KRAZE for your answer. I need to declare variable for the row 1 inside while loop in example 1 so that the variable changes as per the iteration, how can i do that?

      – seleniumlover
      Aug 1 '12 at 17:18













    • be more specific.. are you referring to my example or something else.. edit your initial post with the loop that you are using so that I can help you from there.. thanks

      – MethodMan
      Aug 1 '12 at 17:22











    • I'm referring to your 1st example, how can you associate a variable to the row such that it reads all the row in the while loop.

      – seleniumlover
      Aug 1 '12 at 17:30













    • Also i'm getting error for IExcelDataReader. What reference do i need to add so that it is considered as an object?

      – seleniumlover
      Aug 1 '12 at 17:54











    • did you add a using statement at the top of the project something like this..this is called Aliasing using Excel = Microsoft.Office.Interop.Excel;

      – MethodMan
      Aug 1 '12 at 18:02














    0












    0








    0







    This answer may or may not help you but I am going to take a chance and post 2 samples of code that you can use..



    Example #1



    using System;
    using System.Windows.Forms;
    using Excel = Microsoft.Office.Interop.Excel;

    namespace WindowsApplication1
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
    Excel.Application xlApp ;
    Excel.Workbook xlWorkBook ;
    Excel.Worksheet xlWorkSheet ;
    Excel.Range range ;

    string str;
    int rCnt = 0;
    int cCnt = 0;

    xlApp = new Excel.ApplicationClass();
    xlWorkBook = xlApp.Workbooks.Open("csharp.net-informations.xls", 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);

    range = xlWorkSheet.UsedRange;

    for (rCnt = 1; rCnt <= range.Rows.Count; rCnt++)
    {
    for (cCnt = 1; cCnt <= range.Columns.Count; cCnt++)
    {
    str = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value2 ;
    MessageBox.Show(str);
    }
    }

    xlWorkBook.Close(true, null, null);
    xlApp.Quit();

    releaseObject(xlWorkSheet);
    releaseObject(xlWorkBook);
    releaseObject(xlApp);
    }

    private void releaseObject(object obj)
    {
    try
    {
    System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
    obj = null;
    }
    catch (Exception ex)
    {
    obj = null;
    MessageBox.Show("Unable to release the Object " + ex.ToString());
    }
    finally
    {
    GC.Collect();
    }
    }

    }
    }


    Example #2



    FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read);

    //1. Reading from a binary Excel file ('97-2003 format; *.xls)
    IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
    //...
    //2. Reading from a OpenXml Excel file (2007 format; *.xlsx)
    IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
    //...
    //3. DataSet - The result of each spreadsheet will be created in the result.Tables
    DataSet result = excelReader.AsDataSet();
    //...
    //4. DataSet - Create column names from first row
    excelReader.IsFirstRowAsColumnNames = true;
    DataSet result = excelReader.AsDataSet();

    //5. Data Reader methods
    while (excelReader.Read())
    {
    //excelReader.GetInt32(0);
    }

    //6. Free resources (IExcelDataReader is IDisposable)
    excelReader.Close();


    Example #3



    using (FileStream fileStream = File.Open(inputFilenames[0], FileMode.Open, FileAccess.Read))
    {
    IExcelDataReader excelReader;
    if (Path.GetExtension(inputFilenames[0]) == ".xls")
    excelReader = Factory.CreateReader(fileStream, ExcelFileType.Binary);
    else
    excelReader = Factory.CreateReader(fileStream, ExcelFileType.OpenXml);

    excelReader.NextResult();
    while (excelReader.Name != this.Worksheet)
    excelReader.NextResult();

    while (excelReader.Read())
    {
    if (FirstRowHasColumnNames)
    {
    FirstRowHasColumnNames = false;
    }
    else
    {
    //do stuff
    var test = GetColumnData(excelReader, 1);
    }
    }

    this.Save(outputFilename);
    }


    you can look at this link as well
    Reading from Excel File






    share|improve this answer















    This answer may or may not help you but I am going to take a chance and post 2 samples of code that you can use..



    Example #1



    using System;
    using System.Windows.Forms;
    using Excel = Microsoft.Office.Interop.Excel;

    namespace WindowsApplication1
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
    Excel.Application xlApp ;
    Excel.Workbook xlWorkBook ;
    Excel.Worksheet xlWorkSheet ;
    Excel.Range range ;

    string str;
    int rCnt = 0;
    int cCnt = 0;

    xlApp = new Excel.ApplicationClass();
    xlWorkBook = xlApp.Workbooks.Open("csharp.net-informations.xls", 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);

    range = xlWorkSheet.UsedRange;

    for (rCnt = 1; rCnt <= range.Rows.Count; rCnt++)
    {
    for (cCnt = 1; cCnt <= range.Columns.Count; cCnt++)
    {
    str = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value2 ;
    MessageBox.Show(str);
    }
    }

    xlWorkBook.Close(true, null, null);
    xlApp.Quit();

    releaseObject(xlWorkSheet);
    releaseObject(xlWorkBook);
    releaseObject(xlApp);
    }

    private void releaseObject(object obj)
    {
    try
    {
    System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
    obj = null;
    }
    catch (Exception ex)
    {
    obj = null;
    MessageBox.Show("Unable to release the Object " + ex.ToString());
    }
    finally
    {
    GC.Collect();
    }
    }

    }
    }


    Example #2



    FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read);

    //1. Reading from a binary Excel file ('97-2003 format; *.xls)
    IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
    //...
    //2. Reading from a OpenXml Excel file (2007 format; *.xlsx)
    IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
    //...
    //3. DataSet - The result of each spreadsheet will be created in the result.Tables
    DataSet result = excelReader.AsDataSet();
    //...
    //4. DataSet - Create column names from first row
    excelReader.IsFirstRowAsColumnNames = true;
    DataSet result = excelReader.AsDataSet();

    //5. Data Reader methods
    while (excelReader.Read())
    {
    //excelReader.GetInt32(0);
    }

    //6. Free resources (IExcelDataReader is IDisposable)
    excelReader.Close();


    Example #3



    using (FileStream fileStream = File.Open(inputFilenames[0], FileMode.Open, FileAccess.Read))
    {
    IExcelDataReader excelReader;
    if (Path.GetExtension(inputFilenames[0]) == ".xls")
    excelReader = Factory.CreateReader(fileStream, ExcelFileType.Binary);
    else
    excelReader = Factory.CreateReader(fileStream, ExcelFileType.OpenXml);

    excelReader.NextResult();
    while (excelReader.Name != this.Worksheet)
    excelReader.NextResult();

    while (excelReader.Read())
    {
    if (FirstRowHasColumnNames)
    {
    FirstRowHasColumnNames = false;
    }
    else
    {
    //do stuff
    var test = GetColumnData(excelReader, 1);
    }
    }

    this.Save(outputFilename);
    }


    you can look at this link as well
    Reading from Excel File







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Aug 1 '12 at 18:03

























    answered Aug 1 '12 at 17:15









    MethodManMethodMan

    15.8k62749




    15.8k62749













    • Thanks DJ KRAZE for your answer. I need to declare variable for the row 1 inside while loop in example 1 so that the variable changes as per the iteration, how can i do that?

      – seleniumlover
      Aug 1 '12 at 17:18













    • be more specific.. are you referring to my example or something else.. edit your initial post with the loop that you are using so that I can help you from there.. thanks

      – MethodMan
      Aug 1 '12 at 17:22











    • I'm referring to your 1st example, how can you associate a variable to the row such that it reads all the row in the while loop.

      – seleniumlover
      Aug 1 '12 at 17:30













    • Also i'm getting error for IExcelDataReader. What reference do i need to add so that it is considered as an object?

      – seleniumlover
      Aug 1 '12 at 17:54











    • did you add a using statement at the top of the project something like this..this is called Aliasing using Excel = Microsoft.Office.Interop.Excel;

      – MethodMan
      Aug 1 '12 at 18:02



















    • Thanks DJ KRAZE for your answer. I need to declare variable for the row 1 inside while loop in example 1 so that the variable changes as per the iteration, how can i do that?

      – seleniumlover
      Aug 1 '12 at 17:18













    • be more specific.. are you referring to my example or something else.. edit your initial post with the loop that you are using so that I can help you from there.. thanks

      – MethodMan
      Aug 1 '12 at 17:22











    • I'm referring to your 1st example, how can you associate a variable to the row such that it reads all the row in the while loop.

      – seleniumlover
      Aug 1 '12 at 17:30













    • Also i'm getting error for IExcelDataReader. What reference do i need to add so that it is considered as an object?

      – seleniumlover
      Aug 1 '12 at 17:54











    • did you add a using statement at the top of the project something like this..this is called Aliasing using Excel = Microsoft.Office.Interop.Excel;

      – MethodMan
      Aug 1 '12 at 18:02

















    Thanks DJ KRAZE for your answer. I need to declare variable for the row 1 inside while loop in example 1 so that the variable changes as per the iteration, how can i do that?

    – seleniumlover
    Aug 1 '12 at 17:18







    Thanks DJ KRAZE for your answer. I need to declare variable for the row 1 inside while loop in example 1 so that the variable changes as per the iteration, how can i do that?

    – seleniumlover
    Aug 1 '12 at 17:18















    be more specific.. are you referring to my example or something else.. edit your initial post with the loop that you are using so that I can help you from there.. thanks

    – MethodMan
    Aug 1 '12 at 17:22





    be more specific.. are you referring to my example or something else.. edit your initial post with the loop that you are using so that I can help you from there.. thanks

    – MethodMan
    Aug 1 '12 at 17:22













    I'm referring to your 1st example, how can you associate a variable to the row such that it reads all the row in the while loop.

    – seleniumlover
    Aug 1 '12 at 17:30







    I'm referring to your 1st example, how can you associate a variable to the row such that it reads all the row in the while loop.

    – seleniumlover
    Aug 1 '12 at 17:30















    Also i'm getting error for IExcelDataReader. What reference do i need to add so that it is considered as an object?

    – seleniumlover
    Aug 1 '12 at 17:54





    Also i'm getting error for IExcelDataReader. What reference do i need to add so that it is considered as an object?

    – seleniumlover
    Aug 1 '12 at 17:54













    did you add a using statement at the top of the project something like this..this is called Aliasing using Excel = Microsoft.Office.Interop.Excel;

    – MethodMan
    Aug 1 '12 at 18:02





    did you add a using statement at the top of the project something like this..this is called Aliasing using Excel = Microsoft.Office.Interop.Excel;

    – MethodMan
    Aug 1 '12 at 18:02













    0














    Try this:-



            //Reading from a OpenXml Excel file (2007 format; *.xlsx)
    IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);

    // DataSet - Create column names from first row
    excelReader.IsFirstRowAsColumnNames = true;
    DataSet result = excelReader.AsDataSet();

    while (excelReader.Read())
    { // Reading from row and get columns by index
    var test = excelReader.GetString(1);

    }





    share|improve this answer




























      0














      Try this:-



              //Reading from a OpenXml Excel file (2007 format; *.xlsx)
      IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);

      // DataSet - Create column names from first row
      excelReader.IsFirstRowAsColumnNames = true;
      DataSet result = excelReader.AsDataSet();

      while (excelReader.Read())
      { // Reading from row and get columns by index
      var test = excelReader.GetString(1);

      }





      share|improve this answer


























        0












        0








        0







        Try this:-



                //Reading from a OpenXml Excel file (2007 format; *.xlsx)
        IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);

        // DataSet - Create column names from first row
        excelReader.IsFirstRowAsColumnNames = true;
        DataSet result = excelReader.AsDataSet();

        while (excelReader.Read())
        { // Reading from row and get columns by index
        var test = excelReader.GetString(1);

        }





        share|improve this answer













        Try this:-



                //Reading from a OpenXml Excel file (2007 format; *.xlsx)
        IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);

        // DataSet - Create column names from first row
        excelReader.IsFirstRowAsColumnNames = true;
        DataSet result = excelReader.AsDataSet();

        while (excelReader.Read())
        { // Reading from row and get columns by index
        var test = excelReader.GetString(1);

        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Sep 16 '16 at 19:48









        Ghanendra Singh ChaudharyGhanendra Singh Chaudhary

        135




        135






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f11763919%2fhow-to-read-all-the-rows-of-the-excel-sheet-and-run-in-selenium-test-script-usin%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Contact image not getting when fetch all contact list from iPhone by CNContact

            count number of partitions of a set with n elements into k subsets

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks