Do not use on any
shared computer
September 8, 2008 04:08am pdt
 
[x]
Attachment Details

creating a csv file in asp

Tags: csv, asp, file
I have a query that retrieves records from a db, which i dispaly in a table. However I would also like to create a csv file and also allow the user to save it. How do i go about doing this? As i understand a csv file is just a file with values seperated by commas.
Start your free trial to view this solution
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Question Stats
Zone: Web Development
Question Asked By: rubans
Solution Provided By: kingsfan76
Participating Experts: 6
Solution Grade: A
Views: 278
Translate:
Loading Advertisement...
 
[+][-]Expert Comment by thunderchicken

Rank: Guru

Expert Comment by thunderchicken:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by fritz_the_blank

Rank: Genius

Expert Comment by fritz_the_blank:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by fritz_the_blank

Rank: Genius

Expert Comment by fritz_the_blank:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by jkwasson
Expert Comment by jkwasson:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by fritz_the_blank

Rank: Genius

Expert Comment by fritz_the_blank:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Accepted Solution by kingsfan76

Rank: Guru

Accepted Solution by kingsfan76:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by esthera
Expert Comment by esthera:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Open Discussion
Open Discussion
 
Comment by mediatide
I tried all the solutions and could only get the one by jkwasson to work on my application. even with this I have encountered a problem I cannot seem to fix. The code runs then opens the .xls file but I find that each successive row seems to concatenate on the row before (eg) excel row 1 =table row 1, Excel row 2 = table row 1 + row 2. This continues until the final Excel row contains all the table rows consecutively. I cannot seem to fix the code to return only one Excel row per table row....

My code:

<%
Session("FileName")
FileName= "thisfile001.csv"
%>
<%
set oRS = Server.CreateObject("ADODB.Recordset")
oRS.ActiveConnection = MM_Honda_STRING
oRS.Source = "SELECT DISTINCT *  FROM dbo.countries"
oRS.CursorType = 0
oRS.CursorLocation = 2
oRS.LockType = 3
oRS.Open()
oRS_numRows = 0
%>
<%
dim oFSO, oFile,oField
dim strOutput

set oFSO = server.createobject ("scripting.filesystemobject")
set oFile = oFSO.createtextfile("D:\websites\tidyco\portals.tidyco\supplier\" & FileName,true)
do while not oRS.eof
    for each oField in oRS.fields
        strOutput = strOutput & oField.value & ","
    next
    'remove the last comma from the line before continuing
strOutput = left(strOutput,len(strOutput)-1)
    oFile.writeline strOutput
    oRS.movenext
Loop

oFile.close
set oFile = nothing
set oFSO = nothing
'clean up your recordset and connection objects
%>
<%
oRS.Close()
%>
<%
Response.Redirect(FileName)
%>
 
 
Comment by mediatide
I finally saw the reason. each time it loops the recordset, oField.value variable is given a new value which is added to the stored variable strOutput which has the value of the previous transaction. All that is required is to clear this variable value to "" just before each loop takes place!  so modified code is:

<%
Session("FileName")
FileName= "thisfile001.csv"
%>
<%
set oRS = Server.CreateObject("ADODB.Recordset")
oRS.ActiveConnection = MM_Honda_STRING
oRS.Source = "SELECT DISTINCT *  FROM dbo.countries"
oRS.CursorType = 0
oRS.CursorLocation = 2
oRS.LockType = 3
oRS.Open()
oRS_numRows = 0
%>
<%
dim oFSO, oFile,oField
dim strOutput

set oFSO = server.createobject ("scripting.filesystemobject")
set oFile = oFSO.createtextfile("D:\websites\tidyco\portals.tidyco\supplier\" & FileName,true)
do while not oRS.eof
      strOutput = ""      'this is the new line which clears the value before getting the next record!
    for each oField in oRS.fields
        strOutput = strOutput & oField.value & ","
    next
    'remove the last comma from the line before continuing
strOutput = left(strOutput,len(strOutput)-1)
    oFile.writeline strOutput
    oRS.movenext
Loop

oFile.close
set oFile = nothing
set oFSO = nothing
'clean up your recordset and connection objects
%>
<%
oRS.Close()
%>
<%
Response.Redirect(FileName)
%>
 
 
20080723-EE-VQP-34