views:

73

answers:

1

I am trying to export to repeter/Gridview in excel, this is working fine in my local machine and windows server 2003, however when I deployed on Windows server 2008 its not working properly. here is my code

    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    string attachment = "attachment; filename=myReport.xls";
    Response.ClearContent();
    Response.AddHeader("content-disposition", attachment);
    Response.ContentType = "application/vnd.ms-excel";
    rpt.RenderControl(htw);
    Response.Write(sw.ToString());
    Response.Flush();
    Response.End();

Server Details : Windows Server 2008 and IIS7

In mozilla all the page contents are exported to excel with an error but in IE & Chrome, empty excel file is exported without data.

+1  A: 

You are writing a string to the response. I would expect this file type to be binary and therefore you should be using Response.BinaryWrite(). What is actually coming from rpt.RenderControl(htw)? I suspect, that you should change your content type to text/csv, which excel will still handle.

matt-dot-net
this is not working
Muhammad Akhtar
what is not working? Have you tried sw.Flush()?
matt-dot-net
you mean I have to add sw.Flush() instead of Response.Flush()?
Muhammad Akhtar
Yes, try sw.Flush before you write it to the response
matt-dot-net
no luck, Actually nothing written in my file, whatever the file format like word, excel or text, can you plz try this on windows server 2008 at your end and test it weather it is working or not?
Muhammad Akhtar