Less nonsense, please look at the code:
Runquery.asp
<%@ language= "VBSCRIPT"%>
<%
' Dsnless connection to Access Database
Strdsnpath = "Provider=msdasql;driver={microsoft Access DRIVER (*.mdb)};D bq=" & Server.MapPath ("Testdb.mdb")
%>
<!--#include file= "adovbs.inc"--> please copy this file yourself
<%
server.scripttimeout=1000
Response.Buffer = True
if (Request.Form ("returnas") = "Content") Then
Response.ContentType = "Application/msexcel"
End If
Response.Expires = 0
Dim oconn
Dim ORS
Dim strSQL
Dim strfile
Set oconn = Server.CreateObject ("ADODB. Connection ")
Set ORS = Server.CreateObject ("ADODB. Recordset ")
strSQL = BuildSQL ()
oRS.Open strSQL, Strdsnpath, adOpenForwardOnly, adLockReadOnly, adCmdText
%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<title>excel Export demo</title>
<body>
<%
if (Request.Form ("returnas") = "CSV") Then
Createcsvfile ()
else if (Request.Form ("returnas") = "Excel") Then
Createxlsfile ()
else if (Request.Form ("returnas") = "HTML") Then
Genhtml ()
else if (Request.Form ("returnas") = "Content") Then
Genhtml ()
End If
End If
End If
End If
Set ORS = Nothing
Set oconn = Nothing
Response.Flush
%>
</body>
<script Language=vbscript runat=server>
Function BuildSQL ()
Dim strSQL
Dim strtemp
strtemp = ""
strSQL = "Select year, Region, Sales_amt from Sales"
if (Request.Form ("year") <> "all") then
strtemp = "Where year ="
strtemp = strtemp & Request.Form ("year")
End If
if (Request.Form ("Region") <> "all") then
if (Len (strtemp) > 0) Then
strtemp = strtemp & "and Region ="
Else
strtemp = Strstl & "Where Region ="
End If
strtemp = strtemp & "'"
strtemp = strtemp & Request.Form ("Region")
strtemp = strtemp & "'"
End If
BuildSQL = strSQL & strtemp
End Function
Function GenFileName ()
Dim fname
fname = "File"
Systime=now ()
Fname= fname & CStr (Year (systime)) & CStr (month (systime)) & CStr (Day (systime))
Fname= fname & CStr (Hour (systime)) & CStr (Minute (systime)) & CStr (second (systime))
GenFileName = fname
End Function
Function genhtml ()
Response.Write ("<div align=center><font size=+1>sales reporting</font></div>")
Response.Write ("<table width=100% border=1 cellspacing=1 cellpadding=1>")
Response.Write ("<TR>")
Response.Write ("<TD>Year</TD>")
Response.Write ("<TD>Region</TD>")
Response.Write ("<TD>Sales</TD>")
Response.Write ("</TR>")
if (Ors.bof = True and ors.eof = True) Then
Response.Write ("Database Empty")
Else
Ors.movefirst
Do as not ors.eof
Response.Write ("<TR>")
Response.Write ("<TD>")
Response.Write (Ors.fields ("year"). Value)
Response.Write ("</TD>")
Response.Write ("<TD>")
Response.Write (Ors.fields ("Region"). Value)
Response.Write ("</TD>")
Response.Write ("<TD>")
Response.Write (Ors.fields ("Sales_amt"). Value)
Response.Write ("</TD>")
Response.Write ("</TR>")
Ors.movenext
Loop
Response.Write ("</TABLE>")
End If
End Function
Function Createcsvfile ()
strfile = GenFileName ()
Set fs = Server.CreateObject ("Scripting.FileSystemObject")
Set a = fs. CreateTextFile (server. MapPath (".") & "\" & strfile & ". csv", True)
If not ors.eof Then
strtext = CHR & "Year" & Chr (+) & ","
strtext = strtext & Chr (+) & "Region" & Chr (+) & ","
strtext = strtext & Chr (+) & &