Click here to Skip to main content
15,867,453 members
Articles / Web Development / ASP.NET
Article

One click ASP.NET export button control for Excel/CSV file

Rate me:
Please Sign up or sign in to vote.
4.57/5 (77 votes)
8 Dec 20042 min read 879.6K   29.3K   212   169
An extended ASP.NET button control for exporting/downloading Excel or CSV file.

Sample image

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, it is done by coding a separate page and adding various HTTP headers and responses. (Refer: Microsoft).

Like me, most of us believe in reuse. The export functionality can be achieved by reusing the 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, 'because this control doesn't require intermediate page for exporting data'. Just drag the ExportButton control on an ASPX page or a 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 headers, MIME types or encodings.

VB
'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 ExportButton properties can either be set at design time and/or run time].

Properties Explained

  • ExportType [CSV or EXCEL]
  • Separator [Comma or TAB]
  • Delimiter [Any character]

ExportType property is 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 is used to specify any delimiter like single quote, double quote, pipe character etc. Default is Nothing (empty string).

Demo code

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

  1. Install is straightforward. Extract demo code zip files to a designated directory.
  2. Two directories will be created. (namely, \\PNWebControls and \\TestMyControls). Make sure they both fall under same parent directory.
  3. Create a virtual directory by either right clicking on the directory \\TestMyControls and opening properties for web share, or use IIS MMC. (Note: 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).

How to use the Control in your project

You may either include the PNWebControls project and reference it in your project, or reference the assembly (PNayak.Web.UI.WebControls.ExportButton.dll). You can also add this control in Visual Studio Toolbox for design time.

[Note: This assembly also contains ExportLinkButton as some may prefer link button over regular button.]

Check out my .NET spot for more to discover.

History

  • 8 July 2004 - updated downloads.
  • 9 Dec 2004 - updated downloads and article.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
Cranking code more than 12 years. Technical/Project lead/MCSD. Offered services to various industuries like S/W, Telecom, Publishing, Insurance etc.

When not on computer, I play/swim/read with my kids and help my better half (of course my lovely wife) to clean house. Solving challenging S/W problems is my passion. Hate non-productive meetings. Do lots of GOOGLE and Eat/Drink/Sleep around MSDN.

Comments and Discussions

 
GeneralGREAT SOLUTION !!! Pin
mscotover16-Apr-07 6:20
mscotover16-Apr-07 6:20 
QuestionProblem with returns. Pin
betism15-Jan-07 12:47
betism15-Jan-07 12:47 
AnswerRe: Problem with returns. Pin
ketcapli28-Jun-07 12:28
ketcapli28-Jun-07 12:28 
GeneralRe: Problem with returns. Pin
Baiju Nagori18-Sep-07 7:09
Baiju Nagori18-Sep-07 7:09 
GeneralTo transfer data type text Pin
mardo11-Jan-07 5:14
mardo11-Jan-07 5:14 
GeneralRemove Headers Pin
clprogrammer3-Jan-07 6:07
clprogrammer3-Jan-07 6:07 
Questionpostback is not working... Pin
cebl11-Dec-06 8:56
cebl11-Dec-06 8:56 
QuestionASP.NET 2.0 Plans? Pin
mjhufford12-Oct-06 4:49
mjhufford12-Oct-06 4:49 
AnswerRe: ASP.NET 2.0 Plans? Pin
Lu5ck30-Oct-06 14:18
Lu5ck30-Oct-06 14:18 
GeneralRe: ASP.NET 2.0 Plans? Pin
KenHeer122-May-07 10:07
KenHeer122-May-07 10:07 
QuestionRe: ASP.NET 2.0 Plans? Pin
Member 26568207-Mar-08 18:17
Member 26568207-Mar-08 18:17 
Generalproblem with csv Pin
Yogesh Jamkhindikar31-Aug-06 0:27
Yogesh Jamkhindikar31-Aug-06 0:27 
GeneralOpen prompt Pin
nikos_katravas10-Aug-06 21:32
nikos_katravas10-Aug-06 21:32 
GeneralI Like it. Pin
amkarkha8-Aug-06 12:46
amkarkha8-Aug-06 12:46 
GeneralReg. PNayak.Web.UI.WebControls.ExportButton.dll registration Pin
lakshmijayaraman27-Jun-06 0:22
lakshmijayaraman27-Jun-06 0:22 
GeneralNullReferenceException Pin
xsynek25-Jun-06 20:20
xsynek25-Jun-06 20:20 
QuestionCSV parsing trouble Pin
uvtip15-Jun-06 9:43
uvtip15-Jun-06 9:43 
AnswerRe: CSV parsing trouble Pin
mjhufford12-Oct-06 4:02
mjhufford12-Oct-06 4:02 
GeneralRe: CSV parsing trouble Pin
uvtip16-Nov-06 6:57
uvtip16-Nov-06 6:57 
Questionis it possible to use/adapt this control for use within VWD Express? Pin
Productivity_Gary15-Feb-06 12:06
Productivity_Gary15-Feb-06 12:06 
GeneralTo strip HTML Pin
Sire4042-Jan-06 5:08
Sire4042-Jan-06 5:08 
Questioncan not export asia character Pin
Fico Zhu27-Dec-05 19:46
Fico Zhu27-Dec-05 19:46 
GeneralProblem with single quote Pin
R. Senthil Kumaran26-Oct-05 8:59
R. Senthil Kumaran26-Oct-05 8:59 
GeneralProblem with single quote Pin
Anonymous26-Oct-05 8:56
Anonymous26-Oct-05 8:56 
GeneralAlternative way Pin
Jan Gex23-Sep-05 6:22
Jan Gex23-Sep-05 6:22 

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.