How to use asp to convert sqlserver data to execl files

Source: Internet
Author: User
Tags chr


<% @ 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 \\\');
...

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.