Pages

Tuesday, 19 February 2013

How to Retrieve data From the Excel

Copy These code and paste in to Button Event...Take One grid View then Bind the data....




OleDbConnection conn = new OleDbConnection("Provider= Microsoft.ACE.OLEDB.12.0;Data Source=D:/Raj Collection For ASP Examples/Raj Practise Examples/Excel InsertData/Test4.xls; Extended Properties=\"Excel 12.0;HDR=YES;\"");
        conn.Open();
        string s = "Select * from [Sheet1$] where TitleId='" + TextBox1.Text + "'";
        OleDbCommand cmd = new OleDbCommand(s, conn);
        DataSet ds = new DataSet();
        OleDbDataAdapter da = new OleDbDataAdapter(cmd);

        da.Fill(ds);

        dt.Columns.Add(new System.Data.DataColumn("TitleId", typeof(String)));
        dt.Columns.Add(new System.Data.DataColumn("TitleName", typeof(String)));
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            dr = dt.NewRow();
            dr[0] = ds.Tables[0].Rows[i]["TitleId"].ToString();
            dr[1] = ds.Tables[0].Rows[i]["TitleName"].ToString();
            dt.Rows.Add(dr);
        }

        GridView1.DataSource = dt;
        GridView1.DataBind();
        conn.Close();

No comments:

Post a Comment