ASP.NET - How to export one complete asp.net page to excel

Asked By anbu n on 31-Oct-11 12:02 PM
how to export one complete asp.net page to excel

the page consist of textbox, dynamically created tables with labels in them
smr replied to anbu n on 31-Oct-11 12:52 PM
hi

In an export to Excel, write this code on btnexporttoExcel Click event.

string attachment = "attachment; filename=Export.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
// Create a form to contain the grid
HtmlForm frm = new HtmlForm();
gv.Parent.Controls.Add(frm);
frm.Attributes["runat"] = "server";
frm.Controls.Add(gv);
frm.RenderControl(htw);
 
//GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
 


After Click, one Save As Dialog Box opens.



refer
http://www.codeproject.com/KB/TipsnTricks/AllInOneExportData.aspx
Devil Scorpio replied to anbu n on 31-Oct-11 03:37 PM
Hi,

Here is the code to export asp.net page to excel

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Threading;
 
// <summary>
// Summary description for ReportBase
// </summary>
public class ReportBase : Page
{
    const string ExcelExport = "ExcelExport";
   
    public ReportBase()
    {
        this.Load += new EventHandler(ReportBase_Load);
    }
 
    void ReportBase_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Session.Add(ExcelExport, Request.QueryString[ExcelExport]);
        }
    }
 
    protected override void Render(HtmlTextWriter writer)
    {
        if (Session[ExcelExport] != null && bool.Parse(Session[ExcelExport].ToString()))
        {
 
            using (System.IO.StringWriter stringWrite = new System.IO.StringWriter())
            {
                using (HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite))
                {
                    base.Render(htmlWrite);
                    DownloadExcel(stringWrite.ToString());
                }
            }
        }
        else
        {
            base.Render(writer);
        }
    }
 
    public void DownloadExcel(string text)
    {
        try
        {
            HttpResponse response = Page.Response;
            response.Clear();
            response.AddHeader("cache-control", "must-revalidate");
            response.ContentType = "application/vnd.ms-excel";
            response.Write(text); 
            response.Flush();
            response.End();
        }
        catch (ThreadAbortException)
        {
            //If the download link is pressed we will get a thread abort.
        }
    }
}
Jitendra Faye replied to anbu n on 01-Nov-11 02:12 AM
Use this code-

First put your control in Panel, then use this code -

protected


void ButtonExport_Click(object sender, EventArgs e)

{

Response.Clear();

Response.Buffer =

true;

Response.AddHeader(

"content-disposition", "attachment;filename=FileName.xls");

Response.ContentEncoding = System.Text.

Encoding.UTF7;

Response.ContentType =

"application/vnd.ms-excel";

System.IO.

StringWriter oStringWriter = new System.IO.StringWriter();

System.Web.UI.

HtmlTextWriter htmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);


this.Panel1.RenderControl(htmlTextWriter);

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

Response.Flush();

Response.End();

}


public override void VerifyRenderingInServerForm(Control control)

{

}



Try this and let me know.