One click export button for Excel/CSV file

Sample Image - maximum width is 600 pixels

Introduction

Microsoft has done a wonderful job of creating an extensible framework especially for ASP.NET by creating various server controls. This article is about extending ASP.NET button control and adding your own functionality for ONE CLICK exporting from ASP.NET page.

Background

There are various ways one can export data using ASP.NET. Usually done by coding a separate page and adding various HTTP headers and responses. (Refer: http://support.microsoft.com/default.aspx?scid=kb;EN-US;317719).

Like me, most of us believe in reuse. The export functionality can be achieved by reusing same page for various projects either by passing DATASET from the parent page or reconnecting to the data source on the landing page and manipulating response object.

One Click Export

You may wonder why I call it ONE CLICK EXPORT, 'cause this control doesn't require intermediate page for exporting the data. Just drag the export button control on ASPX page or USER control and set its properties at design time or runtime to hook dataview. Yup, That’s it, no more hassles of dealing to investigate HTTP hearders, MIME types or encodings.


‘ASPX page [Design time]
…
…
<pnwc:ExportButton id="btnExcel" runat="server" Separator="TAB" Text="Export to Excel" FileNameToExport="Books.xls" BackColor="#E0E0E0"></pnwc:ExportButton>
….
….

‘Code Behind [Run time]
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim ds as dataset=filldataset()
        dgBooks.DataSource = ds.Tables("Books")
        dgBooks.DataBind()
			
        'Set Export button properties 
        btnExcel.Dataview = ds.Tables("Books").DefaultView
        btnExcel.FileNameToExport = "Books.xls"
        btnExcel.ExportType = PNayak.Web.UI.WebControls.ExportButton.ExportTypeEnum.Excel
        btnExcel.Separator = PNayak.Web.UI.WebControls.ExportButton.SeparatorTypeEnum.TAB

    End Sub

[Note: The Export button properties can either be set at Design time and/or Run time].

Properties Explained

ExportType property used to either select CSV or EXCEL format btnExcel.ExportType = PNayak.Web.UI.WebControls.ExportButton.ExportTypeEnum.Excel
Separator property can be used to either select TAB or Comma separator btnExcel.Separator = PNayak.Web.UI.WebControls.ExportButton.SeparatorTypeEnum.TAB
Delimiter property used to specify any delimiter like single quote, double quote, pipe character etc. Default is NOTHING (empty string).

Source code

The source code is provided with the sample project to test out the solution.

  1. Install is straightforward. Extract zip files to a designated directory.
  2. Two directories will be created. (namely \\PNWebControls & \\TestMyControls). Make sure they both fall under same parent directory.
  3. Create a web share for “TestMyControls” directory. This is a DEMO project.
  4. Open up a demo solution (TestPNControls.sln) in Visual Studio 2003. (This control also works well with VS 2002. You know the drill, just change the version numbers inside the project/solution file and open it in Visual Studio 2002).

Check out my .NET spot at http://prashantnayak.freeservers.com for more to discover.