Search

Read excel file to data grid

using System;
using System.Data;
using System.Data.OleDb ;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
string connStr = "provider=Microsoft.Jet.OLEDB.4.0;Data Source=ExcelFile.xls;Extended Properties=Excel 8.0;";
OleDbConnection MyConnection;
DataSet ds;
OleDbDataAdapter MyCommand;
MyConnection = new OleDbConnection(connStr);
MyCommand = new OleDbDataAdapter("select * from [Sheet1$]", MyConnection);
ds = new System.Data.DataSet();
MyCommand.Fill(ds);
MyConnection.Close();
GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();
}
catch (Exception ex)
{
Label1.Text = ex.ToString();
}
}
}