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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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


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.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

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.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.