How to read an excel file in a console application in c#?

Asked

Viewed 469 times

2

Guys, I’m trying to read a data from an excel table in c#,my code is like this:

            string resp;
            string rota= "D:/Users/e608871/documents/visual studio 2010/Projects/CEMIGID/CEMIGID/CEMIGID.xlsx";
            var excel = new ExcelQueryFactory(rota);
            var res = (from row in excel.Worksheet("Sheet1")
                       let item = new Program
                       {

                           ID = row[0].Cast<string>(),
                           diretorio = row[1].Cast<string>()
                       }
                       select item).ToList();
            excel.Dispose();
            foreach (var item in res)
            {
                resp = item.ID;
            }

but it doesn’t read, it just opens the console and shows nothing

  • 2

    It’s because there’s no Console.WriteLine() ai. What did you want him to show?

  • it is pq I am debugging and in debug it is not getting the values

1 answer

0

Assuming that the variable res has value, you can display them using the Console Writeline method (whereas your application is Console).

foreach (var item in res)
{
   Console.WriteLine(item.ID);
}

Inside the foreach you are assigning resp = item.ID knows that it is always overwriting the value of the variable resp, right?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.