Click here to Skip to main content
11,928,786 members (52,843 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


10 bookmarked

Export any page to Excel automatically

, 24 Oct 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
Export any page to Excel automatically
This is your base page, you can now call your page with ExcelExport=True in the query string to make the page export to Excel.

Keep in mind that you will have a popup message when opening in .xls because it is not in the correct format. However, it will display nicely in Excel in most cases. Some fancy HTML Ajax will break it slightly and in those cases you could strip it out manually in the render function.

Here is a version with embedded css."
Note that if the popup is a problem, you can see my other Excel tip/trick. Clickety[^]

Here is how you can parse out controls you don't want to export to excel
Parse out controls from your html page.[^]

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))
    public void DownloadExcel(string text)
            HttpResponse response = Page.Response;
            response.AddHeader("cache-control", "must-revalidate");
            response.ContentType = "application/";
        catch (ThreadAbortException)
            //If the download link is pressed we will get a thread abort.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
Canada Canada
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralReason for my vote of 1 2626 Pin
khanyasir1-Nov-11 21:23
memberkhanyasir1-Nov-11 21:23 
Reason for my vote of 1
GeneralRe: 2626? Pin
rj452-Nov-11 7:34
memberrj452-Nov-11 7:34 
GeneralReason for my vote of 5 Thanks for this nice article. Pin
Member 432084425-Oct-11 11:33
memberMember 432084425-Oct-11 11:33 
GeneralYou forgot to mention the URL for your other Tip/Trick. I th... Pin
thatraja21-Oct-11 19:48
mvpthatraja21-Oct-11 19:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.151126.1 | Last Updated 24 Oct 2011
Article Copyright 2011 by rj45
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid