Downfile. asp
<Body>
<%
Set rs = server. Createobject ("ADODB. recordset ")
SQL = "Exec gettbl"
Sql1 = "Exec gettbl1"
Rs. Open SQL, connectionstring, 1, 1
'Rs1. Open sql1, connectionstring, 1, 1
Set FS = server. Createobject ("scripting. FileSystemObject ")
'-- Suppose you want to store the generated Excel file as follows:
'-- Outtemp must have the everyone permission
Filename = "outtemp \ table" & datepart ("YYYY", now) & datepart ("M", now) & datepart ("D", now) & datepart ("H", now) & datepart ("N", now) & datepart ("S", now )&". xls"
Fn = filename
'-- If the original Excel file exists, delete it.
Filename = left (server. mappath (request. servervariables ("path_info"), cmdrev (server. mappath (request. servervariables ("path_info"), "\") & filename
If fs. fileexists (filename) then
FS. deletefile (filename)
End if
'-- Create an Excel file
Set myfile = FS. createtextfile (filename, true)
Dim strline, responsestr
Strline = ""
For each X in RS. Fields
Strline = strline & X. Name & CHR (9)
Next
'-- Write the column name of the table to excel first
Myfile. writeline strline
While not Rs. EOF
Strline = ""
For each X in RS. Fields
Strline = strline & X. Value & CHR (9)
Next
'-- Write table data to excel
Myfile. writeline strline
Rs. movenext
Wend
Rs. Close
'-- Write the data of the second table
Rs. open sql1, connectionstring, 1, 1
strline = ""
for each X in RS. fields
strline = strline & X. name & CHR (9)
next
'-- write the table column name to excel
myfile. writeline strline
while not Rs. EOF
strline = ""
for each X in RS. fields
strline = strline & X. value & CHR (9)
next
'-- write table data to excel
myfile. writeline strline
Rs. movenext
Wend
'response. write " export an Excel file "
response. redirect (FN)
set myfile = nothing
set FS = nothing
Rs. close
%>