Δ All posts

ASP Function RStoCSV - Converts a RecordSet to Excel

9 August 2007

Usage

call RStoCSV(recordSetObject,"MyFileName")

Note

Requires yStr

yourFunctionsFile.asp

function RStoCSV(rs, filename)
	set output = new yStr
	total = rs.Fields.Count
	count = 0
	for each x in rs.Fields
		output.App """" & x.name & """"
		if count < total then
			output.App ","
		end if
		count = count + 1
	next
	output.App vbCrLf
	While not rs.EOF
		set record = new yStr
		for each x in rs.fields
			if(isNull(x)) or x = "" then
				x = " "
			end if
			'x = Replace(x,",","\,")
			if record.output = "" then
				record.App """" & x
			else
				record.App """,""" & x
			end if
		next
		output.App record.output & """" & vbCrLf
		rs.movenext
	wend
	Response.Clear()
	Set userAgent=Server.CreateObject("MSWC.BrowserType")
	if userAgent.browser = "IE" then
		Response.AddHeader "Content-Disposition","attachment;filename=export.csv"
		Response.ContentType = "text/csv"
	else
		Response.AddHeader "Content-Disposition","attachment;filename=" & Replace(filename," ","_") &".csv"
		response.ContentType = "text/csv"
	end if
	response.write output.output
	response.End
end function

Can't stand it when groups follow me on twitter just to fish me into following them (@flcreatives). Spam is spam.

...twittered about 8 days ago