Click here to Skip to main content
11,636,985 members (69,250 online)
Click here to Skip to main content

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

, 8 Dec 2004 687.9K 28.1K 213
Rate this:
Please Sign up or sign in to vote.
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.

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

[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

Share

About the Author

Prashant Nayak (.Net Lover)
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.

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 5 Pin
m3jim11-Jan-13 1:48
memberm3jim11-Jan-13 1:48 
Answerunicode problem Pin
kamran_kamaei29-Nov-11 14:53
memberkamran_kamaei29-Nov-11 14:53 
Questionwhen I click on export button broser shows me msg "Internet Explorer cannot display the webpage " ..plz help urgent ! Pin
anjaliaroraverma26-Sep-11 18:41
memberanjaliaroraverma26-Sep-11 18:41 
AnswerRe: when I click on export button broser shows me msg "Internet Explorer cannot display the webpage " ..plz help urgent ! Pin
anjaliaroraverma26-Sep-11 19:00
memberanjaliaroraverma26-Sep-11 19:00 
GeneralRe: when I click on export button broser shows me msg "Internet Explorer cannot display the webpage " ..plz help urgent ! Pin
Member 97423229-Jan-13 2:48
memberMember 97423229-Jan-13 2:48 
GeneralRe: when I click on export button broser shows me msg "Internet Explorer cannot display the webpage " ..plz help urgent ! Pin
thedarkone210-May-13 0:26
memberthedarkone210-May-13 0:26 
GeneralRe: when I click on export button broser shows me msg "Internet Explorer cannot display the webpage " ..plz help urgent ! Pin
Praveen Kandari12-Mar-15 22:52
memberPraveen Kandari12-Mar-15 22:52 
GeneralHow to use the code in c# Pin
sridharan282-Jun-11 19:29
membersridharan282-Jun-11 19:29 
GeneralMy vote of 1 Pin
Eng Basel27-Apr-11 2:32
memberEng Basel27-Apr-11 2:32 
GeneralMy vote of 1 Pin
gandhiabc4u13-Apr-11 23:54
membergandhiabc4u13-Apr-11 23:54 
GeneralNo data found when using export button Pin
cfchapman4-Aug-10 7:56
membercfchapman4-Aug-10 7:56 
GeneralRe: No data found when using export button Pin
Praveen Kandari12-Mar-15 22:59
memberPraveen Kandari12-Mar-15 22:59 
GeneralError Display unicode letter In Excel Pin
DongMT2-Aug-10 23:44
memberDongMT2-Aug-10 23:44 
QuestionFormatting a Integer column to String Pin
SibiSurya10-May-10 3:29
memberSibiSurya10-May-10 3:29 
QuestionTotal Noob begging for someones help Pin
Phil Lavender12-Dec-09 1:37
memberPhil Lavender12-Dec-09 1:37 
Questionresponese.end error message Pin
Member 404560329-Aug-09 2:42
memberMember 404560329-Aug-09 2:42 
AnswerRe: responese.end error message Pin
Loki7021-Oct-09 5:12
memberLoki7021-Oct-09 5:12 
AnswerRe: responese.end error message Pin
V.Manjunath11-Jan-11 8:57
memberV.Manjunath11-Jan-11 8:57 
GeneralExport to excel not CSV Pin
torchwood90915-Jun-09 6:00
membertorchwood90915-Jun-09 6:00 
GeneralRe: Export to excel not CSV Pin
gg423721-Aug-09 5:30
membergg423721-Aug-09 5:30 
GeneralRe: Export to excel not CSV Pin
CikaPero15-Sep-10 22:18
memberCikaPero15-Sep-10 22:18 
GeneralExport to Excel using this library IN A USER CONTROL Pin
torchwood90915-Jun-09 5:54
membertorchwood90915-Jun-09 5:54 
Generalfilldataset Pin
torchwood90911-Jun-09 9:51
membertorchwood90911-Jun-09 9:51 
AnswerRe: filldataset Pin
musacj11-Jun-09 22:35
membermusacj11-Jun-09 22:35 
GeneralRe: filldataset Pin
torchwood90915-Jun-09 4:36
membertorchwood90915-Jun-09 4:36 
AnswerRe: filldataset Pin
musacj15-Jun-09 5:00
membermusacj15-Jun-09 5:00 
GeneralRe: filldataset Pin
torchwood90916-Jun-09 7:03
membertorchwood90916-Jun-09 7:03 
AnswerRe: filldataset Pin
musacj17-Jun-09 0:23
membermusacj17-Jun-09 0:23 
QuestionCan someone help me to do this from a user control from a datasource? Pin
torchwood90911-Jun-09 9:42
membertorchwood90911-Jun-09 9:42 
AnswerRe: Can someone help me to do this from a user control from a datasource? Pin
psit5716-Jul-09 8:47
memberpsit5716-Jul-09 8:47 
Generalfile transfer to mobile phone Pin
sakshi singla15-Mar-09 2:48
membersakshi singla15-Mar-09 2:48 
GeneralDataview Export to excel - Messed up excel sheet Pin
erkrsr30-Nov-08 18:35
membererkrsr30-Nov-08 18:35 
QuestionRe: Dataview Export to excel - Messed up excel sheet Pin
torchwood90911-Jun-09 9:40
membertorchwood90911-Jun-09 9:40 
GeneralRe: Dataview Export to excel - Messed up excel sheet Pin
gg42375-Nov-09 0:41
membergg42375-Nov-09 0:41 
GeneralUsing with SSL Pin
Andrew Woomer13-Nov-08 9:56
memberAndrew Woomer13-Nov-08 9:56 
GeneralIf using this with Ajax update panel - you better do this... Pin
ilovthecov27-Oct-08 13:22
memberilovthecov27-Oct-08 13:22 
GeneralRe: If using this with Ajax update panel - you better do this... Pin
Member 398938613-Mar-09 0:06
memberMember 398938613-Mar-09 0:06 
QuestionHow do you format the Excel output? Pin
5150.Net14-Oct-08 7:31
member5150.Net14-Oct-08 7:31 
QuestionCan I use this control in Datagrid ? Pin
thakar10-Sep-08 0:45
memberthakar10-Sep-08 0:45 
GeneralFormat Pin
Member 172018313-Mar-08 7:49
memberMember 172018313-Mar-08 7:49 
GeneralRe: Format Pin
Alex Tang21-Apr-08 19:18
memberAlex Tang21-Apr-08 19:18 
GeneralRe: Format Pin
Sandeep.Ban27-May-08 21:45
memberSandeep.Ban27-May-08 21:45 
GeneralRe: Format Pin
5150.Net14-Oct-08 7:19
member5150.Net14-Oct-08 7:19 
GeneralRe: Format Pin
jesiahwedgewood27-Apr-09 12:56
memberjesiahwedgewood27-Apr-09 12:56 
GeneralRe: Format Pin
gtxboyracer8-Feb-10 10:34
membergtxboyracer8-Feb-10 10:34 
GeneralProblem Pin
Member 172018312-Mar-08 9:52
memberMember 172018312-Mar-08 9:52 
GeneralRe: Problem [modified] Pin
psit5715-Jul-09 10:14
memberpsit5715-Jul-09 10:14 
Generalexcel report in asp.net Pin
sehgalama30-Jan-08 19:40
membersehgalama30-Jan-08 19:40 
GeneralRe: excel report in asp.net Pin
mauxanhlam12-Jun-08 21:51
membermauxanhlam12-Jun-08 21:51 
NewsAlternative Solution [modified] Pin
kelvin199718-Nov-07 18:30
memberkelvin199718-Nov-07 18:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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
Web03 | 2.8.150728.1 | Last Updated 9 Dec 2004
Article Copyright 2004 by Prashant Nayak (.Net Lover)
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid