I had a small page doiing the same thing. here is the code snippet for the pages code behind.
public partial class _Default : System.Web.UI.Page
{
OleDbConnection con = null;
protected void Page_Load(object sender, EventArgs e)
{
string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\testExcel.xls;Extended Properties='Excel 8.0;HDR=YES;'";
con = new OleDbConnection(connectionString);
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
con.Open();
Label1.Text = con.State.ToString();
if (con.State == ConnectionState.Open)
{
OleDbCommand cmd = con.CreateCommand();
cmd.CommandText = "Select * from [Sheet1$]";
cmd.CommandType = CommandType.Text;
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataTable table = new DataTable();
da.Fill(table);
GridView1.DataSource = table;
GridView1.DataBind();
}
}
finally
{
con.Close();
}
}
}