从数据库中查询数据后想以EXCEL报表存放到虚拟目录下

fengzhichaowuxi 2004-05-06 10:07:56

<%@ LANGUAGE="VBSCRIPT" %>
<%option explicit%>

<HTML>
<HEAD>
<meta content="text/html; charset=gb2312" http-equiv="Content-Type">
<TITLE>生成EXCEL文件</TITLE>
</HEAD>
<body>
<a href="makeexcel.asp?act=make">生成EXCEL</a>
<hr size=1 align=left width=300px>
<%
if Request("act") = "" then
else

dim conn,connString
set conn=server.CreateObject("adodb.connection")
connString = "Driver={SQL Server};Server=127.0.0.1;Database=test;Uid=sa;Pwd=testpwd;"

conn.Open connString

dim rs,sql,filename,fs,myfile,x,link

Set fs = server.CreateObject("scripting.filesystemobject")
'--假设你想让生成的EXCEL文件做如下的存放

filename = Server.MapPath("c:\inetpub\wwwroot\sheet\333.xls")
'--如果原来的EXCEL文件存在的话删除它
if fs.FileExists(filename) then
fs.DeleteFile(filename)
end if
'--创建EXCEL文件
set myfile=fs.CreateTextFile(filename,true)


Set rs = Server.CreateObject("ADODB.Recordset")
'--从数据库中把你想放到EXCEL中的数据查出来
sql = "select * from testdb WHERE 考核日期 between '2002-02-28' and '2002-03-29' order by 考核日期 desc"
rs.Open sql,conn
if rs.EOF and rs.BOF then

else

dim strLine,responsestr
strLine=""
For each x in rs.fields
strLine= strLine & x.name & chr(9)
Next

'--将表的列名先写入EXCEL
myfile.writeline strLine

dim fieldValue
Do while Not rs.EOF
strLine=""

for each x in rs.Fields
fieldValue = x.value
if not IsNull(fieldValue) then
fieldValue=Replace(x.value,chr(13)&chr(10),"||")
else
end if

strLine= strLine & fieldValue & chr(9)
'strLine= strLine & x.value & chr(9)
next
'--将表的数据写入EXCEL
myfile.writeline strLine

rs.MoveNext
loop

end if

rs.Close
set rs = nothing
conn.close
set conn = nothing
set myfile = nothing
Set fs=Nothing

link="<A HREF=" & filename & ">Open The Excel File</a>"
Response.write link
end if
%>
</BODY>
</HTML>

我在从数据库中查询数据后想以EXCEL报表存放到c:\inetpub\wwwroot\sheet下,可是点击超链接之后提示找不到文件,请问各位高手是怎么回事,或是否有更好的程序提供?小弟刚学ASP,是在文本编辑器中写的原代码。谢谢!!!




...全文
71 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
射天狼 2004-05-06
  • 打赏
  • 举报
回复
你把EXCEL文件放在客户的机器上还是放在服务器的机器上,B/S和C/S执行的原理不一样,B/S是在服务器上解释之后再到客户端执行的!你在浏览器中看一下源码!!
思想问题!!!!
online 2004-05-06
  • 打赏
  • 举报
回复
怎么跑到vb版来了?

看这个路径下有没有该文件
filename = Server.MapPath("c:\inetpub\wwwroot\sheet\333.xls")

28,409

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