User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the ASP.NET section within the Web Development category of DaniWeb, a massive community of 426,125 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,767 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our ASP.NET advertiser: Lunarpages ASP Web Hosting
Views: 6361 | Replies: 6
Reply
Join Date: Mar 2006
Posts: 18
Reputation: mikefitz is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
mikefitz mikefitz is offline Offline
Newbie Poster

Export to Excel from ASP.NET app

  #1  
Jan 10th, 2007
Hi,
I need to export data from a DataSet Element to Excel. Does anyone know the easiest way to export the data from the DataSet to an Excel file on the client's machine?

Thanks,
Mike
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jan 2007
Location: Japan
Posts: 70
Reputation: sedgey is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 4
sedgey sedgey is offline Offline
Junior Poster in Training

Re: Export to Excel from ASP.NET app

  #2  
Jan 10th, 2007
The simplest way is to export the file as a csv (comma seperated values) with a .xls extension which will open in excel although it's technically not an excel document, the alternative is to use the office interop for excel, only trouble with that is that office will need to be installed on the web server plus license. There are some components which do this kind of export e.g. aspose excel but obviously you would need to pay for it. Obviously in both cases the client would be prompted to save the file to their disk as the documents will be streamed from the webserver, having the application automatically save the document on the clients machine would be a lot more complex.

Heres a good article about the csv method:
http://www.dotnetspider.com/kb/Article963.aspx
sedgey: so little daylight, too much caffeine
MCSD MCAD MCSE
http://www.thefriendlybuilder.co.uk,
http://www.sheetmusicdirect.com
Reply With Quote  
Join Date: Mar 2006
Posts: 18
Reputation: mikefitz is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
mikefitz mikefitz is offline Offline
Newbie Poster

Re: Export to Excel from ASP.NET app

  #3  
Jan 11th, 2007
Does anyone have a code example using VB.NET?
Reply With Quote  
Join Date: Jan 2007
Location: Japan
Posts: 70
Reputation: sedgey is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 4
sedgey sedgey is offline Offline
Junior Poster in Training

Re: Export to Excel from ASP.NET app

  #4  
Jan 11th, 2007
This does code translation from C# to VB and vice versa:

http://www.carlosag.net/Tools/CodeTr...r/Default.aspx
sedgey: so little daylight, too much caffeine
MCSD MCAD MCSE
http://www.thefriendlybuilder.co.uk,
http://www.sheetmusicdirect.com
Reply With Quote  
Join Date: Jan 2007
Posts: 1
Reputation: Shoobs is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Shoobs Shoobs is offline Offline
Newbie Poster

Re: Export to Excel from ASP.NET app

  #5  
Jan 24th, 2007
Originally Posted by mikefitz View Post
Does anyone have a code example using VB.NET?
I have found a really good example from microsoft at "http://support.microsoft.com/kb/317719". The author has added more than you need... I have used a datagrid not gridview on a new web form (copy their Bottom.aspx form only). I changed the code so you can send the page some SQL which will then be used to query the database and bind with the datagrid.
If you want to give the Excel form a name use this line: [Response.AddHeader("Content-Disposition", "attachment; filename=Exported.xls""")].
Then simply put a Response.Redirect at the end of the code to return you to the page you requested the function from. It appears instantaneously and you dont look like you navigate away from the page at all.

Hope this helps!
Reply With Quote  
Join Date: Jan 2007
Location: Japan
Posts: 70
Reputation: sedgey is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 4
sedgey sedgey is offline Offline
Junior Poster in Training

Re: Export to Excel from ASP.NET app

  #6  
Feb 9th, 2007
sedgey: so little daylight, too much caffeine
MCSD MCAD MCSE
http://www.thefriendlybuilder.co.uk,
http://www.sheetmusicdirect.com
Reply With Quote  
Join Date: Jan 2007
Posts: 47
Reputation: kapil.goyal is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
kapil.goyal kapil.goyal is offline Offline
Light Poster

Re: Export to Excel from ASP.NET app

  #7  
Feb 15th, 2007
i m providing a code made necessary change at ur end.


Response.Clear()
Response.AddHeader("content-disposition", "attachment;filename=MyExcel.xls")
Response.Charset = ""
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.ContentType = "application/vnd.xls"
Dim sw As StringWriter = New StringWriter
Dim htw As HtmlTextWriter = New HtmlTextWriter(sw)
dgdfiles.RenderControl(htw)
Response.Write(sw.ToString())
Response.End()



dgdfiles is the datagrid control id.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb ASP.NET Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the ASP.NET Forum

All times are GMT -4. The time now is 4:23 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC