ASP export excle

Source: Internet
Author: User

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
%>

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.