Write to Excel using ADO from ASP

Hi All

I am trying to export data from my asp page using ADO to an Excel worksheet. This is what i have so far



Dim sql
Dim Rs

set conn=Server.CreateObject("ADODB.Connection")

conn.open = "Driver={Sybase System 11};Srvr=DEV-DATABASE;DB=AAAA_0500;UID=username;PWD=PASSWORD"

If Conn.State = 0 Then

response.write "Connection Failed"

End If

conn.cursorlocation = 3

sql = "select Study_site_no,Study_pat_id,visit from report"

   Set Rs = Server.CreateObject("ADODB.Recordset")

      Rs.CursorType = adOpenDynamic
      Rs.CursorLocation = 3
      Rs.Open sql,Conn,3,1

Response.ContentType = "application/vnd.ms-excel"

Response.AddHeader "Content-Disposition", "attachment; filename=Enquiry_List.xls"

if Rs.eof <> true then

response.write "<table border=1>"

while not Rs.eof

response.write "<tr><td>" & Rs.fields("ID") & "</td><td>" & 

Rs.fields("Study_site") & "</td><td>" & Rs.fields("Study_Pat") & "</td><td>" & Rs.fields("Visit") & 

response.write "</table>"
end if
set rs=nothing

I am getting a compilation error at response.write "<tr><td>" & Rs.fields("ID") & "</td><td>" &

Any help is highly appreciated..

If this code is wrong and if there are some good examples it would be great.

Who is Participating?
s_mokenConnect With a Mentor Commented:

mmm not sure how you got that line

anyway if you look at the line in your first post which is fine;

Rs.fields("Study_site") & "</td><td>" & Rs.fields("Study_Pat") & "</td><td>" & Rs.fields("Visit") & 

and this line you just posted
 "<tr><td>" & Rs.fields("Study_Site") & "</td><td>" Rs.fields("Study_Pat") "</td></tr>"

where are the '&' symbols between on either side of the  Rs.fields("Study_Pat") ???
"<tr><td>" & Rs.fields("Study_Site") & "</td><td>" & Rs.fields("Study_Pat") & "</td></tr>"
                                                                            ^                                  ^

Its quite OK for all this to be on the same line, like so;

response.write "<tr><td>" & Rs.fields("ID") & "</td><td>" & Rs.fields("Study_site") & "</td><td>" & Rs.fields("Study_Pat") & "</td><td>" & Rs.fields("Visit") & "</td></tr>"

but it appeared that you had separated them out into three lines, thats fine just so long as the continuation is on the immediate next line and a '_' (underscore) is used at the end, to indicate  continue on to the next line

As shown below this is all taken as one line
response.write "<tr><td>" & Rs.fields("ID") & "</td><td>" & Rs.fields("Study_site") _
& "</td><td>" & Rs.fields("Study_Pat") & "</td><td>" & Rs.fields("Visit") & "</td></tr>"

remove the '_' from the end of the first line and it will be taken as two separate lines.

You can't end a line with '&'

if this line is supposed to continue onto the next line then put an '_' on the end, and get rid of the blank lines.

response.write "<tr><td>" & Rs.fields("ID") & "</td><td>" & _
Rs.fields("Study_site") & "</td><td>" & Rs.fields("Study_Pat") & "</td><td>" & Rs.fields("Visit") & _

all that text is now effectively on the same line
sramkrisAuthor Commented:

I changed my code to what you had but i get an erroe expected end of statement

response.write "<tr><td>" & Rs.fields("Study_Site") & "</td><td>" Rs.fields("Study_Pat") "</td></tr>"

Not sure what is wrong here

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

sramkrisAuthor Commented:
Hi smoken

I will accpet this as the answer but i just wanted to ask one more thing. If its possible can you tell me how do i get the column name to print before i print the data.
in the excel sheet.

Thanks a lot

Well anything that goes into your html table will be exported to the excel spreedsheet, so you just have to put the Column Names as the first row in your table e.g.
response.write "<tr><td>Column1</td><td>Column2</td><td>Column3</td></tr>......etc"
if you use <th> then the headers will be Bold and Centered in Excel.
See line 2 below on how it would appear in your code.
simple as that

response.write "<table border=1>"
response.write "<tr><th>Column1</th><th>Column2</th><th>Column3</th></tr>"
while not Rs.eof
response.write "<tr><td>" & Rs.fields(0) & "</td><td>" & _
Rs.fields(1) & "</td><td>" & Rs.fields(2) & "</td><td>" & Rs.fields(3) & _

sramkrisAuthor Commented:
Hi s_moken

I have posted another question an extension of the same problem could you please look in to that and help me out.

All Courses

From novice to tech pro — start learning today.