如何使用C#读取和循环Excel中的每一行

时间:2013-10-30 15:51:01

标签: c# excel

我需要读取一个excel文件并遍历行。我使用下面的代码来读取excel文件。但是我没有获得整行,因为获取列值不感兴趣。

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);
}

我需要检查行是否为空,如果不为空,我需要搜索特定列中的某些值。

感谢您的帮助

0 个答案:

没有答案
相关问题