asp代码实现access数据导出到excel文件:
一,下面是导出XLS格式
<% dim referer referer = Request.ServerVariables("HTTP_REFERER") Dim CName,action action=request.Form("action") CName="../../Excel/" dim daytime ’daytime=year(now())&"-"&month(now())&"-"&day(now())&"-"&hour(now())&"-"&Minute(now()) daytime=year(now())&"年"&month(now())&"月"&day(now())&"日"&hour(now())&"时" Set rs = Server.CreateObject("ADODB.Recordset") ’--从数据库中把你想放到EXCEL中的数据查出来 sql="select * from gbook order by id desc" rs.Open sql,conn if rs.EOF and rs.BOF then response.write "<script>alert(’操作出错,下面是产生错误的可能原因:\n\n库里暂时没有数据,所以不能导出EXCEL文件!’);location.href = ’"&referer&"’;</script>" response.end else dim filename,fs,myfile,x Set fs = server.CreateObject("scripting.filesystemobject") ’--假设你想让生成的EXCEL文件做如下的存放 filename = Server.MapPath(""&CName&""&daytime&".xls") ’--如果原来的EXCEL文件存在的话删除它 if fs.FileExists(filename) then fs.DeleteFile(filename) end if ’--创建EXCEL文件 set myfile = fs.CreateTextFile(filename,true) dim strLine,responsestr strLine="" For each x in rs.fields strLine= strLine & x.name & chr(9) Next ’--将表的列名先写入EXCEL myfile.writeline strLine Do while Not rs.EOF strLine="" for each x in rs.Fields strLine= strLine & x.value & chr(9) next ’--将表的数据写入EXCEL myfile.writeline strLine rs.MoveNext loop rs.Close set rs = nothing conn.close set conn = nothing set myfile = nothing Set fs=Nothing Response.Write "<BR><BR><center><b>导出成功,请选择继续操作</b></center>" Response.Write "<br>" response.Write "<table width=90% cellspacing=1 cellpadding=3 align=center>" Response.Write "<tr align=center> <td>" response.write ("<font color=green>√</font><a href=’"&CName&""&ENames&".xls’>下载</a>") & " <font color= green>√</font><a href=’"&referer&"’>返回</a>" Response.Write "</td></tr></table>" end if %> |
二,下面是导出CSV格式
<% dim referer referer = Request.ServerVariables("HTTP_REFERER") Dim CName,action action=request.Form("action") CName="../../Excel/" dim daytime ’daytime=year(now())&"-"&month(now())&"-"&day(now())&"-"&hour(now())&"-"&Minute(now()) daytime=year(now())&"年"&month(now())&"月"&day(now())&"日"&hour(now())&"时" Set rs = Server.CreateObject("ADODB.Recordset") ’--从数据库中把你想放到EXCEL中的数据查出来 sql="select * from gbook order by id desc" rs.Open sql,conn if rs.EOF and rs.BOF then response.write "<script>alert(’操作出错,下面是产生错误的可能原因:\n\n库里暂时没有数据,所以不能导出EXCEL文件!’);location.href = ’"&referer&"’;</script>" response.end else Dim Fs,Exc Dim Excelstring,ExcelHead,Excelstr,Excelstr2 Dim Scou_FileNamen,Scou_FieldList Dim Dest_FileName,Dest_FieldList Dest_FileName=""&CName&""&daytime&".csv" Excelstring = "" ExcelHead = "识别号" & "," & "主题类型" & "," & "公司名称"& "," & "姓名"& "," & "性别"& "," & "职位"& "," & "城市省份" & "," & "城市" & "," & "公司地址"& "," & "邮编"& "," & "邮件"& "," & "电话"& "," & "传真" & "," & "手机"& "," & "公司网址"& "," & "详细内容"& "," & "IP地址"& "," & "留言时间" Set Rs = Server.CreateObject ("ADODB.Recordset") Sql = "Select * From gbook" Rs.Open Sql,Conn,1,3 Do While Not Rs.Eof Excelstr = Rs("ID") & "," & Rs("zhutiname") & "," & Rs("gsname") & "," & Rs("gsname") & "," & Rs("sex") & "," & Rs("zhiwei")& "," & Rs("Province")& "," & Rs("City") & "," & Rs("dizhi") & "," & Rs("youbian") & "," & Rs("email") & "," & Rs("tel")& "," & Rs("fax")& "," & Rs("tel2") & "," & Rs("homeurl") & "," & Rs("body") & "," & Rs("ip") & "," & Rs("addtime") Excelstr2 = Excelstr2 & Chr(13) & Excelstr Rs.Movenext Loop Excelstring = ExcelHead & Excelstr2 Rs.Close Set Rs=Nothing Set Fs = CreateObject("Scripting.FileSystemObject") Set Exc = Fs.OpenTextFile(Server.MapPath(Dest_FileName),2,True) Exc.Write(Excelstring) If Fs.FileExists(Server.MapPath(Dest_FileName)) Then Response.Write("<a href=’"&Dest_FileName&"’>报表已经生成,点击查看</a><hr>") Response.Write("<a href=’"&referer&"’>返回</a>") ’Response.write(Excelstring) Else Response.Write("生成报表失败!") End If end if %> |
相关视频
相关阅读 asp下面javascript上传图片限制格式大小方法ie6下面asp.net mvc3 部署应用程序ASP网站安装不成功的解决办法asp的日期转换星座函数快速掌握ASP连接11种数据库的常用语法ASP将access数据导出为excel电子表的方法技巧:用ASP在线创建Word与Excel文档ASP木马密码加密的破解方法
热门文章 没有查询到任何记录。
最新文章
《龙珠:超宇宙》 战斗E3 2014:瘆人僵尸《消
asp代码实现access数据导出到excel文件如何使用FSO读取Js文件内容并可以编辑修改对初学者有用的一些asp函数集学习ASP编程必会的代码
人气排行 asp代码实现access数据导出到excel文件asp不需要任何配置的伪静态实现如何使用FSO读取Js文件内容并可以编辑修改asp去除html标记和空格的代码Asp全选删除代码教大家网页伪静态知识及其2种实现方法Microsoft SQL Server 7.0安装问题(一)ASP.NET中的Code Behind技术4
查看所有0条评论>>