Solved

Export a recordset to a csv file

Posted on 2003-11-29
2
943 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
  • 2
2 Comments
 
LVL 33

Accepted Solution

by:
hongjun earned 125 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
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/…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now