Solved

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

Posted on 2011-03-17
2
412 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn how to count occurrences of each item in an array.

630 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