Thursday, 6 March 2014

Export the whole html page in excel

Response.Clear();
        Response.Buffer = true;

        Response.AddHeader("content-disposition", "attachment; filename=FacebookAccountList.xls");

        Response.ContentType = "application/excel";
        StringWriter sWriter = new StringWriter();
        HtmlTextWriter hTextWriter = new HtmlTextWriter(sWriter);
       
        Response.ContentEncoding = Encoding.UTF8;
        Response.HeaderEncoding = Encoding.UTF8;
        Response.Charset = Encoding.UTF8.WebName;
        EnableViewState = false;
        /*Put the control which you want to render in below line*/
        test.RenderControl(hTextWriter);
        /**/
        string output = sWriter.ToString();
        output = "<html><head><meta charset=" + Encoding.UTF8.WebName + " /></head><body>" + output + "</body></html>";
        Response.Write(output);

        Response.Flush();
        Response.End();