How to read data from excel file using c#?

using OleDB and use the Excel file like a datatable in a database you can read data from excel file.

Like see in below example:


string con = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=path;
Extended Properties='Excel8.0;HDR=Yes;'"
using(OleDbConnection cn= new OleDbConnection(con))
{
cn.Open();
OleDbCommand olcmd = new OleDbCommand("select * from [Sheet1$]", cn)
using(OleDbDataReader dr = olcmd.ExecuteReader())
{
while(dr.Read())
{
var row1Col0 = dr[0];
Console.WriteLine(row1Col0);
}
}
}

Above example use the Microsoft.Jet.OleDb.4.0 provider to open and read the Excel file.

About the Author

avatar