ASP:把access数据库中的数据导入到Excel中

下面是access数据库中的数据导入到Excel的代码

<%
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())&"时"
daytime="contacts"'文件名称
Set rs = Server.CreateObject("ADODB.Recordset")
'--从数据库中把你想放到EXCEL中的数据查出来
sql="select Name,Tele from Booking group by Name,Tele"
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>"
end if
%>


展开阅读全文

没有更多推荐了,返回首页