5,693,062 members and growing! (18,751 online)
Email Password   helpLost your password?
Web Development » Custom Controls » General     Intermediate

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

By Prashant Nayak (.Net Lover)

An extended ASP.NET button control for exporting/downloading Excel or CSV file.
VB.NET 1.0, .NET 1.1, Win2K, WinXP, Win2003, Windows, .NET, ASP.NET, WebForms, Visual Studio, VS.NET2002, VS.NET2003, Dev

Posted: 17 May 2004
Updated: 8 Dec 2004
Views: 361,657
Bookmarked: 167 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
66 votes for this Article.
Popularity: 7.63 Rating: 4.19 out of 5
7 votes, 10.8%
1
1 vote, 1.5%
2
3 votes, 4.6%
3
12 votes, 18.5%
4
42 votes, 64.6%
5

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.

'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

About the Author

Prashant Nayak (.Net Lover)


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.

Occupation: Web Developer
Location: United States United States

Other popular Custom Controls articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 131 (Total in Forum: 131) (Refresh)FirstPrevNext
GeneralDataview Export to excel - Messed up excel sheetmembererkrsr19:35 30 Nov '08  
GeneralUsing with SSLmemberAndrew Woomer10:56 13 Nov '08  
GeneralIf using this with Ajax update panel - you better do this...memberilovthecov14:22 27 Oct '08  
GeneralHow do you format the Excel output?member5150.Net8:31 14 Oct '08  
GeneralCan I use this control in Datagrid ?memberthakar1:45 10 Sep '08  
GeneralFormatmemberMember 17201838:49 13 Mar '08  
GeneralRe: FormatmemberAlex Tang20:18 21 Apr '08  
GeneralRe: FormatmemberSandeep.Ban22:45 27 May '08  
GeneralRe: Formatmember5150.Net8:19 14 Oct '08  
GeneralProblemmemberMember 172018310:52 12 Mar '08  
Generalexcel report in asp.netmembersehgalama20:40 30 Jan '08  
GeneralRe: excel report in asp.netmembermauxanhlam22:51 12 Jun '08  
NewsAlternative Solution [modified]memberkelvin199719:30 18 Nov '07  
AnswerRe: Alternative Solutionmemberkelvin199719:35 18 Nov '07  
AnswerAbout Excel and my GenerateFile() functionmemberkelvin199720:17 18 Nov '07  
GeneralRe: Alternative SolutionsupporterTom Welch15:30 11 Feb '08  
Generalunable to run start debuggin on web servermemberafreen taj3:38 6 Nov '07  
GeneralError when trying to runmembertomkovic16:25 26 Oct '07  
GeneralSo simple to usememberMaulik Dave11:36 9 Aug '07  
General.Net 2.0 Version in C#membersgt13echo5:57 3 Jul '07  
GeneralRe: .Net 2.0 Version in C#memberprivacy space23:40 23 Aug '07  
GeneralRe: .Net 2.0 Version in C#memberdennisbetten9:43 9 Mar '08  
GeneralGreat button for "Simple Exports"memberketcapli13:31 28 Jun '07  
GeneralC# versionmemberjerzyk_10:39 18 Jun '07  
GeneralGREAT SOLUTION !!!membermscotover7:20 16 Apr '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 8 Dec 2004
Editor: Smitha Vijayan
Copyright 2004 by Prashant Nayak (.Net Lover)
Everything else Copyright © CodeProject, 1999-2008
Web10 | Advertise on the Code Project