<% @ LANGUAGE = "VBSCRIPT" %>
<% Option explicit %>
<%
\\\ 'Example AS: put a 24-hour online user in the database into an EXCEL file
\\\ 'Author: Steel worker
\ 'Email: hello_hhb@21cn.com
\\\ 'Date:
\\\ 'Test: passed the TEST in NT4, SP6, SQL SERVER 7.0, and EXCEL2000
%>
<HTML>
<HEAD>
<Meta content = "text/html; charset = gb2312" http-equiv = "Content-Type">
<TITLE> generate an EXCEL file </TITLE>
</HEAD>
<Body>
<A href = "dbtoexcel. asp? Act = make "> EXCEL for generating online population </a>
<Hr size = 1 align = left width = 300px>
<%
If Request ("act") = "" then
Else
Dim conn
Set conn = server. CreateObject ("adodb. connection ")
Conn. Open "test", "sa ",""
\\\ 'Conn. Open Application ("connstr ")
Dim rs, SQL, filename, fs, myfile, x, link
Set fs = server. CreateObject ("scripting. filesystemobject ")
\\\' -- Suppose you want to store the generated EXCEL file as follows
Filename = "c: online.xls"
\\\' -- Delete an EXCEL file if it exists.
If fs. FileExists (filename) then
Fs. DeleteFile (filename)
End if
\ '-- Create an EXCEL file
Set myfile = fs. CreateTextFile (filename, true)
Set rs = Server. CreateObject ("ADODB. Recordset ")
\\\' -- Check the data you want to put in EXCEL from the database
SQL = "select population, hourpos, datepos from populationperhour order by datepos, hourpos asc"
Rs. Open SQL, conn
If rs. EOF and rs. BOF then
Else
Dim strLine, responsestr
StrLine = ""
For each x in rs. fields
StrLine = strLine & x. name & chr (9)
Next
\\\' -- Write the column name of the table into EXCEL
Myfile. writeline strLine
Do 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
Loop
End if
Rs. Close
Set rs = nothing
Conn. close
Set conn = nothing
Set myfile = nothing
Set fs = Nothing
Link = "<a href =" & filename & "> Open The Excel File </a>"
Response. write link
End if
%>
</BODY>
</HTML>
2. Database related:
Create table [populationperhour] (
[Population] [int] not null,
[Hourpos] [int] not null,
[Datepos] [datetime] NOT NULL
);
Insert into populationperhour values (\\\ '000000\\ ', \\\ '1 \\\', \\\ '2017-1-11 \\\');
Insert into populationperhour values (\\\ '000000\\ ', \\\ '2 \\', \\\ '2017-1-11 \\\');
...