How to read and loop through each rows in an Excel using C#
- by Ullan
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