ASP导出为Word或Excel的最简单方法

asp

16-8-18 10:31:49

EXCEL  
<%  
Response.ContentType ="application/vnd.ms-excel"  
Response.AddHeader "Content-Disposition", "attachment; filename=红宝网络表格.xls"  
%>  
  
WORD  
<%  
Response.ContentType ="application/vnd.ms-word"  
Response.AddHeader "Content-Disposition", "attachment; filename=红宝网络文档.doc"  

%>  



导出为WORD时,若文档中含有表格,需要打打印,则要在导出的页面中加入下面的样式。

<style type="text/css">  
<!--  
table{  
border-collapse:collapse;border:none;mso-border-alt:solid windowtext .5pt;  
mso-yfti-tbllook:480;mso-padding-alt:0cm 5.4pt 0cm 5.4pt;mso-border-insideh:  
.5pt solid windowtext;mso-border-insidev:.5pt solid windowtext;border-left:solid windowtext 1.0pt;border-top:solid windowtext 1.0pt;  
}  
td{  
border-top:none;border-left:  
none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;  
mso-border-top-alt:solid windowtext .5pt;mso-border-left-alt:solid windowtext .5pt;  
mso-border-alt:solid windowtext .5pt;padding:0cm 5.4pt 0cm 5.4pt;  
}  
-->  
</style>  



导出为EXCEL文件的实例代码如下:

<!--#include file="Config.asp" -->  
<%Response.ContentType ="application/vnd.ms-excel"    
Response.AddHeader "Content-Disposition", "attachment; filename=chengji.xls"%>  
<!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=utf-8" />  
<title>打字成绩</title>  
<style type="text/css">  
.tableWg {border:1px solid #9bbde6;}  
.tableWg tr{text-align:center;}  
.tableWg td{ border-bottom:1px dotted #9bbde6; border-right:1px dotted #9bbde6;}  
</style>  
</head>  
<body>  
<%sql=session("chengjisql")  
response.Write hbwl.dbSelect(sql,0,1,"",0,"",0,"tableWg")%>  
</body>  
</html>  
<%set hbwl=nothing%>