Wednesday, April 20, 2011

Importing From Excel File To Datagrid In ASP.NET.....:)

Here In design page I have one grid id as GVExcel.
and G:\\Yash\\WorkInProgress\\TestProject\\website1\\Student.xls path of excel file which has to import.


protected void Page_Load(object sender, EventArgs e)
    {       
        try
        {
            string conn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=G:\\Yash\\WorkInProgress\\TestProject\\website1\\Student.xls;Extended Properties=Excel 8.0;";

            DataSet ds = new DataSet();
            OleDbDataAdapter da = new OleDbDataAdapter("select * From [Sheet1$]", conn);
            da.Fill(ds);
            GVExcel.DataSource = ds;
            GVExcel.DataBind();
        }
        catch(Exception ex)
        {
        }
        finally
        {
            // Close connection
            //oledbConn.Close();
        }     
    }

Hope this code is helpful for you.

No comments:

Post a Comment