Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Export a recordset to a csv file

Posted on 2003-11-29
2
Medium Priority
?
949 Views
Last Modified: 2008-05-11
I would like to export the contents of a recordset to a csv file. The first line of the csv file should contain the colmn names
0
Comment
Question by:strangedesign
[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
2 Comments
 
LVL 33

Accepted Solution

by:
hongjun earned 500 total points
ID: 9844240
try this


<%
  'Output the page in plain text format
  Response.ContentType = "text/csv"
  Response.AddHeader "Content-Disposition", "filename=email_list.csv;"

  'Connect to database and get recordset of subscriber batch
  dim objRS, objConn

  set objConn = Server.CreateObject("ADODB.Connection")
  objConn.Open("DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" & Server.MapPath("yourmdb.mdb")
  set objRS = objConn.Execute("select * from yourtable")

  'Write out field headers
  dim strField
  Response.Write("'") 'Fix for Excel import
  for each strField in objRS.Fields
    Response.Write(strField.Name & ", ")
  next
  Response.Write(vbCrLf)

  'Write out subscriber details
  do while not (objRS.BOF or objRS.EOF)
    for each strField in objRS.Fields
      Response.Write(strField.Value & ", ")
    next
    objRS.MoveNext
    Response.Write(vbCrLf)
  loop

  objConn.Close
  set objRS = nothing
  set objConn = nothing
  Response.End()
%>



hongjun
0
 
LVL 33

Expert Comment

by:hongjun
ID: 9844280
this could be better if you want it to save to your web root automatically.


<%
dim strFile, strtext
dim a

strFile="\myfile"
Set fs = Server.CreateObject("Scripting.FileSystemObject")
Set a = fs.OpenTextFile(server.MapPath("\") & strFile & ".csv", 8, true)


  'Connect to database and get recordset of subscriber batch
  dim objRS, objConn

  set objConn = Server.CreateObject("ADODB.Connection")
  objConn.Open("DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" & Server.MapPath("yourmdb.mdb"))
  set objRS = objConn.Execute("select * from yourtable")

  'Write out field headers
  dim strField

  strfield = "'"

  for each strField in objRS.Fields
    strtext = strtext & strField.Name & ", "
  next
  strtext = strtext & vbCrLf

  'Write out subscriber details
  do while not (objRS.BOF or objRS.EOF)
    for each strField in objRS.Fields
      strtext = strtext & strField.Value & ", "
    next
    objRS.MoveNext
    strtext = strtext & vbCrLf
  loop

  objConn.Close
  set objRS = nothing
  set objConn = nothing

 a.Writeline(strtext)

  Response.End()
%>


hongjun
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

618 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