Solved

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

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Any business that wants to seriously grow needs to keep the needs and desires of an international audience of their websites in mind. Making a website friendly to international users isn’t prohibitively expensive and can provide an incredible return…
Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

831 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