Using ASP to make Execl report method (II.)

Source: Internet
Author: User
Tags chr access database
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 (+) & &



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.