Solved

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

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
CTAs encourage people to do something specific to show interest in your company, product or service. Keep reading to learn why CTAs should always be thought of as extremely important, albeit small, sections of websites.
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…
This video teaches users how to migrate an existing Wordpress website to a new domain.

734 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