Export GridView to Excel

    Date: 05/16/06 (Asp Dot Net)    Keywords: html, asp, web

    Hey gang, I've got an odd Internet Explorer/MS Excel behavior that I need to figure out and fix.

    My xls.aspx.cs:

        protected void Page_Load(object sender, EventArgs e)
        {
            GridView grdExcel = (GridView)grdReport;
            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment;filename=rpt.xls");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType = "application/vnd.ms-excel";
            Response.Charset = "";
            this.EnableViewState = false;

            System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);

            Response.Write("");
            //this.ClearControls(grdExcel);
            grdExcel.RenderControl(oHtmlTextWriter);

            Response.Write(oStringWriter.ToString());

            Response.End();
        }

        public override void
            VerifyRenderingInServerForm(Control control)
        {
            return;
        }

    Common code example from all over the net.

    What happens: When clicking on my link to download the excel doc, I am finding that "Open" in IE does not work.  I can save the document to my local drive and then open, but when I try to click the link and select "Open" it tells me:

    "'C:\Documents and Settings\user\local settings\temp net files\content.IE5\0L6RCHUN\rpt[n].xls' could not be found. Check the spelling of the file name, and verify that the file location is correct.

    If you are trying to open the file from your list of most recently used files on the File menu, make sure the file has not been renamed, moved, or deleted.
    " (note: file path location isn't exact. just giving general idea of temp location)

    But, if I click ok on that excel window error, and then return to the IE window and click on the link once more (now MS Excel is open).... My excel document opens properly.

    I'm using:
    MS Excel 2003 (11.5612.5606)
    IE 6.0.2900.2180.xpsp_sp2_gdr.050301-1519
    Windows XP Pro ver. 2002 SP 2
    .NET 2.0/C#

    (FYI: Works beautiful in Firefox.)

    Anyone experience anything similar?


    UPDATE: If I set Caching to private, it works. (Response.Cache.SetCacheability(HttpCacheability.Private);)

    Source: http://community.livejournal.com/aspdotnet/66369.html

« Stupid DataGrid/DataView... || Format a string »


antivirus | apache | asp | blogging | browser | bugtracking | cms | crm | css | database | ebay | ecommerce | google | hosting | html | java | jsp | linux | microsoft | mysql | offshore | offshoring | oscommerce | php | postgresql | programming | rss | security | seo | shopping | software | spam | spyware | sql | technology | templates | tracker | virus | web | xml | yahoo | home