Solved

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

Posted on 2011-03-17
2
405 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
An enjoyable and seamless user experience can go a long way on an eCommerce site. While a cohesive layout and engaging copy play roles in creating a positive user experience, some sites neglect aspects that seem marginal but in actuality prove very …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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.

914 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

12 Experts available now in Live!

Get 1:1 Help Now