查看: 193|回复: 5

[问与答] ASP通过条件查询记录并保存下载

[复制链接]
伏地魔 发表于 2024-3-6 18:19:22 | 显示全部楼层 |阅读模式
本帖最后由 伏地魔 于 2024-3-6 18:26 编辑

我是一个ASP半调子爱好者,自己做了个程序想实现条件查询后的下载,试了好多次都没实现!请大神帮着给看看,谢谢!

【页面1】
查询条件输入页面,通过FORM方式获取time1、time2、time3、time4的值(页面截图如下)
8f179876d44048638f6cf9eb83de13b2.png

【页面2】
以下为【页面2】的原代码
  1. <!--#include file="conn.asp"-->
  2. <%
  3. time1=request("time1")
  4. time2=request("time2")
  5. time3=request("time3")
  6. time4=request("time4")

  7. dim filename,fs,myfile,s,x  
  8. Set fs = server.CreateObject("scripting.filesystemobject")
  9. '--假设你想让生成的EXCEL文件做如下的存放  
  10. filename = Server.MapPath("download.xls")
  11. '--如果原来的EXCEL文件存在的话删除它
  12. if fs.FileExists(filename) then
  13. fs.DeleteFile(filename)
  14. end if

  15. '--创建EXCEL文件  
  16. set myfile = fs.CreateTextFile(filename,true)  
  17. if time1<>"" and time2<>"" then
  18.         sql = "SELECT * FROM sp WHERE time>=#"&time1&" 00:00:00# and time<=#"&time2&" 23:59:59# order by id desc"
  19. else
  20.         if time3<>"" and time4<>"" then
  21.                 sql = "SELECT * FROM sp WHERE exittime>=#"&time3&" 00:00:00# and exittime<=#"&time4&" 23:59:59# order by id desc"
  22.         else
  23.                 sql = "select * from sp order by gdbj asc, id desc"
  24.         end if
  25. end if
  26. Set rs=server.createobject("adodb.recordset")
  27. rs.open sql,db,3,1
  28. if not rs.EOF and not rs.BOF then  
  29. dim trLine,responsestr  
  30. strLine="序号"&chr(9)&"受理工号"&chr(9)&"用户名"&chr(9)&"办理号"&chr(9)&"联系号"&chr(9)&"是否弹窗"&chr(9)&"分类1"&chr(9)&"分类2"&chr(9)&"用户需求"&chr(9)&"登记时间"&chr(9)&"处理结果"&chr(9)&"处理人"&chr(9)&"处理备注"&chr(9)&"处理时间"

  31. '--将表的列名先写入EXCEL
  32. myfile.writeline strLine
  33. Do while Not rs.EOF
  34. strLine=id&chr(9)&tijiaogonghao&chr(9)&yonghuming&chr(9)&guzhanghao&chr(9)&lianxidianhua&chr(9)&sp&chr(9)&sp1&chr(9)&sp2&chr(9)&beizhu&chr(9)&time&chr(9)&chuli&chr(9)&exitname&chr(9)&exitbeizhu&chr(9)&exittime
  35. myfile.writeline strLine
  36. rs.MoveNext
  37. loop
  38. end if

  39. response.redirect "download.xls"
  40. link="<A HREF="&filename&"><font color=red><b>导出excel文件</b></font></a>"
  41. Response.write link

  42. rs.Close
  43. set rs = nothing
  44. '返回【页面1】
  45. response.write("window.top.location.href='download.asp';")

  46. %>
复制代码
xuanxiao 发表于 2024-3-6 23:21:48 | 显示全部楼层
也不用后端,纯前端就可以实现。
 楼主| 伏地魔 发表于 2024-3-7 08:48:31 | 显示全部楼层
xuanxiao 发表于 2024-3-6 23:21
也不用后端,纯前端就可以实现。

那我这个是哪出问题了,请大神指导一下,谢谢
xuanxiao 发表于 2024-3-7 17:21:10 | 显示全部楼层
伏地魔 发表于 2024-3-7 08:48
那我这个是哪出问题了,请大神指导一下,谢谢


ASP导出EXCEL.rar (33 KB, 下载次数: 0, 售价: 5 个ASP币)

  1.     <script>
  2.             function ExportToExcel() {        
  3.                  $("#myTable").table2excel({
  4.                   name: "Backup file for HTML content",              
  5.                   filename: "表格.xls",                                  
  6.                                 exclude_img: true,
  7.                                 exclude_links: true,
  8.                                 exclude_inputs: true,
  9.                                 preserveColors: false
  10.                                   
  11.               });         
  12.          
  13.             }
  14.        
  15.     </script>
复制代码


参考代码,替换掉table内容就可以了。
1.png

生成的效果
2.png
一往情僧 发表于 2024-3-19 18:42:59 | 显示全部楼层
也可以先查询到页面,加个存为EXCEL的代码就行。
xuanxiao 发表于 2024-3-20 10:17:03 | 显示全部楼层
一往情僧 发表于 2024-3-19 18:42
也可以先查询到页面,加个存为EXCEL的代码就行。

对 ,这个思路是可以的
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表

在线客服

售前咨询
售后咨询
服务热线
023-58418553
微信公众号