Click here to Skip to main content
15,909,242 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i m exporting gridview into excel but its not working i dnt knw why?
By using the code that is->
C#
try
        {
            Response.Buffer = true;
            Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Customers.xls"));
            Response.ContentType = "application/ms-excel";
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            gv_appt.AllowPaging = false;
            // gv_Employes.DataBind();
            //Change the Header Row back to white color
            gv_appt.HeaderRow.Style.Add("background-color", "#FFFFFF");
            //Applying stlye to gridview header cells
            for (int i = 0; i < gv_appt.HeaderRow.Cells.Count; i++)
            {
                gv_appt.HeaderRow.Cells[i].Style.Add("background-color", "#507CD1");
            }
            int j = 1;
            //This loop is used to apply stlye to cells based on particular row
            foreach (GridViewRow gvrow in gv_appt.Rows)
            {
                gvrow.BackColor = Color.White;
                if (j <= gv_appt.Rows.Count)
                {
                    if (j % 2 != 0)
                    {
                        for (int k = 0; k < gvrow.Cells.Count; k++)
                        {
                            gvrow.Cells[k].Style.Add("background-color", "#EFF3FB");
                        }
                    }
                }
                j++;
            }
            foreach (GridViewRow row in gv_appt.Rows)
            {
               
                LinkButton appgenrt = (LinkButton)row.FindControl("lbtn_apptGenerate");
                LinkButton email = (LinkButton)row.FindControl("lbtn_email");
                LinkButton edit = (LinkButton)row.FindControl("btn_editappt");
                LinkButton del = (LinkButton)row.FindControl(" btl_aapdlt");
                appgenrt.Visible = false;
                email.Visible = false;
                edit.Visible = false;
                del.Visible = false;
                gv_appt.Columns[7].Visible = false;
                gv_appt.Columns[8].Visible = false;
                gv_appt.Columns[9].Visible = false;
                gv_appt.Columns[10].Visible = false;
            }
            gv_appt.RenderControl(htw);
            Response.Write(sw.ToString());
            Response.End();
        }
        catch { }
Posted
Updated 16-Nov-11 18:31pm
v3

If you are getting error: "Control 'gv_appt' of type 'GridView' must be placed inside a form tag with runat=server.", then
you need to place the following method in your code behind:

C#
public override void VerifyRenderingInServerForm(Control control)
{
    //You may keep it empty. But needed.//
}


Click here[^] for detail.
 
Share this answer
 
Comments
Member 8387468 21-Nov-11 5:38am    
ok mam i have used this code already but used tab container tool of ajax in that container there are 4 tab panels & each tab panel has 1 gridview and 1 imagebutton on the click of that image button i want to change gridview into excel and this image button is used on each tab panel.so have done the coding to change gridview to excel.can you tell me should i have to make the this method for all image button means for all methods that are make individually for all gridview to excel...?
Member 8387468 21-Nov-11 5:42am    
sorry actly sir
C#
myDataGrid.Visible = true;
            Response.Clear();
            Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
            Response.Charset = "";
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType = "application/vnd.xls";

            System.IO.StringWriter stringWrite = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

            myDataGrid.RenderControl(htmlWrite);
            Response.Write(stringWrite.ToString());
            Response.End();
            myDataGrid.Visible = false;


Add export class library to your project..
Refer the following link:
C# class library for exporting data to CSV/Excel file[^]
 
Share this answer
 
v3
Comments
Member 8387468 17-Nov-11 0:23am    
can u tell me whats the prob in my code?
Neha Thanka 17-Nov-11 0:28am    
Did u add RKLib.ExportData.dll in your project?
Member 8387468 17-Nov-11 0:47am    
no mam
Neha Thanka 17-Nov-11 1:36am    
Please add it and try again....
All d best....
Member 10753099 8-May-14 2:45am    
THANKSSSSSSSSSS

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900