Thursday, October 21, 2010

read excel sheet C#


Assuming txtFileName is a textbox which has the name of the excel. Make sure the sheet name is Sheet1. Please adjust the code according to your requirements.

String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + txtFileName .Text .ToString () + ";Extended Properties=Excel 8.0;";
OleDbConnection oleDBConn = new OleDbConnection(sConnectionString);
oleDBConn.Open();
OleDbCommand oleDBCmd = new OleDbCommand("SELECT * FROM [Sheet1$]", oleDBConn);
OleDbDataAdapter oledbadptr = new OleDbDataAdapter();
oledbadptr.SelectCommand = oleDBCmd;
DataSet ds1 = new DataSet();
oledbadptr.Fill(ds1);
oleDBConn.Close();

No comments:

Post a Comment