Solved

Export a recordset to a csv file

Posted on 2003-11-29
2
947 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 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

724 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