I would recomment using the .toString method:
strReturn= objRS.GetString (StringFormat, NumRows, ColumnDelimiter, RowDelimiter, NullExpr)
For details see http://www.devguru.com/Tec
FtB
Main Topics
Browse All TopicsI have a query that retrieves records from a db, which i dispaly in a table. However I would also like to create a csv file and also allow the user to save it. How do i go about doing this? As i understand a csv file is just a file with values seperated by commas.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
I would recomment using the .toString method:
strReturn= objRS.GetString (StringFormat, NumRows, ColumnDelimiter, RowDelimiter, NullExpr)
For details see http://www.devguru.com/Tec
FtB
here is some general code to create a simple csv file from a recordset of data, using the file system object to create the csv file on your webserver
dim oFSO, oFile,oField
dim strOutput
'assuming you have already connected to your database and you have a connection object called oConn and you have already pulled a recordset of your data called oRS
set oFSO = server.createobject ("scripting.filesystemobje
set oFile = oFSO.createtextfile("myfil
do until oRS.eof
for each oField in oRS.fields
stroutput = stroutput & ofield.value & ","
next
'remove the last comma from the line before continuing
stroutput = left(stroutput,len(stroutp
ofile.writeline stroutput
ors.movenext
loop
ofile.close
set oFile = nothing
set oFSO = nothing
'clean up your recordset and connection objects
You will want to make sure you have write permissions to the folder on the webserver where you are going to create this file.
hope this helps.
jk
Here's how i usually create CSV file and some simple sample codes.
<%
'sOutput stores the final output
'sData stores each line output
'==== write the title (name of the column) ===
sData = Chr(34) & "First Name" & Chr(34) & ","
sData = sData & Chr(34) & "Last Name" & Chr(34)
sOutPut = sOutPut & sData & vbCrLf
'===== now output 1 line of data =======
sData = Chr(34) & "John" & Chr(34) & ","
sData = sData & Chr(34) & "Doe" & Chr(34)
sOutPut = sOutPut & sData & vbCrLf
FileName="myTestFile.csv" 'default file name
Response.Clear
Response.ContentType = "text/csv"
Response.AddHeader "Content-Disposition", "filename=" & FileName & ";"
Response.Write(sOutPut)
%>
in your actual code, you would replace the "output 1 line of data" part with a recordset loop to output all your records (which means you would have to run the same query).
In your main page where you display the records in html, you can add a submit button for export to CSV. When you submit the form to create CSV, it should open a dialogue for "save as", and then you can save the csv file in your local machine.
have not tried FtB's way before. I might try it the next time i work on csv export
kingsfan
here's a function that will create a csv file.
You just need to pass it a sql query
sub outputcsv(sql)
'response.write sql
randomize
nRandom = (100-1)*Rnd+1
fileExcel =monthname(month(date)) & cstr(year(date)) & "_" & CStr(nRandom) & ".csv"
filePath= Server.mapPath("admin.asp"
filepath=mid(filepath,1,le
'response.write "filepath:" & filepath & "<br>"
filename=filePath & "excelfiles\" & fileExcel
'response.write filename
Set fs = Server.CreateObject("Scrip
Set MyFile = fs.CreateTextFile(filename
Set rs = objconn.Execute(sql)
strLine="<table><tr>" 'Initialize the variable for storing the filednames
For each x in rs.fields
strLine= strLine & "<td>" & x.name & "</td>"
Next
strline=strline&"</tr>"
MyFile.writeline strLine
Do while Not rs.EOF
strLine="<tr>"
for each x in rs.Fields
cell=x.value
strLine= strLine & "<td>" & cell & "</td>"
next
strline=strline & "</tr>"
MyFile.writeline strLine
rs.MoveNext
Loop
myfile.writeline "</table>"
MyFile.Close
Set MyFile=Nothing
Set fs=Nothing
link="<A HREF=excelfiles/" & fileExcel & " class=adminmenu target=_new>Click Here to Open Excel</a>"
Response.write "<br>Excel File Created<br><br>" & link
end sub
I tried all the solutions and could only get the one by jkwasson to work on my application. even with this I have encountered a problem I cannot seem to fix. The code runs then opens the .xls file but I find that each successive row seems to concatenate on the row before (eg) excel row 1 =table row 1, Excel row 2 = table row 1 + row 2. This continues until the final Excel row contains all the table rows consecutively. I cannot seem to fix the code to return only one Excel row per table row....
My code:
<%
Session("FileName")
FileName= "thisfile001.csv"
%>
<%
set oRS = Server.CreateObject("ADODB
oRS.ActiveConnection = MM_Honda_STRING
oRS.Source = "SELECT DISTINCT * FROM dbo.countries"
oRS.CursorType = 0
oRS.CursorLocation = 2
oRS.LockType = 3
oRS.Open()
oRS_numRows = 0
%>
<%
dim oFSO, oFile,oField
dim strOutput
set oFSO = server.createobject ("scripting.filesystemobje
set oFile = oFSO.createtextfile("D:\we
do while not oRS.eof
for each oField in oRS.fields
strOutput = strOutput & oField.value & ","
next
'remove the last comma from the line before continuing
strOutput = left(strOutput,len(strOutp
oFile.writeline strOutput
oRS.movenext
Loop
oFile.close
set oFile = nothing
set oFSO = nothing
'clean up your recordset and connection objects
%>
<%
oRS.Close()
%>
<%
Response.Redirect(FileName
%>
I finally saw the reason. each time it loops the recordset, oField.value variable is given a new value which is added to the stored variable strOutput which has the value of the previous transaction. All that is required is to clear this variable value to "" just before each loop takes place! so modified code is:
<%
Session("FileName")
FileName= "thisfile001.csv"
%>
<%
set oRS = Server.CreateObject("ADODB
oRS.ActiveConnection = MM_Honda_STRING
oRS.Source = "SELECT DISTINCT * FROM dbo.countries"
oRS.CursorType = 0
oRS.CursorLocation = 2
oRS.LockType = 3
oRS.Open()
oRS_numRows = 0
%>
<%
dim oFSO, oFile,oField
dim strOutput
set oFSO = server.createobject ("scripting.filesystemobje
set oFile = oFSO.createtextfile("D:\we
do while not oRS.eof
strOutput = "" 'this is the new line which clears the value before getting the next record!
for each oField in oRS.fields
strOutput = strOutput & oField.value & ","
next
'remove the last comma from the line before continuing
strOutput = left(strOutput,len(strOutp
oFile.writeline strOutput
oRS.movenext
Loop
oFile.close
set oFile = nothing
set oFSO = nothing
'clean up your recordset and connection objects
%>
<%
oRS.Close()
%>
<%
Response.Redirect(FileName
%>
Business Accounts
Answer for Membership
by: thunderchickenPosted on 2005-01-25 at 10:50:02ID: 13135111
Dim mystring as string
while not rs.EOF
mystring = mystring & rs(0) & ", "
mystring = mystring & vbCrLf
rs.movenext
Wend
Then just write mystring to a file