ASP.NET Exporting To Excel & Word

Today, I was working on my karaoke web application.

http://blog.pcsitter.info/2009/06/26/designing--creating-a-karaoke-music-library.aspx

In this application, I added the ability to print all the songs from the Music Library. This is the song list I use for karaoke events. I have been able to print the web page; however, I wanted the ability to also export the Music Library song list to Excel and Word.

So amazing how easy this functionality was to add to my application. It took me less than 30 minutes to implement it with no hurdles.

Although the app isn't completely ready, the list list is printable.

http://karaoke.pcsitter.info/PrintSongList.aspx

I created a separate class library, built the source code, and placed my dll into my Visual Studio bin folder and referenced it.

Feel free to use the code below.

Enjoy,
Vivian

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Exporter
{
    /// <summary>
    /// This Class Exports Data From An HTML Data Control (ex. DataGrid, GridView).
    /// </summary>
    public class Exporter
    {
        public enum EXPORT_TYPE_APP
        {
            EXCEL,
            WORD,
            TEXT
        }

        /// <summary>
        /// /// Exports Data From A User Control.
        /// Make sure to clear control from all html controls.
        /// </summary>
        /// <param name="Response"></param>
        /// <param name="UC"></param>
        /// <param name="Control"></param>
        /// <param name="export_to_app"></param>
        public Exporter(HttpResponse Response, UserControl UC, Control Control, EXPORT_TYPE_APP export_to_app)
        {
            try
            {
                switch (export_to_app)
                {
                    case EXPORT_TYPE_APP.EXCEL:
                        Response.Clear();
                        Response.Buffer = true;
                        Response.ContentType = "application/vnd.ms-excel";
                        Response.Charset = "";
                        UC.EnableViewState = false;
                        System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
                        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
                        ClearControls(Control);
                        Control.RenderControl(oHtmlTextWriter);
                        Response.Write(oStringWriter.ToString());
                        Response.End();
                        break;

                    case EXPORT_TYPE_APP.WORD:
                        Response.Clear();
                        Response.AddHeader("content-disposition", "attachment;filename=ExportedToWord.doc");
                        Response.Charset = "";
                        Response.Cache.SetCacheability(HttpCacheability.NoCache);
                        Response.ContentType = "application/vnd.word";
                        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
                        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
                        Control.RenderControl(htmlWrite);
                        Response.Write(stringWrite.ToString());
                        Response.End();
                        break;
                }
              
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Clears Control. If Error Occurs:
        /// www.c-sharpcorner.com/UploadFile/DipalChoksi/ExportASPNetDataGridToExcel11222005041447AM/ExportASPNetDataGridToExcel.aspx       
        /// </summary>
        /// <param name="control"></param>
        private void ClearControls(Control control)
        {
            for (int i = control.Controls.Count - 1; i >= 0; i--)
            {
                ClearControls(control.Controls[i]);
            }
            if (!(control is TableCell))
            {
                if (control.GetType().GetProperty("SelectedItem") != null)
                {
                    LiteralControl literal = new LiteralControl();
                    control.Parent.Controls.Add(literal);
                    try
                    {
                        literal.Text = (string)control.GetType().GetProperty("SelectedItem").GetValue(control, null);
                    }
                    catch
                    {
                    }
                    control.Parent.Controls.Remove(control);
                }
                else
                    if (control.GetType().GetProperty("Text") != null)
                    {
                        LiteralControl literal = new LiteralControl();
                        control.Parent.Controls.Add(literal);
                        literal.Text = (string)control.GetType().GetProperty("Text").GetValue(control, null);
                        control.Parent.Controls.Remove(control);
                    }
            }
            return;
        }

    }//End of class
} //End of namespace








 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name (required)

 Email (will not be published) (required)

Your comment is 0 characters limited to 3000 characters.