4
Answers

Save the contents of gridview to the excel file?

Photo of AMOL

AMOL

12y
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)

1
Photo of Manish Dwivedi
NA 8.3k 1.2m 14y

Hi will,
for str, single character is converted to its relevant ASCII value.
Same is happen for str2 , taking ASCII value for characters(Here fix string is AMEND, you have to make it large like AMENDING)
and str3 we are taking the difference of ASCII value.

and making the format like that 1245/2543/475812
and in last line the value you got is converted to datetime.
This is the working og this code, but still i didn't get the purpose of code.
 
 
 


 
Anybody wish to share with me how this code actually works?
 
Accepted
0
Photo of Will
NA 3 1.8k 14y

Thank You very much for your answer.
I had worked out that it was subtracting from the ASCII value of each character in the str2 "string" moments before you post, and have since made the code work how I required :)
Regards :)