`
luliangok
  • 浏览: 771738 次
文章分类
社区版块
存档分类
最新评论

asp动态生成excel文件

 
阅读更多

<!-- #include virtual="inc/conn.asp" -->
<%
'##################实现将动态生成excel表格,并将内容一并写入######################
'#######################Code CreateBy guofeng#####################################
%>
<HTML>
<HEAD>
<meta content="text/html; charset=gb2312" http-equiv="Content-Type">
<TITLE>生成EXCEL文件</TITLE>
</HEAD>
<body>
<a href="text.asp?act=make">生成在交费记录的EXCEL</a>
<hr size=1 align=left width=300px>
<%
if Request("act") = "" then
else
'######################主程序开始###################
'###############Code CreateBy guofeng###############
dim sql,filename,fs,myfile,x,link

Set fs = server.CreateObject("scripting.filesystemobject")
'--假设你想让生成的EXCEL文件做如下的存放
filename = "E:/龙山纪念林客户管理/online.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 population,hourpos,datepos from populationperhour order by datepos,hourpos asc"
sql="select * from basic order by id"
rs.Open sql,conn
if rs.EOF and rs.BOF then
else
'######################开始写入###################
'###############Code CreateBy guofeng###############
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

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>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics