Solved

Save Output Result of an SQL Select to CSV from ASP Page

Posted on 2011-03-17
2
404 Views
Last Modified: 2012-05-11
I have a select which stores the results and returns distinct records to the page. I would like the option of adding a link to my page which when clicked will export the results to a CSV file. Is this possible?

My query is as follows:

set demousers=Conn.Execute ("SELECT DISTINCT firstname AS firstname, lastname AS lastname, email FROM uga_comment UNION ALL  SELECT DISTINCT fname AS firstname, lname AS lastname, email FROM cfp_demo ORDER BY lastname ASC")

Open in new window


Any help or guidance is appreciated.

Thank you.
0
Comment
Question by:Shawn Janes
2 Comments
 
LVL 15

Accepted Solution

by:
pateljitu earned 500 total points
ID: 35158615
Try this code.
'EXPORT TO CSV
<%
If (Request.Form("Action") = 1) Then
	
	set demousers=Conn.Execute ("SELECT DISTINCT firstname AS firstname, lastname AS lastname, email FROM uga_comment UNION ALL  SELECT DISTINCT fname AS firstname, lname AS lastname, email FROM cfp_demo ORDER BY lastname ASC")
	
	dim strCSVContent
	    		    	
	Response.Clear
	Response.ContentType = "application/vnd.ms-excel"
	Response.AddHeader "Content-Disposition","attachment; filename=" & "somefilename.csv"
		
	If not demousers.EOF then
            Do while not demousers.EOF
            
	        strCSVContent = "firstname,"& demousers("firstname ") & vbcrlf
	        strCSVContent = strCSVContent & "lastname, "& demousers("lastname") & vbcrlf
	        strCSVContent = strCSVContent & "email ,"& demousers("email ") & vbcrlf
	
	    demousers.MoveNext
            loop	
	end if
        
        demousers.Close()
        
	Response.Write(strCSVContent)
	Response.End

End if
%>

<form name="frmSearch" method="post">
	<input type="hidden" name="Action" value="" />
	<a href='javascript:document.forms[0].Action.value=1;document.forms[0].submit();' >Create CSV</a>
</form>

Open in new window

0
 

Author Closing Comment

by:Shawn Janes
ID: 35158678
PERFECT! Thank you!

Shawn.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now