Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sending a report to Excel object in asp

Status
Not open for further replies.

accessguy52

Programmer
Sep 18, 2002
73
US
Hello folks, in appreciation to all who helped me, I'd like to post a small but simple solution to putting up reports in your bowser object by way of Excel. This example was used by me courtesy of Steven Smith's example ( I just used a simple table in Access with two 2 fields: Country and TotalSalesByCountry (or something like that - you could name it GDP if you want). Here's the code including Steve's suggestion for a msgbox in case of no records found:

<%


'Open the connection and retrieve data from the database
Set cn = Server.CreateObject(&quot;ADODB.Connection&quot;)


cn.Open &quot;DSN=mgraph2;UID=&quot;&quot;;PWD=&quot;&quot;;DATABASE=mgraph2&quot;
Set rs = cn.Execute(&quot;SELECT * FROM Countries&quot;)
%>

if rs.BOF and rs.EOF then%>
<HTML>
<head>
<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
<!-- Hide Javascript
{
alert(&quot;No records found -- click OK, then use the Back button to return to form&quot;)
}
// - End hide Javascript - -->
</Script>
else
Response.ContentType = &quot;application/vnd.ms-excel&quot;

<TABLE BORDER=1>
<TR>
<%
'Loop thru field names and print out field names
j = 2 'row counter
For i = 0 to rs.Fields.Count - 1
%>
<TD BGCOLOR=YELLOW><B><% = rs(i).Name %></B></TD>
<% Next %>
</TR>
<%
'Loop thru rows displaying each field.
Do While not rs.EOF
%>
<TR>
<% For i = 0 to rs.Fields.Count - 1
%>
<TD VALIGN=TOP><% = rs(i) %></TD>
<% Next %>
</TR>
<%
rs.MoveNext
j = j + 1
Loop
'Clean up
rs.Close
%>

</TABLE>

Just substitute your own tablename and so on. Hope this helps someone else.

Accessguy52
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top