我在做一项目时,客户要求要将从数据库中获取数据后的ASP页面导出成EXCEL或WORD文档。经本人试验后找出了最简单的方法:在ASP文件的最开头位置加入下面的代码就可以了,非常简单。
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="hbwlConfig.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%>