JAYRAM

JAYRAM

  • NA
  • 272
  • 214.4k

I am trying to export gridview contents into Excel as given

Feb 4 2013 2:14 AM
I am trying to export gridview contents into Excel as given below, Excel is created but there is no content. but I am able to view the content in the webpage.

Response.Clear();
        Response.Buffer = true;
        string filename = "GridViewExport_" + DateTime.Now.ToString() + ".xls";
        Response.AddHeader("content-disposition",
        "attachment;filename=" + filename);
        Response.Charset = "";
        Response.ContentType = "application/vnd.ms-excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        gv.AllowPaging = false;
        gv.DataBind();
        gv.RenderControl(hw);
        //style to format numbers to string
        string style = @"<style> .textmode { mso-number-format:\@; } </style>";
        Response.Write(style);
        Response.Output.Write(sw.ToString());
        Response.Flush();
        Response.End();
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        //verifies that the control is rendered
    }

Answers (3)