Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Tuesday, August 21, 2012

Binding Data From Microsoft Excel WorkBook Sheet to GridView in .Net


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;

public partial class _Default : System.Web.UI.Page
{

    OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Documents and Settings\prasad.b\Desktop\sample.xls;Extended Properties='Excel 12.0;HDR=YES;'");
    OleDbDataAdapter ad;
    DataSet dSet = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GridView1.DataSource = getDataFromExcel();
            GridView1.DataBind();
        }
    }

    public DataSet getDataFromExcel()
    {
        ad = new OleDbDataAdapter("Select * from [Sheet1$]", con);
        dSet = new DataSet();
        ad.Fill(dSet);
        return dSet;
    }
}


No comments:

Post a Comment