Friday, December 11, 2009

Importing from excel sheet to program/dataset

Hai,

Try it...

using System.Data.OleDb;

string connectionString = "Provider=Microsoft.Jet.OleDb.4.0; Data Source=D:/Book11.xls; Extended Properties=Excel 8.0;";

using (OleDbConnection Connection = new OleDbConnection(connectionString))
{
Connection.Open();
using(OleDbCommand command = new OleDbCommand())
{
command.Connection = Connection;
command.CommandText = "select * from [Sheet1$]";
//Name of the sheet(sheet1), '$' specify it is an excel sheet. otherwise consider as tablename.
DataSet ds = new DataSet();
OleDbDataAdapter da = new OleDbDataAdapter(command);
da.Fill(ds);
//show it in a gridview
GridView1.DataSource = ds;
GridView1.DataBind();
Connection.Close();
}
}

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home