Saturday, March 6, 2010

Import Excel Sheet into a .NET C# page

I got my idea from here
http://www.java2s.com/Code/ASP/ADO.net-Database/LoaddatafromExceldatasourceC.htm
It actually worked beautifully, I read it straight into a gridview now I can deal with the data normally

protected void Page_Load(object sender, EventArgs e)
{
string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Book1.xls; Extended Properties=""Excel 8.0;HDR=Yes"";";
string CommandText = "select * from [Sheet1$]";

OleDbConnection myConnection = new OleDbConnection(ConnectionString);
OleDbCommand myCommand = new OleDbCommand(CommandText, myConnection);

myConnection.Open();

GridView1.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
GridView1.DataBind();

myConnection.Close();
}

No comments:

Post a Comment