在网上看了好些人的代码,研究了一段时间,今天终于把asp导出Excel报表程序做出来了,不敢独享,拿出来与大家分享! 这个是导出页面,一定得加target="_blank",不然后导出的数据不会自动弹出!
<form name="thisForm1" method="post" action="2Excel.asp" target="_blank"> <input type="submit" name="Submit" value="导出" id=Submit> </form>
下面是具体的导出程序:
<!--#Include File="Conn.Asp"--> <% FileName = Year(Date)&"-"&Month(Date)&"-"&Day(Date)&"小鱼留言表.xls" '表名 FilePath = "DownLoadFile/"&FileName '下载路径
Set Fs = Server.CreateObject("Scripting.FileSystemObject") Set Fout = fs.Createtextfile(Server.MapPath(FilePath),true) Sql= "Select * From Book Where User_Name = 'abc' Order By Id Desc"
Set Rs = Server.CreateObject("Adodb.RecordSet") Rs.Open Sql,Conn,1,1 If Rs.Eof And Rs.Bof Then Call CloseDB() '关闭连接 Response.Write "<br><br><br>在此日期范围内无留言!" Else strLine=" ID号"&chr(9)&"主题"&chr(9)&" 姓名"&chr(9)&" 电话"&chr(9)&" 年龄"&chr(9)&" 性别"&chr(9)&" Email"&chr(9)&" 地址"&chr(9)&" 邮编"&chr(9)&" 内容"&chr(9)&" 日期" fout.writeline Replace(strLine,chr(13)&chr(10),"") Do While Not Rs.Eof If Rs("sex")=-1 Then Sex="男" If Rs("sex")=0 Then Sex="女" 'strLine strLine=Rs("id")&chr(9)&Rs("subject")&chr(9)&Rs("name")&chr(9)&"_"&Rs("phone")&chr(9)&Rs("age")&chr(9)&Sex&chr(9)&Rs("email")&chr(9)&Rs("address")&chr(9)&Rs("zip")&chr(9)&Rs("memo")&chr(9)&" "&Rs("adddate") fout.writeline Replace(strLine,chr(13)&chr(10),"") Rs.MoveNext Loop fout.writeline Replace(strLine,chr(13)&chr(10),"") Response.Write("<script language='javascript'>location.replace('"&FilePath&"');</script>") '这一句是自动弹出导出的Excel Response.End Call CloseDB() End If %>
上面的程序: FileName = Year(Date)&"-"&Month(Date)&"-"&Day(Date)&"小鱼留言表.xls" '表名 若改成.doc,则会自动生成doc文件 |