4
Answers

Save the contents of gridview to the excel file?

AMOL

AMOL

9y
3k
1
I want to save the contents of gridview to the excel file and i am using the following code to done this. It works fine but the excel file it make is of the 0 KB size. please help me to resolve this issue. Thanks in advance... 


having the following code with me but it sttil  irritate me...
please suggest me other method.....


 private void ExportToExcel()
        {
            HtmlForm form = new HtmlForm();


            string attachment = "attachment; filename=AssignedDoctors.xls";


            Response.ClearContent();


            Response.AddHeader("content-disposition", attachment);


            Response.ContentType = "application/ms-excel";


            StringWriter stw = new StringWriter();


            HtmlTextWriter htextw = new HtmlTextWriter(stw);
           
            // put your grid view in here
            form.Controls.Add(GridView1);


            this.Controls.Add(form);


            form.RenderControl(htextw);


            Response.Write(stw.ToString());


            Response.End();
        }


Answers (4)