Slick way to generate a CSV for user download?

M

MyndPhlyp

I've been trying to Google this but have been getting far too many hits to
be of use. Is there a slick way to generate a CSV or other Excel-friendly
format and push it to the client? I'm using VBScript (and HTML and other
typical stuff of course) on the ASP pages (no .NET stuff). Just a shove in
the right direction is all I really need here.
 
M

Mark Schupp

Response.ContentType = "text/comma-separated-values"
Response.AddHeader "Content-Disposition","attachment; filename=extract.csv"

For i = 1 to 10

Next
Response.Write CSVString( "row" & CStr(i)) & "," & CSVString("column 2")
& vbCrLf
Response.end

Function CSVString( ByVal strIn )
If IsNull( strIn ) Then strIn = ""
CSVString = """" & Replace(strIn, """", "'") & """"
CSVString = Replace(CSVString, vbCrLf, "<cr>")
End Function
 
K

Kyle Peterson

just query the database... write the text file string line by line and
create the file however you wan it with the filesystem object

its pretty easy really
 
M

MyndPhlyp

Mark Schupp said:
Response.ContentType = "text/comma-separated-values"
Response.AddHeader "Content-Disposition","attachment; filename=extract.csv"

For i = 1 to 10

Next
Response.Write CSVString( "row" & CStr(i)) & "," & CSVString("column 2")
& vbCrLf
Response.end

Function CSVString( ByVal strIn )
If IsNull( strIn ) Then strIn = ""
CSVString = """" & Replace(strIn, """", "'") & """"
CSVString = Replace(CSVString, vbCrLf, "<cr>")
End Function

Yeah, that's kind of what I was looking for. Short, sweet and simple. Thanx
for ruining my weekend with yet another thing to play around with. <G> (It's
s'posed to rain anyway.)
 
H

Hal Rosser

MyndPhlyp said:
I've been trying to Google this but have been getting far too many hits to
be of use. Is there a slick way to generate a CSV or other Excel-friendly
format and push it to the client? I'm using VBScript (and HTML and other
typical stuff of course) on the ASP pages (no .NET stuff). Just a shove in
the right direction is all I really need here.

Create the csv file on the server
provide a link to that file on the asp page
 
M

MyndPhlyp

Hal Rosser said:
Create the csv file on the server
provide a link to that file on the asp page

Thanks Hal, but the Response.Write et al solution earlier in this thread
appears to be the better alternative to files. The CSV, although I didn't
mention it, is the contents of a tabular report currently being viewed by
the user so it has to be generated on demand. As you might guess, this is
for an internal web. Doint it this way allows me to skip the creation to
disk and instead just push the stuff to the user ... on demand, of course.
 
R

Roland Hall

