ASP to build table Excel file (xls) _ Application Tips

Source: Internet
Author: User
Copy Code code as follows:

<% @LANGUAGE = "VBSCRIPT" codepage= "936"%>
<%
If Request ("action") =1 Then
Response.contenttype= "Application/ms-excel"
Response.AddHeader "Content-disposition", "Attachment;filename=www.xls"
End If
%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title> Untitled Document </title>
<style type= "Text/css" >
Table {
border-top:1px solid #003399;
border-left:1px solid #003399;
}
TD {
border-right:1px solid #003399;
border-bottom:1px solid #003399;
}
thead {
Background-color: #000066;
Font-weight:bold;
padding:5px;
Color: #FFFFFF;
}
</style>
<script language= "JavaScript" >
function Tabletoexcel () {
location.href= ' action=1 ';
}
</script>

<body>
<input type= "button" value= "Export Data" onclick= "Tabletoexcel ()"/>
<%
Connstr= "..."
Set conn = Server.CreateObject ("ADODB. Connection ")
Conn. Open ConnStr

Set rs = server. CreateObject ("Adodb.recordset")
Rs.Open "SELECT top * from [Your table name]", conn,1,1
If not (rs.eof and RS.BOF) then
Column = Rs.fields.count
Response. Write ("<table cellpadding= ' 0 ' cellspacing= ' 0 ' >")
Response. Write ("<thead><td> serial number </td>")
For each F in Rs.fields
Response. Write ("<td>" & F.name & "</td>")
Next
Response. Write ("</thead>")
For j = 1 to Rs.recordcount
If J > 5 Then ' hides the data in the fifth, tested if the display is none of the data is not derived
Response. Write ("<tr style= ' Display:none ' >")
Else
Response. Write ("<tr>")
End If
Response. Write ("<td>" & J & "</td>")
For i = 0 to Column-1
Response. Write ("<td>" & RS (i) & "</td>")
Next
Response. Write ("</tr>")
Rs.movenext
Next
Response. Write ("</table>")
End If
Rs.close
Set rs = Nothing
Conn.close
Set conn = Nothing
%>
</body>
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.