Solved

Export a recordset to a csv file

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP SQL Syntax Duplicate Key 7 116
Query not working correctly? (or how i want it to... 6 54
Writing comments on <p></P> or paragraph 2 23
Asp in script 6 46
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…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

726 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