in message : Response.ContentType = "text/comma-separated-values"
: Response.AddHeader "Content-Disposition","attachment;
filename=extract.csv"
:
: For i = 1 to 10
:
: Next
: Response.Write CSVString( "row" & CStr(i)) & "," & CSVString("column
2")
: & vbCrLf
: Response.end
:
: Function CSVString( ByVal strIn )
: If IsNull( strIn ) Then strIn = ""
: CSVString = """" & Replace(strIn, """", "'") & """"
: CSVString = Replace(CSVString, vbCrLf, "<cr>")
: End Function

In this example, shouldn't the response.write goes inside the for...next
loop?
And, how do you keep from getting the Action cancelled page?

--
Roland Hall
/* This information is distributed in the hope that it will be useful, but
without any warranty; without even the implied warranty of merchantability
or fitness for a particular purpose. */
Technet Script Center - http://www.microsoft.com/technet/scriptcenter/
WSH 5.6 Documentation - http://msdn.microsoft.com/downloads/list/webdev.asp
MSDN Library - http://msdn.microsoft.com/library/default.asp
 
M

MyndPhlyp

Roland Hall said:
in message : Response.ContentType = "text/comma-separated-values"
: Response.AddHeader "Content-Disposition","attachment;
filename=extract.csv"
:
: For i = 1 to 10
:
: Next
: Response.Write CSVString( "row" & CStr(i)) & "," & CSVString("column
2")
: & vbCrLf
: Response.end
:
: Function CSVString( ByVal strIn )
: If IsNull( strIn ) Then strIn = ""
: CSVString = """" & Replace(strIn, """", "'") & """"
: CSVString = Replace(CSVString, vbCrLf, "<cr>")
: End Function

In this example, shouldn't the response.write goes inside the for...next
loop?
And, how do you keep from getting the Action cancelled page?

It is an oversimplified example probably modified in haste. I think just
about everybody has been guilty of that at least once in their life. Yes,
the Response.Write should be between the For and Next. My query was
interpreted correctly though - how to push a file to a browser. The key bits
are the Response.ContentType and Response.AddHeader methods (to which I add
Response.Clear at the beginning and Response.Flush just before the
Response.End). What happens between the Response.AddHeader and the
Response.Flush, Response.End is really of no consequence.
 
R

Roland Hall

in message :
: : > "Mark Schupp" wrote in message
: : > : Response.ContentType = "text/comma-separated-values"
: > : Response.AddHeader "Content-Disposition","attachment;
: > filename=extract.csv"
: > :
: > : For i = 1 to 10
: > :
: > : Next
: > : Response.Write CSVString( "row" & CStr(i)) & "," &
CSVString("column
: > 2")
: > : & vbCrLf
: > : Response.end
: > :
: > : Function CSVString( ByVal strIn )
: > : If IsNull( strIn ) Then strIn = ""
: > : CSVString = """" & Replace(strIn, """", "'") & """"
: > : CSVString = Replace(CSVString, vbCrLf, "<cr>")
: > : End Function
: >
: > In this example, shouldn't the response.write goes inside the for...next
: > loop?
: > And, how do you keep from getting the Action cancelled page?
:
: It is an oversimplified example probably modified in haste. I think just
: about everybody has been guilty of that at least once in their life. Yes,
: the Response.Write should be between the For and Next. My query was
: interpreted correctly though - how to push a file to a browser. The key
bits
: are the Response.ContentType and Response.AddHeader methods (to which I
add
: Response.Clear at the beginning and Response.Flush just before the
: Response.End). What happens between the Response.AddHeader and the
: Response.Flush, Response.End is really of no consequence.

It is to me. I was asking for knowledge, not to challenge. I wasn't aware
I could send content as a file without first having a file.

--
Roland Hall
/* This information is distributed in the hope that it will be useful, but
without any warranty; without even the implied warranty of merchantability
or fitness for a particular purpose. */
Technet Script Center - http://www.microsoft.com/technet/scriptcenter/
WSH 5.6 Documentation - http://msdn.microsoft.com/downloads/list/webdev.asp
MSDN Library - http://msdn.microsoft.com/library/default.asp
 
M

MyndPhlyp

Roland Hall said:
It is to me. I was asking for knowledge, not to challenge. I wasn't aware
I could send content as a file without first having a file.

I too was unaware ... 'till now. It has since been permanently recorded in
that feeble cabinet I call a brain (only to eventually get lost in the dust
of time).
 
M

MyndPhlyp

Dave Anderson said:
http://msdn.microsoft.com/library/en-us/ado270/htm/mdmthgetstringmethod(recordset)ado.asp


Cool. I wasn't aware of the Recordset.GetString method. Thanx.

BTW - I used to know a Dave Anderson hundreds of years ago in New England.
We called him "Uncle Dave" 'cus he was an only child (and therefore would
never be an uncle). On a longshot, would that happen to be you?
 
M

Mark Schupp

: It is an oversimplified example probably modified in haste. I think just
: about everybody has been guilty of that at least once in their life. Yes,
: the Response.Write should be between the For and Next. My query was
: interpreted correctly though - how to push a file to a browser. The key

It was between the for and next when I tested the code. It pasted into the
message very strangely. I did notice and thought I fixed it (my whole life
has been weird lately).

IIRC the "action canceled" is a weird bug (or maybe a "feature") in IE.
Having to do with whether the page is called using GET or POST. Try using
GET.
 
R

Roland Hall

in message :
: : >
: > It is to me. I was asking for knowledge, not to challenge. I wasn't
: aware
: > I could send content as a file without first having a file.
:
: I too was unaware ... 'till now. It has since been permanently recorded in
: that feeble cabinet I call a brain (only to eventually get lost in the
dust
: of time).

Lucky brain owner... heading to Oz.

--
Roland Hall
/* This information is distributed in the hope that it will be useful, but
without any warranty; without even the implied warranty of merchantability
or fitness for a particular purpose. */
Technet Script Center - http://www.microsoft.com/technet/scriptcenter/
WSH 5.6 Documentation - http://msdn.microsoft.com/downloads/list/webdev.asp
MSDN Library - http://msdn.microsoft.com/library/default.asp
 
R

Roland Hall

in message :> : It is an oversimplified example probably modified in haste. I think
just
: > : about everybody has been guilty of that at least once in their life.
: Yes,
: > : the Response.Write should be between the For and Next. My query was
: > : interpreted correctly though - how to push a file to a browser. The
key
:
: It was between the for and next when I tested the code. It pasted into the
: message very strangely. I did notice and thought I fixed it (my whole life
: has been weird lately).

That is weird. You must have a special browser. Could probably call it Ed.
(O;=

: IIRC the "action canceled" is a weird bug (or maybe a "feature") in IE.
: Having to do with whether the page is called using GET or POST. Try using
: GET.

I'll try that. I would assume (cough) this means this is not an option to
send an attachment by server-side email, or is it, meaning gathering data
and sending an attachment by CDOSYS without first saving it as a file?

--
Roland Hall
/* This information is distributed in the hope that it will be useful, but
without any warranty; without even the implied warranty of merchantability
or fitness for a particular purpose. */
Technet Script Center - http://www.microsoft.com/technet/scriptcenter/
WSH 5.6 Documentation - http://msdn.microsoft.com/downloads/list/webdev.asp
MSDN Library - http://msdn.microsoft.com/library/default.asp
 
M

MyndPhlyp

Roland Hall said:
in message :
: I too was unaware ... 'till now. It has since been permanently recorded in
: that feeble cabinet I call a brain (only to eventually get lost in the
dust
: of time).

Lucky brain owner... heading to Oz.

I am not too sure about the "lucky" part. There appears to be significant
irreversible damage and I strongly suspect it is a hand-me-down from a
research project at a remote asylum. Whether or not it is even accurate to
call it a "brain" is left up to the medical examiner that will perform the
eventual autopsy hopefully many years from now. But thanks for the optimism.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top