?
Solved

Export a recordset to a csv file

Posted on 2003-11-29
2
Medium Priority
?
948 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

801 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