欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp将table生成excel文件(xls)

程序员文章站 2022-03-14 10:47:33
复制代码 代码如下:<%@language="vbscript" codepage="936"%> <% if request("action")=1 t...
复制代码 代码如下:

<%@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">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>无标题文档</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>
</head>

<body>
<input type="button" value="导出数据" onclick="tabletoexcel()" />
<%
connstr="..."
set conn = server.createobject("adodb.connection")
conn.open connstr

set rs = server.createobject("adodb.recordset")
rs.open "select top 10 * from [你的表名]",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>序号</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 '在第五条的时候隐藏数据,经过测试如果是display为none的数据是不会导出来的
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>
</html>