asp将table生成excel文件(xls)

来源:asp之家 时间:2011-03-07 11:17:00 

代码如下:


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

标签:asp,excel
0
投稿

猜你喜欢

  • oracle 数据库连接分析

    2009-07-28 10:42:00
  • 科讯CMS编辑器会自动更改代码

    2008-12-12 13:00:00
  • CSS.JS文件发布机制的思考

    2009-08-04 13:07:00
  • asp如何去除HTML标签

    2010-06-07 20:47:00
  • Design IT. (8),一匹“更快的马”

    2009-02-11 12:19:00
  • 将HTML表单数据存储为XML格式

    2007-08-23 13:04:00
  • 用 JS 来控制 target 链接属性

    2007-10-08 12:59:00
  • ASP 关于动态数据显示页面得锚点

    2007-11-04 20:28:00
  • 让ASP也支持动态include文件

    2008-05-08 13:00:00
  • 不要使用@import[译]

    2009-05-01 12:01:00
  • Firebox 3 后退后按钮 diasabled 状态不恢复的一个解决方案

    2008-11-06 12:28:00
  • 人性化设计用文字注入情感

    2007-08-22 08:59:00
  • SQL Server数据库对服务器的需求

    2010-06-11 13:52:00
  • 将字符实体引用转换成 Unicode 字符

    2011-07-01 12:31:51
  • 教你制作1px边框表格的四种方法

    2008-10-04 10:16:00
  • 银行账号输入格式化, 支持部分浏缆器

    2007-09-26 18:27:00
  • asp好用的函数集分享

    2011-09-13 12:49:07
  • 空行不空格式排版组织原理

    2010-05-03 14:46:00
  • windows 2000 IIS下配置Php+Mysql+zend的图文教程(完整版)

    2007-06-15 10:51:00
  • 如何建设一个多语言版的ASP网站?

    2009-11-26 20:36:00
  • asp之家 网络编程 m.aspxhome.com