How to read and loop through each rows in an Excel using C#

Posted by Ullan on Stack Overflow See other posts from Stack Overflow or by Ullan
Published on 2013-10-30T15:51:00Z Indexed on 2013/10/30 15:53 UTC
Read the original article Hit count: 144

Filed under:
|

I need to read an excel file and loop through the rows. I used the below code to read the excel file. But I am not getting the entire row, inseatd of getting the column values.

try
{
    OleDbConnection objConnection = new OleDbConnection(strConnectionString);
    string queryString = string.Format("Select * from [{0}$]", strSheetName, objConnection);

    OleDbCommand command = new OleDbCommand(queryString, objConnection);
    objConnection.Open();
    OleDbDataReader reader = command.ExecuteReader();

    while (reader.Read())
    {

        var val1 = reader[0].ToString(); //Here I want to get each row
    }
    reader.Close();
}
catch (Exception ex)
{
   Console.WriteLine(ex.Message);
}

I need to check if the row is empty or not, if not empty, I need to serach for some values in a particular column.

Thanks for your help

© Stack Overflow or respective owner

Related posts about c#

Related posts about excel