error showing while export gridview to excel

Posted by Vibheesh under C# on 3/24/2012 | Points: 10 | Views : 1293 | Status : [Member] | Replies : 3
protected void btnExcel_Click(object sender, ImageClickEventArgs e)
{
Response.Clear();

Response.Buffer = true;

Response.AddHeader("content-disposition",

"attachment;filename=GridViewExport.doc");

Response.Charset = "";

Response.ContentType = "application/vnd.ms-word ";

StringWriter sw = new StringWriter();

HtmlTextWriter hw = new HtmlTextWriter(sw);

GridView1.AllowPaging = false;

GridView1.DataBind();

GridView1.RenderControl(hw);

Response.Output.Write(sw.ToString());

Response.Flush();

Response.End();
}
while click the button excel page showing file conversion problem




Responses

Posted by: Sksamantaray on: 3/24/2012 [Member] Silver | Points: 25

Up
0
Down
Hi ,
In your line-6:
try to put this Response.ContentType = "application/vnd.ms-excel";

Thanks,
Sanjay

Vibheesh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sksamantaray on: 3/26/2012 [Member] Silver | Points: 25

Up
0
Down
From your code it seems that you want to see the gridview output in Msword.
But you button Click event is not follwing naming convention.
Whats the exact error you are getting?
Is it something like put the Gridview control in form with runat server?


Thanks,
Sanjay

Vibheesh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sksamantaray on: 3/26/2012 [Member] Silver | Points: 25

Up
0
Down
Hi Vibheesh,
I have implemented your code to create word file and excel file.
Its working fine.
if you want to generate excel file ,then in line 4: make the extention to xls and line 6 write :Excel.

Them most importantly add the following code in your page:
    public override void VerifyRenderingInServerForm(Control control)

{
//
}


If it works for mark as answer, otherwise get back to me .

Thanks,
Sanjay

Vibheesh, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response