Read Excel Using OleDbConnection in C#

string fileName = Path.Combine(System.Windows.Forms.Application.StartupPath, "YourExcelFileName.xlsx");
string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties=\"Excel 12.0;HDR=No;IMEX=1\";";

//code to read the content of format file 
OleDbConnection con = new OleDbConnection(strConn);

System.Data.DataTable tblTable = new System.Data.DataTable();
OleDbDataAdapter myCommand = new OleDbDataAdapter("select * from [SheetName$]", con);

try
{
    con.Open();
    myCommand.Fill(tblTable);
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message); return;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s