Dynamic CSV file

Results 1 to 2 of 2

Thread: Dynamic CSV file

  1. #1
    Join Date
    Dec 1969

    Default Dynamic CSV file

    Not sure if this one is possible or not, I need to create a csv file that is then downloaded by the user after they click a link. I have it working perfectly (more or less) with it using an fso and creating the file on the server first, however, I have been told by the powers that be, try to do this without writing the file to the server. Any ideas anyone?<BR><BR>Thanks,<BR>J

  2. #2
    Join Date
    Dec 1969

    Default Try this...

    This code will dump one entire table (or any other SQL SELECT result) from a DB into a ".csv" pseudo-file and send it off to the user.<BR><BR>Note that you CAN NOT use &#060;HTML&#062; or &#060;BODY&#062; or other tags! You *must* send *ONLY* the CSV data.<BR><BR>Oh...and you better hope that the user has Excel installed or they will get garbage.<BR><BR>********************<BR><BR>&#060; %<BR>Response.ContentType = "application/msexcel"<BR>Response.addHeader "content-disposition", "attachment;filename=myFile.csv"<BR><BR>Set conn = Server.CreateObject("ADODB.Connection")<BR>conn.Op en "...whatever..."<BR><BR>Set RS = conn.Execute("SELECT * FROM tablename")<BR><BR>&#039; get column names into CSV form...<BR>Dim colnames<BR>ReDim colnames( RS.Fields.Count-1 )<BR>For fnum = 0 To RS.Fields.Count-1<BR> colnames = RS.Fields(fnum).Name<BR>Next<BR>strColumns = """" & Join( colnames, """,""" ) & """"<BR><BR>&#039; now convert data to csv string...<BR>data = RS.GetString( , , """,""", """" & vbNewLine & """" )<BR>&#039; tack on starting " but remove the bogus one that starts last line<BR>data = """" & Left( data, Len(data)-1 )<BR><BR>Response.Write strColumns & vbNewLine & data<BR>RS.Close<BR>conn.Close<BR>%&#062;<BR><BR> <BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts